Skip to content

Commit e691554

Browse files
committed
Add support ATOPACCTDIR env variable
Now we can set custom process accounting directory via env variable ATOPACCTDIR instead of ACCTDIR constant.
1 parent b55f28a commit e691554

2 files changed

Lines changed: 63 additions & 20 deletions

File tree

acctproc.c

Lines changed: 59 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,12 @@
134134
#include "atopacctd.h"
135135

136136
#define ACCTDIR "/var/cache/atop.d"
137+
#define ACCTDIRENV "ATOPACCTDIR"
137138
#define ACCTFILE "atop.acct"
138139
#define ACCTENV "ATOPACCT"
139140

141+
static char *acctdir = ACCTDIR;
142+
140143
static char acctatop; /* boolean: atop's own accounting busy ? */
141144
static off_t acctsize; /* previous size of account file */
142145
static int acctrecsz; /* size of account record */
@@ -246,6 +249,19 @@ acctswon(void)
246249
}
247250
}
248251

252+
/*
253+
** when a particular environment variable is present, atop should
254+
** use a specific directory for accounting-file (as defined by the environment
255+
** variable)
256+
*/
257+
if ( (ep = getenv(ACCTDIRENV)) && *ep)
258+
{
259+
acctdir = ep;
260+
}
261+
262+
char acctfilepath[128];
263+
snprintf(acctfilepath, sizeof acctfilepath, "%s/%s", acctdir, ACCTFILE);
264+
249265
/*
250266
** when the atopacctd daemon is active on this system,
251267
** it should be the preferred way to read the accounting records
@@ -420,16 +436,16 @@ acctswon(void)
420436
** if this directory exists (e.g. previous atop-run killed)
421437
** it will be cleaned and newly created
422438
*/
423-
if ( mkdir(ACCTDIR, 0700) == -1)
439+
if ( mkdir(acctdir, 0700) == -1)
424440
{
425441
if (errno == EEXIST)
426442
{
427443
(void) acct(0);
428-
(void) unlink(ACCTDIR "/" ACCTFILE);
429-
(void) rmdir(ACCTDIR);
444+
(void) unlink(acctfilepath);
445+
(void) rmdir(acctdir);
430446
}
431447

432-
if ( mkdir(ACCTDIR, 0700) == -1)
448+
if ( mkdir(acctdir, 0700) == -1)
433449
{
434450
/*
435451
** persistent failure
@@ -438,19 +454,18 @@ acctswon(void)
438454
return 4;
439455
}
440456
}
441-
442457
/*
443458
** create accounting file in new directory
444459
*/
445-
(void) close( creat(ACCTDIR "/" ACCTFILE, 0600) );
460+
(void) close( creat( acctfilepath, 0600) );
446461

447462
/*
448463
** switch on accounting
449464
*/
450-
if ( acct(ACCTDIR "/" ACCTFILE) < 0)
465+
if ( acct( acctfilepath ) < 0)
451466
{
452-
(void) unlink(ACCTDIR "/" ACCTFILE);
453-
(void) rmdir(ACCTDIR);
467+
(void) unlink(acctfilepath);
468+
(void) rmdir(acctdir);
454469
(void) semop(sematopid, &semrelse, 1);
455470

456471
return 5;
@@ -461,11 +476,11 @@ acctswon(void)
461476
** accounting is switched on now;
462477
** open accounting-file
463478
*/
464-
if ( (acctfd = open(ACCTDIR "/" ACCTFILE, O_RDONLY) ) < 0)
479+
if ( (acctfd = open(acctfilepath, O_RDONLY) ) < 0)
465480
{
466481
(void) acct(0);
467-
(void) unlink(ACCTDIR "/" ACCTFILE);
468-
(void) rmdir(ACCTDIR);
482+
(void) unlink(acctfilepath);
483+
(void) rmdir(acctdir);
469484

470485
(void) semop(sematopid, &semrelse, 1);
471486
return 1;
@@ -486,8 +501,8 @@ acctswon(void)
486501
{
487502
(void) acct(0);
488503
(void) close(acctfd);
489-
(void) unlink(ACCTDIR "/" ACCTFILE);
490-
(void) rmdir(ACCTDIR);
504+
(void) unlink(acctfilepath);
505+
(void) rmdir(acctdir);
491506

492507
acctfd = -1;
493508
return 1;
@@ -509,8 +524,8 @@ acctswon(void)
509524
{
510525
(void) acct(0);
511526
(void) close(acctfd);
512-
(void) unlink(ACCTDIR "/" ACCTFILE);
513-
(void) rmdir(ACCTDIR);
527+
(void) unlink(acctfilepath);
528+
(void) rmdir(acctdir);
514529

515530
acctfd = -1;
516531
return 1;
@@ -621,14 +636,24 @@ acctswoff(void)
621636

622637
if (after.st_size > before.st_size)
623638
{
639+
640+
char *ep;
641+
if ( (ep = getenv(ACCTDIRENV)) && *ep)
642+
{
643+
acctdir = ep;
644+
}
645+
646+
char acctfilepath[128];
647+
snprintf(acctfilepath, sizeof acctfilepath, "%s/%s", acctdir, ACCTFILE);
648+
624649
/*
625650
** remove the directory and file
626651
*/
627652
regainrootprivs(); /* get root privs again */
628653

629654
(void) acct(0);
630-
(void) unlink(ACCTDIR "/" ACCTFILE);
631-
(void) rmdir(ACCTDIR);
655+
(void) unlink(acctfilepath);
656+
(void) rmdir(acctdir);
632657

633658
if (! droprootprivs() )
634659
cleanstop(42);
@@ -965,10 +990,24 @@ acctrestarttrial()
965990

966991
(void) acct(0); // switch off accounting
967992

968-
if ( truncate(ACCTDIR "/" ACCTFILE, 0) == 0)
993+
/*
994+
** when a particular environment variable is present, atop should
995+
** use a specific directory for accounting-file (as defined by the environment
996+
** variable)
997+
*/
998+
char *ep;
999+
if ( (ep = getenv(ACCTDIRENV)) && *ep)
1000+
{
1001+
acctdir = ep;
1002+
}
1003+
1004+
char acctfilepath[128];
1005+
snprintf(acctfilepath, sizeof acctfilepath, "%s/%s", acctdir, ACCTFILE);
1006+
1007+
if ( truncate(acctfilepath, 0) == 0)
9691008
(void) lseek(acctfd, 0, SEEK_SET);
9701009

971-
(void) acct(ACCTDIR "/" ACCTFILE);
1010+
(void) acct(acctfilepath);
9721011

9731012
if (! droprootprivs() )
9741013
cleanstop(42);

man/atop.1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,10 @@ will never read more than 50 MiB with process information from the
223223
process accounting file per interval (approx. 70000 finished processes).
224224
In interactive mode a warning is given whenever processes have been skipped
225225
for this reason.
226+
227+
When the environment variable ATOPACCTDIR set and not empty, it specifies directory for process accounting file instead of
228+
.I /var/cache/atop.d
229+
which set by default.
226230
.PP
227231
.SH COLORS
228232
For the resource consumption on system level,

0 commit comments

Comments
 (0)