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
|
/*
** 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 - AT Computing, Nijmegen, Holland
** E-mail: gerlof@ATComputing.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.
**
** $Log: acctproc.c,v $
** Revision 1.26 2008/03/06 08:37:25 gerlof
** Register/show ppid of a process.
**
** Revision 1.25 2008/01/14 09:22:23 gerlof
** Support for environment variable ATOPACCT to specify the name of a
** particular process accouting file.
**
** Revision 1.24 2007/08/17 08:50:26 gerlof
** Verify if private accounting used before switching off accounting.
**
** Revision 1.23 2007/03/20 13:01:51 gerlof
** Introduction of variable supportflags.
**
** Revision 1.22 2007/02/13 09:14:49 gerlof
** New boolean introduced to indicate if accounting is active.
**
** Revision 1.21 2006/02/07 06:46:15 gerlof
** Removed swap-counter.
**
** Revision 1.20 2005/11/04 14:17:10 gerlof
** Improved recognition of certain version process accounting file.
**
** Revision 1.19 2005/10/31 12:44:58 gerlof
** Support account-record version 3 (used by Mandriva).
**
** Revision 1.18 2005/10/31 08:55:05 root
** *** empty log message ***
**
** Revision 1.17 2004/12/14 15:05:00 gerlof
** Implementation of patch-recognition for disk and network-statistics.
**
** Revision 1.16 2004/06/01 11:57:22 gerlof
** Consider other standard accounting-files, i.e. /var/account/pacct.
**
** Revision 1.15 2004/05/06 09:48:21 gerlof
** Ported to kernel-version 2.6.
**
** Revision 1.14 2003/07/07 09:26:15 gerlof
** Cleanup code (-Wall proof).
**
** Revision 1.13 2003/07/02 06:43:11 gerlof
** Modified include-file sys/acct.h to linux/acct.h to make it
** work on Alpha-based systems as well.
**
** Revision 1.12 2003/06/27 12:31:24 gerlof
** Adapt long to long long.
**
** Revision 1.11 2003/04/03 08:32:58 gerlof
** Cosmetic changes.
**
** Revision 1.10 2003/01/14 09:01:45 gerlof
** Small cosmetic changes.
**
** Revision 1.9 2002/10/03 11:12:03 gerlof
** Modify (effective) uid/gid to real uid/gid.
**
** Revision 1.8 2002/07/24 11:11:12 gerlof
** Redesigned to ease porting to other UNIX-platforms.
**
** Revision 1.7 2002/07/11 07:28:29 root
** Some additions related to secure accounting file handling
**
** Revision 1.6 2002/07/08 09:14:54 root
** Modified secure handling of accounting file
** (inspired by Tobias Rittweiler).
**
** Revision 1.5 2001/11/07 09:16:55 gerlof
** Allow users to run atop without process-accounting switched on.
**
** Revision 1.4 2001/10/16 06:15:52 gerlof
** Partly redesigned.
**
** Revision 1.3 2001/10/04 13:02:24 gerlof
** Redesign of the way to determine how many atop's are using process-accounting
** on basis of semaphores.
**
** Revision 1.2 2001/10/04 08:46:46 gerlof
** Improved verification of kernel-symbol addresses
**
** Revision 1.1 2001/10/02 10:38:35 gerlof
** Initial revision
**
*/
static const char rcsid[] = "$Id: acctproc.c,v 1.26 2008/03/06 08:37:25 gerlof Exp $";
#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/ipc.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include "atop.h"
#include "photoproc.h"
#include "acctproc.h"
#define ACCTDIR "/var/run/atop"
#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 */
static count_t acctexp (comp_t ct);
static count_t acctexp2(comp2_t ct);
static int acctvers(int);
struct pacctadm {
char *name;
struct stat stat;
} pacctadm[] = {
{ "/var/log/account/pacct", {0, }, },
{ "/var/log/pacct", {0, }, },
{ "/var/account/pacct", {0, }, }
};
/*
** 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 SEMAKEY 3121959
#define SEMTOTAL 100
struct sembuf semclaim = {0, -1, SEM_UNDO},
semrelse = {0, +1, SEM_UNDO},
semdecre = {1, -1, SEM_UNDO},
semincre = {1, +1, SEM_UNDO};
/*
** switch on the process-accounting mechanism
*/
int
acctswon(void)
{
int i, j, semid;
static ushort vals[] = {1, SEMTOTAL};
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
*/
seteuid( getuid() ); /* drop setuid-root privs */
if ( (acctfd = open(ep, O_RDONLY) ) == -1)
{
perror("open account-file");
fprintf(stderr,
"warning: no process exit detection\n");
sleep(3);
return 0;
}
if ( !acctvers(acctfd) )
{
fprintf(stderr,
"warning: no process exit detection\n");
sleep(3);
(void) close(acctfd);
return 0;
}
supportflags |= ACCTACTIVE;
return 1;
}
else
{
/*
** no contents
*/
return 0;
}
}
/*
** 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)
{
perror("open account-file");
fprintf(stderr,
"warning: no process "
"exit detection\n");
sleep(3);
return 0;
}
if ( !acctvers(acctfd) )
{
fprintf(stderr,
"warning: no process "
"exit detection\n");
sleep(3);
(void) close(acctfd);
return 0;
}
supportflags |= ACCTACTIVE;
return 1;
}
}
}
}
/*
** 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 exclusively; if this succeeds,
** this is the first atop-incarnation since boot and the semaphore
** should be initialized
*/
if ( (semid = semget(SEMAKEY, 2, 0666|IPC_CREAT|IPC_EXCL)) >= 0)
(void) semctl(semid, 0, SETALL, arg);
else
semid = semget(SEMAKEY, 0, 0);
/*
** check if we got access to the semaphore-group
*/
if (semid == -1)
{
perror("open semaphore-group");
fprintf(stderr, "warning: no process exit detection\n");
sleep(3);
return 0;
}
/*
** the semaphore-group is opened now; claim exclusive rights
*/
(void) semop(semid, &semclaim, 1);
/*
** are we the first to use the accounting-mechanism ?
*/
if (semctl(semid, 1, GETVAL, 0) == SEMTOTAL)
{
/*
** create a new separate directory below /var/run
** 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
*/
fprintf(stderr,
"warning: no process exit detection "
"(can not create directory %s)\n",
ACCTDIR);
(void) semop(semid, &semrelse, 1);
sleep(3);
return 0;
}
}
/*
** create accounting file in new directory
*/
(void) close( creat(ACCTDIR "/" ACCTFILE, 0600) );
/*
** switch on accounting
*/
if ( acct(ACCTDIR "/" ACCTFILE) < 0)
{
perror("activate process accounting");
fprintf(stderr, "warning: no process exit detection");
if (errno == EPERM)
fprintf(stderr, " (not superuser)!\n");
else
fprintf(stderr, "!\n");
(void) unlink(ACCTDIR "/" ACCTFILE);
(void) rmdir(ACCTDIR);
(void) semop(semid, &semrelse, 1);
sleep(3);
return 0;
}
}
/*
** accounting is switched on now;
** open accounting-file
*/
if ( (acctfd = open(ACCTDIR "/" ACCTFILE, O_RDONLY) ) < 0)
{
perror("open account-file");
fprintf(stderr, "warning: no process exit detection!\n");
(void) acct(0);
(void) unlink(ACCTDIR "/" ACCTFILE);
(void) rmdir(ACCTDIR);
(void) semop(semid, &semrelse, 1);
sleep(3);
return 0;
}
/*
** accounting successfully switched on
*/
(void) semop(semid, &semdecre, 1);
(void) semop(semid, &semrelse, 1);
acctatop = 1;
/*
** determine version of accounting-record
*/
(void) fstat(acctfd, &statbuf);
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 */
}
acctvers(acctfd);
supportflags |= ACCTACTIVE;
return 1;
}
/*
** 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 accouting file to verify
** the second byte (always contains version number)
*/
if ( read(fd, &tmprec, sizeof tmprec) < sizeof tmprec)
return 0;
switch (tmprec.ac_version)
{
case 3:
acctrecsz = sizeof(struct acct_v3);
acctversion = 3;
break;
case 6:
acctrecsz = sizeof(struct acct_atop);
acctversion = 6;
supportflags |= PATCHACCT;
break;
default:
acctrecsz = sizeof(struct acct);
acctversion = 2;
}
/*
** 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 semid;
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
*/
semid = semget(SEMAKEY, 0, 0);
(void) semop(semid, &semclaim, 1);
(void) semop(semid, &semincre, 1);
/*
** check if we were the last user of accounting
*/
if (semctl(semid, 1, GETVAL, 0) == SEMTOTAL)
{
/*
** 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
*/
seteuid(0); /* get root privs again */
(void) acct(0);
(void) unlink(ACCTDIR "/" ACCTFILE);
(void) rmdir(ACCTDIR);
}
}
(void) semop(semid, &semrelse, 1);
}
/*
** anyhow close the accounting-file again
*/
(void) close(acctfd); /* close account file again */
supportflags &= ~ACCTACTIVE;
}
/*
** get the number of exited processes written
** in the process-account file since the previous sample
*/
int
acctprocnt(void)
{
struct stat statacc;
/*
** if accounting not supported, skip call
*/
if (acctfd == -1)
return 0;
/*
** determine how many processes exited last interval
*/
(void) fstat(acctfd, &statacc);
if (acctsize > statacc.st_size) /* somebody did reset accounting? */
{
/*
** reposition to start of file
*/
lseek(acctfd, 0, SEEK_SET);
acctsize = 0;
}
return (statacc.st_size - acctsize) / acctrecsz;
}
/*
** read process-records from the account-file,
** which are written since the previous cycle
*/
int
acctphotoproc(struct pstat *accproc, int nrprocs)
{
register int nrexit;
register struct pstat *api;
struct acct acctrec;
struct acct_v3 acctrec_v3;
struct acct_atop acctrec_atop;
/*
** if accounting not supported, skip call
*/
if (acctfd == -1)
return 0;
/*
** check all exited processes in accounting file
*/
for (nrexit=0, api=accproc; nrexit < nrprocs; nrexit++, api++)
{
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.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->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);
strcpy(api->gen.name, acctrec.ac_comm);
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.ppid = acctrec_v3.ac_ppid;
api->gen.nthr = 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->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);
strcpy(api->gen.name, acctrec_v3.ac_comm);
break;
case 6:
if ( read(acctfd, &acctrec_atop, acctrecsz) <acctrecsz)
break; /* unexpected end of account file */
/*
** fill process info from accounting-record
*/
api->gen.state = 'E';
api->gen.pid = acctrec_atop.ac_pid;
api->gen.ppid = acctrec_atop.ac_ppid;
api->gen.nthr = 1;
api->gen.excode = acctrec_atop.ac_exitcode;
api->gen.ruid = acctrec_atop.ac_uid;
api->gen.rgid = acctrec_atop.ac_gid;
api->gen.btime = acctrec_atop.ac_btime;
api->cpu.stime = acctexp (acctrec_atop.ac_stime);
api->cpu.utime = acctexp (acctrec_atop.ac_utime);
api->mem.minflt = acctexp (acctrec_atop.ac_minflt);
api->mem.majflt = acctexp (acctrec_atop.ac_majflt);
api->mem.vmem = acctexp (acctrec_atop.ac_mem);
api->mem.rmem = acctexp (acctrec_atop.ac_rss);
api->dsk.rio = acctexp (acctrec_atop.ac_bread);
api->dsk.wio = acctexp (acctrec_atop.ac_bwrite);
api->dsk.rsz = acctexp2(acctrec_atop.ac_dskrsz);
api->dsk.wsz = acctexp2(acctrec_atop.ac_dskwsz);
api->net.tcpsnd = acctexp (acctrec_atop.ac_tcpsnd);
api->net.tcprcv = acctexp (acctrec_atop.ac_tcprcv);
api->net.tcpssz = acctexp2(acctrec_atop.ac_tcpssz);
api->net.tcprsz = acctexp2(acctrec_atop.ac_tcprsz);
api->net.udpsnd = acctexp (acctrec_atop.ac_udpsnd);
api->net.udprcv = acctexp (acctrec_atop.ac_udprcv);
api->net.udpssz = acctexp2(acctrec_atop.ac_udpssz);
api->net.udprsz = acctexp2(acctrec_atop.ac_udprsz);
api->net.rawsnd = acctexp (acctrec_atop.ac_rawsnd);
api->net.rawrcv = acctexp (acctrec_atop.ac_rawrcv);
strcpy(api->gen.name, acctrec_atop.ac_comm);
}
}
acctsize += nrexit * acctrecsz;
return nrexit;
}
/*
** 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;
}
static count_t
acctexp2(comp2_t ct)
{
count_t exp;
count_t val;
exp = (ct >> 20) & 0xfff; /* obtain 5 bits base-2 exponent */
val = ct & 0xfffff; /* obtain 20 bits mantissa */
while (exp-- > 0)
val <<= 2;
return val;
}
|