1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
|
/*
** ATOP - System & Process Monitor
**
** The program 'atop' offers the possibility to view the activity of
** the system on system-level as well as process-level.
**
** This source-file contains functions to manipulate with process-accounting
** features (switch it on/off and read the process-accounting records).
** ================================================================
** Author: Gerlof Langeveld
** E-mail: gerlof.langeveld@atoptool.nl
** Date: November 1996
** LINUX-port: June 2000
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 2, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful, but
** WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU General Public License for more details.
*/
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
#include <sys/types.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include "atop.h"
#include "photoproc.h"
#include "acctproc.h"
#include "atopacctd.h"
#define ACCTDIR "/var/cache/atop.d"
#define ACCTFILE "atop.acct"
#define ACCTENV "ATOPACCT"
static char acctatop; /* boolean: atop's own accounting busy ? */
static off_t acctsize; /* previous size of account file */
static int acctrecsz; /* size of account record */
static int acctversion; /* version of account record layout */
static int acctfd = -1; /* fd of account file (-1 = not open) */
static long curshadowseq; /* current shadow file sequence number */
static long maxshadowrec; /* number of records per shadow file */
static char *pacctdir = PACCTDIR;
static count_t acctexp (comp_t ct);
static int acctvers(int);
static void acctrestarttrial();
static void switchshadow(void);
/*
** possible process accounting files used by (ps)acct package
*/
struct pacctadm {
char *name;
struct stat stat;
} pacctadm[] = {
{ "/var/log/pacct", {0, }, },
{ "/var/account/pacct", {0, }, },
{ "/var/log/account/pacct", {0, }, }
};
struct pacctadm *pacctcur; // pointer to entry in use
/*
** Semaphore-handling
**
** A semaphore-group with two semaphores is created
**
** 0 - Binary semaphore (mutex) to get access to active atop-counter
**
** 1 - Active atop-counter (inverted).
** This semaphore is initialized at some high value and is
** decremented by every atop-incarnation which uses the private
** accounting-file and incremented as soon as such atop stops again.
** If an atop-incarnation stops and it appears to be the last one
** using the private accounting-file, accounting is stopped
** and the file removed
** (Yes, I know: it's not proper usage of the semaphore-principle).
*/
#define ATOPACCTKEY 3121959
#define ATOPACCTTOT 100
struct sembuf semclaim = {0, -1, SEM_UNDO},
semrelse = {0, +1, SEM_UNDO},
semdecre = {1, -1, SEM_UNDO},
semincre = {1, +1, SEM_UNDO};
struct sembuf semreglock[] = {{0, -1, SEM_UNDO|IPC_NOWAIT},
{1, -1, SEM_UNDO|IPC_NOWAIT}},
semunlock = {1, +1, SEM_UNDO};
/*
** switch on the process-accounting mechanism
**
** return value:
** 0 - activated (success)
** 1 - not activated: unreadable accounting file
** 2 - not activated: empty environment variable ATOPACCT
** 3 - not activated: no access to semaphore group
** 4 - not activated: impossible to create own accounting file
** 5 - not activated: no root privileges
*/
int
acctswon(void)
{
int i, j, sematopid, sempacctpubid;
static ushort vals[] = {1, ATOPACCTTOT};
union {ushort *array;} arg = {vals};
struct stat statbuf;
char *ep;
/*
** when a particular environment variable is present, atop should
** use a specific accounting-file (as defined by the environment
** variable) or should use no process accounting at all (when
** contents of environment variable is empty)
*/
if ( (ep = getenv(ACCTENV)) )
{
/*
** environment variable exists
*/
if (*ep)
{
/*
** open active account file with the specified name
*/
if (! droprootprivs() )
mcleanstop(42, "failed to drop root privs\n");
if ( (acctfd = open(ep, O_RDONLY) ) == -1)
return 1;
if ( !acctvers(acctfd) )
{
(void) close(acctfd);
acctfd = -1;
return 1;
}
supportflags |= ACCTACTIVE;
return 0;
}
else
{
/*
** no contents
*/
return 2;
}
}
/*
** when the atopacctd daemon is active on this system,
** it should be the preferred way to read the accounting records
*/
if ( (sempacctpubid = semget(PACCTPUBKEY, 2, 0)) != -1)
{
FILE *cfp;
char shadowpath[128];
struct flock flock;
struct timespec maxsemwait = {3, 0};
if (! droprootprivs() )
mcleanstop(42, "failed to drop root privs\n");
if (semtimedop(sempacctpubid, semreglock, 2, &maxsemwait) == -1)
{
acctfd = -1;
regainrootprivs();
return 3;
}
snprintf(shadowpath, sizeof shadowpath, "%s/%s/%s",
pacctdir, PACCTSHADOWD, PACCTSHADOWC);
if ( (cfp = fopen(shadowpath, "r")) )
{
if (fscanf(cfp, "%ld/%ld",
&curshadowseq, &maxshadowrec) == 2)
{
fclose(cfp);
snprintf(shadowpath, sizeof shadowpath,
PACCTSHADOWF, pacctdir,
PACCTSHADOWD, curshadowseq);
if ( (acctfd = open(shadowpath, O_RDONLY))!=-1)
{
if ( !acctvers(acctfd) )
{
int maxcnt = 40;
if ( fork() == 0 )
exit(0);
(void) wait((int *) 0);
while ( !acctvers(acctfd) &&
--maxcnt)
usleep(50000);
if (!acctversion)
{
(void) close(acctfd);
acctfd = -1;
semop(sempacctpubid,
&semrelse, 1);
semop(sempacctpubid,
&semunlock, 1);
regainrootprivs();
return 1;
}
}
/*
** set read lock on current shadow file
*/
flock.l_type = F_RDLCK;
flock.l_whence = SEEK_SET;
flock.l_start = 0;
flock.l_len = 1;
if ( fcntl(acctfd, F_SETLK, &flock)
!= -1)
{
supportflags |= ACCTACTIVE;
regainrootprivs();
semop(sempacctpubid,
&semunlock, 1);
return 0;
}
(void) close(acctfd);
}
else
{
perror("open shadowpath");
abort();
}
}
else
{
fprintf(stderr,
"fscanf failed on shadow currency\n");
fclose(cfp);
maxshadowrec = 0;
}
}
(void) semop(sempacctpubid, &semrelse, 1);
(void) semop(sempacctpubid, &semunlock, 1);
}
/*
** check if process accounting is already switched on
** for one of the standard accounting-files; in that case we
** open this file and get our info from here ....
*/
for (i=0, j=0; i < sizeof pacctadm/sizeof pacctadm[0]; i++)
{
if ( stat(pacctadm[i].name, &pacctadm[i].stat) == 0)
j++;
}
if (j)
{
/*
** at least one file is present; check if it is really in use
** at this moment for accounting by forking a child-process
** which immediately finishes to force a new accounting-record
*/
if ( fork() == 0 )
exit(0); /* let the child finish */
(void) wait((int *) 0); /* let the parent wait */
for (i=0; i < sizeof pacctadm/sizeof pacctadm[0]; i++)
{
if ( stat(pacctadm[i].name, &statbuf) == 0)
{
/*
** accounting-file has grown?
*/
if (statbuf.st_size > pacctadm[i].stat.st_size)
{
/*
** open active account file
*/
if ( (acctfd = open(pacctadm[i].name,
O_RDONLY) ) == -1)
return 1;
if ( !acctvers(acctfd) )
{
(void) close(acctfd);
acctfd = -1;
return 1;
}
supportflags |= ACCTACTIVE;
pacctcur = &pacctadm[i]; // register current
return 0;
}
}
}
}
/*
** process-accounting is not yet switched on in a standard way;
** check if another atop has switched on accounting already
**
** first try to create a semaphore group exclusively; if this succeeds,
** this is the first atop-incarnation since boot and the semaphore group
** should be initialized`
*/
if ( (sematopid = semget(ATOPACCTKEY, 2, 0600|IPC_CREAT|IPC_EXCL)) >= 0)
(void) semctl(sematopid, 0, SETALL, arg);
else
sematopid = semget(ATOPACCTKEY, 0, 0);
/*
** check if we got access to the semaphore group
*/
if (sematopid == -1)
return 3;
/*
** the semaphore group is opened now; claim exclusive rights
*/
(void) semop(sematopid, &semclaim, 1);
/*
** are we the first to use the accounting-mechanism ?
*/
if (semctl(sematopid, 1, GETVAL, 0) == ATOPACCTTOT)
{
/*
** create a new separate directory below /tmp
** for the accounting file;
** if this directory exists (e.g. previous atop-run killed)
** it will be cleaned and newly created
*/
if ( mkdir(ACCTDIR, 0700) == -1)
{
if (errno == EEXIST)
{
(void) acct(0);
(void) unlink(ACCTDIR "/" ACCTFILE);
(void) rmdir(ACCTDIR);
}
if ( mkdir(ACCTDIR, 0700) == -1)
{
/*
** persistent failure
*/
(void) semop(sematopid, &semrelse, 1);
return 4;
}
}
/*
** create accounting file in new directory
*/
(void) close( creat(ACCTDIR "/" ACCTFILE, 0600) );
/*
** switch on accounting
*/
if ( acct(ACCTDIR "/" ACCTFILE) < 0)
{
(void) unlink(ACCTDIR "/" ACCTFILE);
(void) rmdir(ACCTDIR);
(void) semop(sematopid, &semrelse, 1);
return 5;
}
}
/*
** accounting is switched on now;
** open accounting-file
*/
if ( (acctfd = open(ACCTDIR "/" ACCTFILE, O_RDONLY) ) < 0)
{
(void) acct(0);
(void) unlink(ACCTDIR "/" ACCTFILE);
(void) rmdir(ACCTDIR);
(void) semop(sematopid, &semrelse, 1);
return 1;
}
/*
** accounting successfully switched on
*/
(void) semop(sematopid, &semdecre, 1);
(void) semop(sematopid, &semrelse, 1);
acctatop = 1;
/*
** determine version of accounting-record
*/
if (fstat(acctfd, &statbuf) == -1)
{
(void) acct(0);
(void) close(acctfd);
(void) unlink(ACCTDIR "/" ACCTFILE);
(void) rmdir(ACCTDIR);
acctfd = -1;
return 1;
}
if (statbuf.st_size == 0) /* no acct record written yet */
{
/*
** let's write one record
*/
if ( fork() == 0 )
exit(0); /* let the child finish */
(void) wait((int *) 0); /* let the parent wait */
}
if ( !acctvers(acctfd) )
{
(void) acct(0);
(void) close(acctfd);
(void) unlink(ACCTDIR "/" ACCTFILE);
(void) rmdir(ACCTDIR);
acctfd = -1;
return 1;
}
supportflags |= ACCTACTIVE;
return 0;
}
/*
** determine the version of the accounting-record layout/length
** and reposition the seek-pointer to the end of the accounting file
*/
static int
acctvers(int fd)
{
struct acct tmprec;
/*
** read first record from accounting file to verify
** the second byte (always contains version number)
*/
if ( read(fd, &tmprec, sizeof tmprec) < sizeof tmprec)
return 0;
switch (tmprec.ac_version & 0x0f)
{
case 2:
acctrecsz = sizeof(struct acct);
acctversion = 2;
break;
case 3:
acctrecsz = sizeof(struct acct_v3);
acctversion = 3;
break;
default:
mcleanstop(8, "Unknown format of process accounting file\n");
}
/*
** accounting successfully switched on
*/
acctsize = acctprocnt() * acctrecsz;
/*
** reposition to actual file-size
*/
(void) lseek(fd, acctsize, SEEK_SET);
return 1;
}
/*
** switch off the process-accounting mechanism
*/
void
acctswoff(void)
{
int sematopid;
struct stat before, after;
/*
** if accounting not supported, skip call
*/
if (acctfd == -1)
return;
/*
** our private accounting-file in use?
*/
if (acctatop)
{
acctatop = 0;
/*
** claim the semaphore to get exclusive rights for
** the accounting-manipulation
*/
sematopid = semget(ATOPACCTKEY, 0, 0);
(void) semop(sematopid, &semclaim, 1);
(void) semop(sematopid, &semincre, 1);
/*
** check if we were the last user of accounting
*/
if (semctl(sematopid, 1, GETVAL, 0) == ATOPACCTTOT)
{
/*
** switch off private accounting
**
** verify if private accounting is still in use to
** avoid that we switch off process accounting that
** has been activated manually in the meantime
*/
(void) fstat(acctfd, &before);
if ( fork() == 0 ) /* create a child and */
exit(0); /* let the child finish */
(void) wait((int *) 0); /* let the parent wait */
(void) fstat(acctfd, &after);
if (after.st_size > before.st_size)
{
/*
** remove the directory and file
*/
regainrootprivs(); /* get root privs again */
(void) acct(0);
(void) unlink(ACCTDIR "/" ACCTFILE);
(void) rmdir(ACCTDIR);
if (! droprootprivs() )
mcleanstop(42,
"failed to drop root privs\n");
}
}
(void) semop(sematopid, &semrelse, 1);
}
/*
** anyhow close the accounting-file again
*/
(void) close(acctfd); /* close account file again */
acctfd = -1;
supportflags &= ~ACCTACTIVE;
}
/*
** get the number of exited processes written
** in the process-account file since the previous sample
*/
unsigned long
acctprocnt(void)
{
struct stat statacc;
/*
** if accounting not supported, skip call
*/
if (acctfd == -1)
return 0;
/*
** determine the current size of the accounting file
*/
if (fstat(acctfd, &statacc) == -1)
return 0;
/*
** handle atopacctd-based process accounting on bases of
** fixed-chunk shadow files
*/
if (maxshadowrec)
{
unsigned long numrecs = 0;
long newseq;
char shadowpath[128];
FILE *cfp;
/*
** verify how many new processes are added to the current
** shadow file
*/
numrecs = (statacc.st_size - acctsize) / acctrecsz;
/*
** verify if subsequent shadow files are involved
** (i.e. if the current file is full)
*/
if (statacc.st_size / acctrecsz < maxshadowrec)
return numrecs;
/*
** more shadow files available
** get current shadow file
*/
snprintf(shadowpath, sizeof shadowpath, "%s/%s/%s",
pacctdir, PACCTSHADOWD, PACCTSHADOWC);
if ( (cfp = fopen(shadowpath, "r")) )
{
if (fscanf(cfp, "%ld", &newseq) == 1)
{
fclose(cfp);
}
else
{
fclose(cfp);
return numrecs;
}
}
else
{
return numrecs;
}
if (newseq == curshadowseq)
return numrecs;
snprintf(shadowpath, sizeof shadowpath, PACCTSHADOWF,
pacctdir, PACCTSHADOWD, newseq);
/*
** determine the size of newest shadow file
*/
if (stat(shadowpath, &statacc) == -1)
return numrecs;
numrecs += ((newseq - curshadowseq - 1) * maxshadowrec) +
(statacc.st_size / acctrecsz);
return numrecs;
}
else
/*
** classic process accounting on bases of directly opened
** process accounting file
*/
{
/*
** accounting reset?
*/
if (acctsize > statacc.st_size)
{
/*
** reposition to start of file
*/
(void) lseek(acctfd, 0, SEEK_SET);
acctsize = 0;
}
/*
** using account file managed by (ps)acct package?
*/
if (pacctcur)
{
/*
** check inode of the current file and compare this
** with the inode of the opened file; if not equal,
** a file rotation has taken place and the size of
** the new file has to be added
*/
if ( stat(pacctcur->name, &(pacctcur->stat)) == 0)
{
if (statacc.st_ino != pacctcur->stat.st_ino)
{
return (statacc.st_size - acctsize +
pacctcur->stat.st_size) /
acctrecsz;
}
}
}
return (statacc.st_size - acctsize) / acctrecsz;
}
}
/*
** reposition the seek offset in the process accounting file to skip
** processes that have not been read
*/
void
acctrepos(unsigned int noverflow)
{
/*
** if accounting not supported, skip call
*/
if (acctfd == -1)
return;
if (maxshadowrec)
{
int i;
off_t virtoffset = acctsize + noverflow * acctrecsz;
off_t maxshadowsz = maxshadowrec * acctrecsz;
long switches = virtoffset / maxshadowsz;
acctsize = virtoffset % maxshadowsz;
for (i=0; i < switches; i++)
switchshadow();
(void) lseek(acctfd, acctsize, SEEK_SET);
}
else
{
/*
** just reposition to skip superfluous records
*/
(void) lseek(acctfd, noverflow * acctrecsz, SEEK_CUR);
acctsize += noverflow * acctrecsz;
/*
** when the new seek pointer is beyond the current file size
** and reading from a process accounting file written by
** the (ps)acct package, a logrotation might have taken place
*/
if (pacctcur)
{
struct stat statacc;
/*
** determine the size of the current accounting file
*/
if (fstat(acctfd, &statacc) == -1)
return;
/*
** seek pointer beyond file size and rotated to
** new account file?
*/
if (acctsize > statacc.st_size &&
statacc.st_ino != pacctcur->stat.st_ino)
{
/*
** - close old file
** - open new file
** - adapt acctsize to actual offset in new file
** and seek to that offset
*/
(void) close(acctfd);
if ( (acctfd = open(pacctcur->name,
O_RDONLY) ) == -1)
return; // open failed
acctsize = acctsize - statacc.st_size;
(void) lseek(acctfd, acctsize, SEEK_SET);
if (fstat(acctfd, &statacc) == -1)
return; // no new inode info
}
}
}
}
/*
** read the process records from the process accounting file,
** that are written since the previous cycle
*/
unsigned long
acctphotoproc(struct tstat *accproc, int nrprocs)
{
register int nrexit;
register struct tstat *api;
struct acct acctrec;
struct acct_v3 acctrec_v3;
struct stat statacc;
/*
** if accounting not supported, skip call
*/
if (acctfd == -1)
return 0;
/*
** determine the size of the (current) account file
*/
if (fstat(acctfd, &statacc) == -1)
return 0;
/*
** check all exited processes in accounting file
*/
for (nrexit=0, api=accproc; nrexit < nrprocs;
nrexit++, api++, acctsize += acctrecsz)
{
/*
** in case of shadow accounting files, we might have to
** switch from the current accounting file to the next
*/
if (maxshadowrec && acctsize >= statacc.st_size)
{
switchshadow();
/*
** determine the size of the new (current) account file
** and initialize the current offset within that file
*/
if (fstat(acctfd, &statacc) == -1)
return 0;
acctsize = 0;
}
/*
** in case of account file managed by (ps)acct package,
** be aware that a switch to a newer logfile might have
** to be done
*/
if (pacctcur && acctsize >= statacc.st_size)
{
/*
** check inode of the current file and compare this
** with the inode of the opened file; if not equal,
** a file rotation has taken place and the file
** has to be reopened
*/
if ( stat(pacctcur->name, &(pacctcur->stat)) == 0)
{
if (statacc.st_ino != pacctcur->stat.st_ino)
{
(void) close(acctfd);
if ( (acctfd = open(pacctcur->name,
O_RDONLY) ) == -1)
return 0; // open failed
if (fstat(acctfd, &statacc) == -1)
return 0; // no new inode info
acctsize = 0; // reset size new file
}
}
}
/*
** read the next record
*/
switch (acctversion)
{
case 2:
if ( read(acctfd, &acctrec, acctrecsz) < acctrecsz )
break; /* unexpected end of account file */
/*
** fill process info from accounting-record
*/
api->gen.state = 'E';
api->gen.nthr = 1;
api->gen.isproc = 1;
api->gen.pid = 0;
api->gen.tgid = 0;
api->gen.ppid = 0;
api->gen.excode = acctrec.ac_exitcode;
api->gen.ruid = acctrec.ac_uid16;
api->gen.rgid = acctrec.ac_gid16;
api->gen.btime = acctrec.ac_btime;
api->gen.elaps = acctrec.ac_etime;
api->cpu.stime = acctexp(acctrec.ac_stime);
api->cpu.utime = acctexp(acctrec.ac_utime);
api->mem.minflt = acctexp(acctrec.ac_minflt);
api->mem.majflt = acctexp(acctrec.ac_majflt);
api->dsk.rio = acctexp(acctrec.ac_rw);
strncpy(api->gen.name, acctrec.ac_comm, PNAMLEN);
api->gen.name[PNAMLEN] = '\0';
break;
case 3:
if ( read(acctfd, &acctrec_v3, acctrecsz) < acctrecsz )
break; /* unexpected end of account file */
/*
** fill process info from accounting-record
*/
api->gen.state = 'E';
api->gen.pid = acctrec_v3.ac_pid;
api->gen.tgid = acctrec_v3.ac_pid;
api->gen.ppid = acctrec_v3.ac_ppid;
api->gen.nthr = 1;
api->gen.isproc = 1;
api->gen.excode = acctrec_v3.ac_exitcode;
api->gen.ruid = acctrec_v3.ac_uid;
api->gen.rgid = acctrec_v3.ac_gid;
api->gen.btime = acctrec_v3.ac_btime;
api->gen.elaps = acctrec_v3.ac_etime;
api->cpu.stime = acctexp(acctrec_v3.ac_stime);
api->cpu.utime = acctexp(acctrec_v3.ac_utime);
api->mem.minflt = acctexp(acctrec_v3.ac_minflt);
api->mem.majflt = acctexp(acctrec_v3.ac_majflt);
api->dsk.rio = acctexp(acctrec_v3.ac_rw);
strncpy(api->gen.name, acctrec_v3.ac_comm, PNAMLEN);
api->gen.name[PNAMLEN] = '\0';
break;
}
}
if (acctsize > ACCTMAXFILESZ && !maxshadowrec)
acctrestarttrial();
return nrexit;
}
/*
** when the size of the private accounting file exceeds a certain limit,
** it might be useful to stop process accounting, truncate the
** process accounting file to zero and start process accounting again
**
** this will only be done if this atop process is the only one
** that is currently using the accounting file
*/
static void
acctrestarttrial()
{
struct stat statacc;
int sematopid;
/*
** not private accounting-file in use?
*/
if (!acctatop)
return; // do not restart
/*
** still remaining records in accounting file that are
** written between the moment that the number of exited
** processes was counted and the moment that all processes
** were read
*/
(void) fstat(acctfd, &statacc);
if (acctsize != statacc.st_size)
return; // do not restart
/*
** claim the semaphore to get exclusive rights for
** the accounting-manipulation
*/
sematopid = semget(ATOPACCTKEY, 0, 0);
(void) semop(sematopid, &semclaim, 1);
/*
** check if there are more users of accounting file
*/
if (semctl(sematopid, 1, GETVAL, 0) < ATOPACCTTOT-1)
{
(void) semop(sematopid, &semrelse, 1);
return; // do not restart
}
/*
** restart is possible
**
** - switch off accounting
** - truncate the file
** - switch on accounting
*/
regainrootprivs(); // get root privs again
(void) acct(0); // switch off accounting
if ( truncate(ACCTDIR "/" ACCTFILE, 0) == 0)
(void) lseek(acctfd, 0, SEEK_SET);
(void) acct(ACCTDIR "/" ACCTFILE);
if (! droprootprivs() )
mcleanstop(42, "failed to drop root privs\n");
acctsize = 0;
(void) semop(sematopid, &semrelse, 1);
}
/*
** expand the special compression-methods
*/
static count_t
acctexp(comp_t ct)
{
count_t exp;
count_t val;
exp = (ct >> 13) & 0x7; /* obtain 3 bits base-8 exponent */
val = ct & 0x1fff; /* obtain 13 bits mantissa */
while (exp-- > 0)
val <<= 3;
return val;
}
/*
** switch to the next accounting shadow file
*/
static void
switchshadow(void)
{
int tmpfd;
char shadowpath[128];
struct flock flock;
/*
** determine path name of new shadow file
*/
curshadowseq++;
snprintf(shadowpath, sizeof shadowpath, PACCTSHADOWF,
pacctdir, PACCTSHADOWD, curshadowseq);
/*
** open new shadow file, while the previous is also kept open
** (to keep the read lock until a read lock is set for the new
** shadow file)
*/
if ( (tmpfd = open(shadowpath, O_RDONLY)) != -1)
{
/*
** set read lock on new shadow file
*/
flock.l_type = F_RDLCK;
flock.l_whence = SEEK_SET;
flock.l_start = 0;
flock.l_len = 1;
if ( fcntl(tmpfd, F_SETLK, &flock) != -1)
{
(void) close(acctfd); // implicit release read lock
acctfd = tmpfd;
return;
}
else
{
(void) close(tmpfd);
}
}
}
/*
** handle the option 'pacctdir' in the /etc/atoprc file
*/
void
do_pacctdir(char *tagname, char *tagvalue)
{
char shadowpath[128];
struct stat dirstat;
/*
** copy the directory pathname to an own buffer
*/
if ( (pacctdir = malloc(strlen(tagvalue)+1)) == NULL )
{
perror("malloc pacctdir");
exit(11);
}
strcpy(pacctdir, tagvalue);
/*
** verify if the atopacctd daemon is active
*/
if ( semget(PACCTPUBKEY, 0, 0) == -1)
{
fprintf(stderr, "Warning: option '%s' specified "
"while atopacctd not running!\n", tagname);
sleep(2);
return;
}
/*
** verify that the topdirectory exists
*/
if ( stat(pacctdir, &dirstat) == -1 )
{
fprintf(stderr, "Warning: option '%s' specified - ", tagname);
perror(pacctdir);
sleep(2);
return;
}
if (! S_ISDIR(dirstat.st_mode) )
{
fprintf(stderr, "Warning: option '%s' specified - ", tagname);
fprintf(stderr, "%s not a directory\n", pacctdir);
sleep(2);
return;
}
/*
** verify that the topdirectory contains the required subdirectory
*/
snprintf(shadowpath, sizeof shadowpath, "%s/%s",
pacctdir, PACCTSHADOWD);
if ( stat(shadowpath, &dirstat) == -1 )
{
fprintf(stderr, "Warning: option '%s' specified - ", tagname);
perror(shadowpath);
sleep(2);
return;
}
if (! S_ISDIR(dirstat.st_mode) )
{
fprintf(stderr, "Warning: option '%s' specified - ", tagname);
fprintf(stderr, "%s not a directory\n", shadowpath);
sleep(2);
return;
}
}
|