Cover V03, I06
Article
Figure 1
Figure 2
Listing 1
Listing 2
Listing 3
Listing 4

nov94.tar


Listing 3: kill.c

/*
* kill.c -- C version of kill logging script
* Copyright 1994 by Steven G. Isaacson
*/

#include <stdio.h>
#include <ctype.h>
#include <sys/types.h>
#include <time.h>

/* log file name */
char *logname="/usr/spool/log/kill.log";

main(argc, argv)
int argc;
char *argv[];
{
FILE *fp;
int i, pid;
char *cwd;
time_t now;

/*
* check for error opening the log file, but don't
* stop people from working just because there is a
* problem with the log file.
*/

if ((fp=fopen(logname, "a+")) != NULL ) {

/* log who is doing what where */

fprintf(fp, "\n" );
if (time(&now) == -1)
fprintf(fp,"time not available\n");
else
fprintf(fp,"%s", ctime(&now));

fprintf(fp,
"getlogin=%s uid=%d euid=%d gid=%d egid=%d
cuserid=%s\n",
getlogin(), getuid(), geteuid(), getgid(), getegid(),
cuserid(NULL));

fprintf(fp,"%s=%s ", "HOME", getenv("HOME"));
fprintf(fp,"%s=%s\n", "NAME", getenv("NAME"));
fprintf(fp, "cwd=%s\n", getcwd((char *)NULL, 64));

for (i=0; i < argc; i++)
fprintf(fp, "%s ", argv[i]);
fprintf(fp, "\n");

for ( i=1; i < argc; i++ ) {

pid=atoi(argv[i]);

/* if kill level (-9,-15, etc.) was specified,
* skip it
*/
if ( pid < 0 )
continue;

plog(fp, pid);
}
fflush(fp);
}

/* now call the real kill program */
execvp("/bin/rkill", argv);
}

/* ================================================= */
/* write pid info to log file using a pipe           */

plog(fp, pid)
FILE *fp;
int pid;
{
char cmd[50], buf[256];
FILE *ptr;

sprintf(cmd, "/bin/ps -lfp %d", pid);

if ((ptr = popen(cmd, "r")) != NULL)
while (fgets(buf, sizeof(buf), ptr) != NULL)
(void) fprintf(fp, "%s ",buf);
pclose(ptr);
return;
}

/* ================================================= */