home *** CD-ROM | disk | FTP | other *** search
- /* uulocks - show status of UUCP ("modem") locks
- * vixie 30jun93 [original]
- *
- * $Id: uulocks.c,v 1.1 1993/07/01 20:17:02 vixie Exp $
- */
-
- #include <sys/param.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <dirent.h>
- #include <stdio.h>
- #include <errno.h>
- #include <string.h>
-
- static char LckPre[] = "LCK..";
- static int LPLen = (sizeof LckPre) - 1;
- static int BadExit = 1, GoodExit = 0;
- static int PS = 0, Fix = 0, NoHdr = 0;
- #if (BSD >= 199103)
- static char *PScmd = "ps %s -p %d";
- static char *PSarg = "-o user,tt,ucomm,state,start";
- #else
- static char *PScmd = "ps %s%d";
- static char *PSarg = "u";
- #endif
- static char AsciiBS = 0x08;
- static time_t Now;
-
- main(argc, argv)
- int argc;
- char *argv[];
- {
- time(&Now);
- args(argc, argv);
- do_hdr();
- do_dir("/var/spool/uucp");
- do_dir("/var/run");
- exit(GoodExit);
- }
-
- usage() {
- fprintf(stderr, "usage: uulocks [-h] [-p] [-a psargs] [-f]\n");
- exit(BadExit);
- }
-
- args(argc, argv)
- int argc;
- char *argv[];
- {
- extern char *optarg;
- char ch;
-
- while ((ch = getopt(argc, argv, "hpa:f")) != EOF) {
- switch (ch) {
- case 'h': NoHdr++; break;
- case 'p': PS++; break;
- case 'a': PSarg = optarg; break;
- case 'f': Fix++; break;
- default: usage(); break;
- }
- }
- }
-
- do_hdr() {
- if (!NoHdr) {
- fputs("\
- Resource PID Lock Age PStat\
- " , stdout);
- if (PS) {
- fputs( " (PS Result)", stdout);
- }
- /*
- ttya1 23321 001+00:35:51 alive (root 23321 ?? getty IE Wed12PM)
- */
- putchar('\n');
- }
- }
-
- do_dir(path)
- char *path;
- {
- DIR *dir;
- struct dirent *dp;
-
- if (chdir(path) < 0) {
- perror(path);
- exit(BadExit);
- }
- if (!(dir = opendir("."))) {
- perror(path);
- exit(BadExit);
- }
- while (dp = readdir(dir)) {
- FILE *lck;
- pid_t pid;
- struct stat sb;
- time_t age;
- char timbuf[(sizeof "000+00:00:00")];
- int j;
-
- if ((dp->d_namlen < LPLen)
- || strncmp("LCK..", dp->d_name, LPLen))
- continue;
- if (!(lck = fopen(dp->d_name, "r"))) {
- perror(dp->d_name);
- continue;
- }
- if (fstat(fileno(lck), &sb)) {
- perror(dp->d_name);
- continue;
- }
- pid = (pid_t) getw(lck);
- fclose(lck);
- age = Now - sb.st_mtime;
- strftime(timbuf, sizeof timbuf, "%j+%H:%M:%S", gmtime(&age));
- sprintf(timbuf, "%03d", atoi(timbuf)-1);
- timbuf[3] = '+'; /* ugly */
- if (kill(pid, 0) >= 0)
- j = 1;
- else
- j = (errno != ESRCH);
- printf("%-12s%10d %s %-5s",
- dp->d_name+LPLen, pid, timbuf,
- j ?"alive" :"dead");
- if (Fix && !j) {
- printf(" [unlink: %s]",
- (unlink(dp->d_name)>=0) ?"ok" :strerror(errno));
- }
- if (PS && j) {
- char cmdbuf[100]; /* ugly */
- FILE *ps;
-
- sprintf(cmdbuf, PScmd, PSarg, pid);
- if (!(ps = popen(cmdbuf, "r"))) {
- printf(" [ps: %s]", strerror(errno));
- } else {
- char ch, last_ch;
-
- /* eat header */
- while (((ch = getc(ps)) != '\n')
- && (ch != EOF)
- )
- ;
- /* get first line, compress multiple blanks */
- if ((last_ch = ch) != EOF) {
- fputs(" (", stdout);
- while (((ch = getc(ps)) != '\n')
- && (ch != EOF)
- ) {
- if (ch == ' '
- && last_ch == ' '
- ) {
- continue;
- }
- putchar(ch);
- last_ch = ch;
- }
- if (last_ch == ' ') {
- putchar(AsciiBS); /* ugly */
- }
- putchar(')');
- }
- /* run it out */
- while (ch != EOF) {
- last_ch = ch;
- ch = getc(ps);
- }
- }
- pclose(ps);
- }
- putchar('\n');
- }
- closedir(dir);
- }
-