Skip to content

Commit ec56dff

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 462cc2b commit ec56dff

File tree

2 files changed

+63
-20
lines changed

2 files changed

+63
-20
lines changed

acctproc.c

+59-20
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 */
@@ -249,6 +252,19 @@ acctswon(void)
249252
}
250253
}
251254

255+
/*
256+
** when a particular environment variable is present, atop should
257+
** use a specific directory for accounting-file (as defined by the environment
258+
** variable)
259+
*/
260+
if ( (ep = getenv(ACCTDIRENV)) && *ep)
261+
{
262+
acctdir = ep;
263+
}
264+
265+
char acctfilepath[128];
266+
snprintf(acctfilepath, sizeof acctfilepath, "%s/%s", acctdir, ACCTFILE);
267+
252268
/*
253269
** when the atopacctd daemon is active on this system,
254270
** it should be the preferred way to read the accounting records
@@ -435,16 +451,16 @@ acctswon(void)
435451
** if this directory exists (e.g. previous atop-run killed)
436452
** it will be cleaned and newly created
437453
*/
438-
if ( mkdir(ACCTDIR, 0700) == -1)
454+
if ( mkdir(acctdir, 0700) == -1)
439455
{
440456
if (errno == EEXIST)
441457
{
442458
(void) acct(0);
443-
(void) unlink(ACCTDIR "/" ACCTFILE);
444-
(void) rmdir(ACCTDIR);
459+
(void) unlink(acctfilepath);
460+
(void) rmdir(acctdir);
445461
}
446462

447-
if ( mkdir(ACCTDIR, 0700) == -1)
463+
if ( mkdir(acctdir, 0700) == -1)
448464
{
449465
/*
450466
** persistent failure
@@ -453,19 +469,18 @@ acctswon(void)
453469
return 4;
454470
}
455471
}
456-
457472
/*
458473
** create accounting file in new directory
459474
*/
460-
(void) close( creat(ACCTDIR "/" ACCTFILE, 0600) );
475+
(void) close( creat( acctfilepath, 0600) );
461476

462477
/*
463478
** switch on accounting
464479
*/
465-
if ( acct(ACCTDIR "/" ACCTFILE) < 0)
480+
if ( acct( acctfilepath ) < 0)
466481
{
467-
(void) unlink(ACCTDIR "/" ACCTFILE);
468-
(void) rmdir(ACCTDIR);
482+
(void) unlink(acctfilepath);
483+
(void) rmdir(acctdir);
469484
(void) semop(sematopid, &semrelse, 1);
470485

471486
return 5;
@@ -476,11 +491,11 @@ acctswon(void)
476491
** accounting is switched on now;
477492
** open accounting-file
478493
*/
479-
if ( (acctfd = open(ACCTDIR "/" ACCTFILE, O_RDONLY) ) < 0)
494+
if ( (acctfd = open(acctfilepath, O_RDONLY) ) < 0)
480495
{
481496
(void) acct(0);
482-
(void) unlink(ACCTDIR "/" ACCTFILE);
483-
(void) rmdir(ACCTDIR);
497+
(void) unlink(acctfilepath);
498+
(void) rmdir(acctdir);
484499

485500
(void) semop(sematopid, &semrelse, 1);
486501
return 1;
@@ -501,8 +516,8 @@ acctswon(void)
501516
{
502517
(void) acct(0);
503518
(void) close(acctfd);
504-
(void) unlink(ACCTDIR "/" ACCTFILE);
505-
(void) rmdir(ACCTDIR);
519+
(void) unlink(acctfilepath);
520+
(void) rmdir(acctdir);
506521

507522
acctfd = -1;
508523
return 1;
@@ -524,8 +539,8 @@ acctswon(void)
524539
{
525540
(void) acct(0);
526541
(void) close(acctfd);
527-
(void) unlink(ACCTDIR "/" ACCTFILE);
528-
(void) rmdir(ACCTDIR);
542+
(void) unlink(acctfilepath);
543+
(void) rmdir(acctdir);
529544

530545
acctfd = -1;
531546
return 1;
@@ -635,14 +650,24 @@ acctswoff(void)
635650

636651
if (after.st_size > before.st_size)
637652
{
653+
654+
char *ep;
655+
if ( (ep = getenv(ACCTDIRENV)) && *ep)
656+
{
657+
acctdir = ep;
658+
}
659+
660+
char acctfilepath[128];
661+
snprintf(acctfilepath, sizeof acctfilepath, "%s/%s", acctdir, ACCTFILE);
662+
638663
/*
639664
** remove the directory and file
640665
*/
641666
regainrootprivs(); /* get root privs again */
642667

643668
(void) acct(0);
644-
(void) unlink(ACCTDIR "/" ACCTFILE);
645-
(void) rmdir(ACCTDIR);
669+
(void) unlink(acctfilepath);
670+
(void) rmdir(acctdir);
646671

647672
if (! droprootprivs() )
648673
mcleanstop(42,
@@ -982,10 +1007,24 @@ acctrestarttrial()
9821007

9831008
(void) acct(0); // switch off accounting
9841009

985-
if ( truncate(ACCTDIR "/" ACCTFILE, 0) == 0)
1010+
/*
1011+
** when a particular environment variable is present, atop should
1012+
** use a specific directory for accounting-file (as defined by the environment
1013+
** variable)
1014+
*/
1015+
char *ep;
1016+
if ( (ep = getenv(ACCTDIRENV)) && *ep)
1017+
{
1018+
acctdir = ep;
1019+
}
1020+
1021+
char acctfilepath[128];
1022+
snprintf(acctfilepath, sizeof acctfilepath, "%s/%s", acctdir, ACCTFILE);
1023+
1024+
if ( truncate(acctfilepath, 0) == 0)
9861025
(void) lseek(acctfd, 0, SEEK_SET);
9871026

988-
(void) acct(ACCTDIR "/" ACCTFILE);
1027+
(void) acct(acctfilepath);
9891028

9901029
if (! droprootprivs() )
9911030
mcleanstop(42, "failed to drop root privs\n");

man/atop.1

+4
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)