home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1994 March / Source_Code_CD-ROM_Walnut_Creek_March_1994.iso / compsrcs / unix / volume26 / uulocks / part01 < prev    next >
Encoding:
Text File  |  1993-06-30  |  10.5 KB  |  399 lines

  1. Newsgroups: comp.sources.unix
  2. From: paul@vix.com (Paul Vixie)
  3. Subject: v26i277: uulocks - print status summary of UUCP locks, Part01/01
  4. Sender: unix-sources-moderator@gw.home.vix.com
  5. Approved: vixie@gw.home.vix.com
  6.  
  7. Submitted-By: paul@vix.com (Paul Vixie)
  8. Posting-Number: Volume 26, Issue 277
  9. Archive-Name: uulocks/part01
  10.  
  11. NAME
  12.        uulocks - print status summary of UUCP locks
  13.  
  14. SYNOPSIS
  15.        uulocks [ -h ] [ -p [ -a psargs ] ]
  16.  
  17. DESCRIPTION
  18.        Uulocks searches the various directories which can contain
  19.        UUCP-style ``lock'' files.  These files are used  by  UUCP
  20.        to  prevent multiple connections to the same remote neigh-
  21.        bor, and also to control access to  the  outgoing  modems.
  22.        On  some systems, tip(1), kermit(1), and even getty(8) use
  23.        these same locks, thus permitting the outbound  modems  to
  24.        be shared between all modem applications.
  25.  
  26.     Paul Vixie <paul@vix.com>
  27.  
  28. #! /bin/sh
  29. # This is a shell archive.  Remove anything before this line, then unpack
  30. # it by saving it into a file and typing "sh file".  To overwrite existing
  31. # files, type "sh file -c".  You can also feed this as standard input via
  32. # unshar, or by typing "sh <file", e.g..  If this archive is complete, you
  33. # will see the following message at the end:
  34. #        "End of shell archive."
  35. # Contents:  README Makefile uulocks.1 uulocks.c
  36. # Wrapped by vixie@gw.home.vix.com on Thu Jul  1 13:17:24 1993
  37. PATH=/bin:/usr/bin:/usr/ucb ; export PATH
  38. if test -f 'README' -a "${1}" != "-c" ; then 
  39.   echo shar: Will not clobber existing file \"'README'\"
  40. else
  41. echo shar: Extracting \"'README'\" \(1376 characters\)
  42. sed "s/^X//" >'README' <<'END_OF_FILE'
  43. XUULOCKS(1)                                             UULOCKS(1)
  44. X
  45. XNAME
  46. X       uulocks - print status summary of UUCP locks
  47. X
  48. XSYNOPSIS
  49. X       uulocks [ -h ] [ -p [ -a psargs ] ]
  50. X
  51. XDESCRIPTION
  52. X       Uulocks searches the various directories which can contain
  53. X       UUCP-style ``lock'' files.  These files are used  by  UUCP
  54. X       to  prevent multiple connections to the same remote neigh-
  55. X       bor, and also to control access to  the  outgoing  modems.
  56. X       On  some systems, tip(1), kermit(1), and even getty(8) use
  57. X       these same locks, thus permitting the outbound  modems  to
  58. X       be shared between all modem applications.
  59. X
  60. X       If  the  -h option is given, no header will be printed for
  61. X       the report.  This is useful if you intend to  post-process
  62. X       the output with perl(1) or awk(1).
  63. X
  64. X       If  the  -p  option is given, then a ps(1) command will be
  65. X       executed for each process seen to  have  an  active  lock.
  66. X       This can be followed by the -a option, which specifies the
  67. X       output format to be given to ps(1).  The default for newer
  68. X       systems  is  -o  user,tt,ucomm,state,start,  and for older
  69. X       systems it is simply u.
  70. X
  71. XSEE ALSO
  72. X       ps(1), tip(1), kermit(1), getty(8)
  73. X
  74. XFILES
  75. X       /var/spool/uucp/LCK..*
  76. X       /var/run/LCK..*
  77. X
  78. XAUTHOR
  79. X       Paul Vixie <paul@vix.com>
  80. X
  81. X$Id: README,v 1.1 1993/07/01 20:17:02 vixie Exp $
  82. END_OF_FILE
  83. if test 1376 -ne `wc -c <'README'`; then
  84.     echo shar: \"'README'\" unpacked with wrong size!
  85. fi
  86. # end of 'README'
  87. fi
  88. if test -f 'Makefile' -a "${1}" != "-c" ; then 
  89.   echo shar: Will not clobber existing file \"'Makefile'\"
  90. else
  91. echo shar: Extracting \"'Makefile'\" \(649 characters\)
  92. sed "s/^X//" >'Makefile' <<'END_OF_FILE'
  93. X# $Id: Makefile,v 1.1 1993/07/01 20:17:02 vixie Exp $
  94. X
  95. XDESTROOT =
  96. XBINDIR = /usr/local/bin
  97. XCATDIR = /usr/share/man/cat1
  98. X
  99. XCC = cc
  100. XCDEBUG = -g
  101. XCFLAGS = $(CDEBUG)
  102. XLDFLAGS =
  103. XMANROFF = nroff -man
  104. XSHAR = cshar
  105. X
  106. XSOURCES = README Makefile uulocks.1 uulocks.c
  107. XALL = uulocks uulocks.0
  108. X
  109. Xall: $(ALL)
  110. X
  111. Xuulocks: uulocks.o Makefile
  112. X    $(CC) $(CDEBUG) $(LDFLAGS) -o $@ uulocks.o
  113. X
  114. Xuulocks.0: uulocks.1 Makefile
  115. X    $(MANROFF) <uulocks.1 >$@
  116. X
  117. Xclean:
  118. X    rm -f $(ALL) kit
  119. X    rm -f *.o *~ *.BAK *.CKP core
  120. X
  121. Xinstall: $(ALL)
  122. X    install -c -m 555 uulocks $(DESTROOT)$(BINDIR)/uulocks
  123. X    install -c -m 444 uulocks.0 $(DESTROOT)$(CATDIR)/uulocks.0
  124. X
  125. Xkit: $(SOURCES)
  126. X    $(SHAR) $(SOURCES) >$@
  127. END_OF_FILE
  128. if test 649 -ne `wc -c <'Makefile'`; then
  129.     echo shar: \"'Makefile'\" unpacked with wrong size!
  130. fi
  131. # end of 'Makefile'
  132. fi
  133. if test -f 'uulocks.1' -a "${1}" != "-c" ; then 
  134.   echo shar: Will not clobber existing file \"'uulocks.1'\"
  135. else
  136. echo shar: Extracting \"'uulocks.1'\" \(2124 characters\)
  137. sed "s/^X//" >'uulocks.1' <<'END_OF_FILE'
  138. X.\" $Id: uulocks.1,v 1.1 1993/07/01 20:17:02 vixie Exp $
  139. X.\"
  140. X.\"/* Copyright 1993 by Paul Vixie
  141. X.\" * All rights reserved
  142. X.\" *
  143. X.\" * Distribute freely, except: don't remove my name from the source or
  144. X.\" * documentation (don't take credit for my work), mark your changes (don't
  145. X.\" * get me blamed for your possible bugs), don't alter or remove this
  146. X.\" * notice.  May be sold if buildable source is provided to buyer.  No
  147. X.\" * warrantee of any kind, express or implied, is included with this
  148. X.\" * software; use at your own risk, responsibility for damages (if any) to
  149. X.\" * anyone resulting from the use of this software rests entirely with the
  150. X.\" * user.
  151. X.\" *
  152. X.\" * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
  153. X.\" * I'll try to keep a version up to date.  I can be reached as follows:
  154. X.\" * Paul Vixie <paul@vix.com> || {hoptoad,pacbell,decwrl,crash}!vixie!paul
  155. X.\" */
  156. X.TH UULOCKS 1 "1 July 1993"
  157. X.UC 4
  158. X.SH NAME
  159. Xuulocks \- print status summary of UUCP locks
  160. X.SH SYNOPSIS
  161. Xuulocks [ \-h ] [ \-p [ \-a psargs ] ]
  162. X.SH DESCRIPTION
  163. X.I Uulocks
  164. Xsearches the various directories which can contain UUCP-style ``lock'' files.
  165. XThese files are used by UUCP to prevent multiple connections to the same
  166. Xremote neighbor, and also to control access to the outgoing modems.  On some
  167. Xsystems,
  168. X.IR tip (1),
  169. X.IR kermit (1),
  170. Xand even
  171. X.IR getty (8)
  172. Xuse these same locks, thus permitting the outbound modems to be shared 
  173. Xbetween all modem applications.
  174. X.PP
  175. XIf the
  176. X.I \-h
  177. Xoption is given, no header will be printed for the report.  This is useful
  178. Xif you intend to post-process the output with
  179. X.IR perl (1)
  180. Xor
  181. X.IR awk (1).
  182. X.PP
  183. XIf the
  184. X.I \-p
  185. Xoption is given, then a
  186. X.IR ps (1)
  187. Xcommand will be executed for each process seen to have an active lock.  This
  188. Xcan be followed by the
  189. X.I \-a
  190. Xoption, which specifies the output format to be given to
  191. X.IR ps (1).
  192. XThe default for newer systems is 
  193. X.IR "\-o user,tt,ucomm,state,start" ,
  194. Xand for older systems it is simply
  195. X.IR u .
  196. X.SH "SEE ALSO"
  197. Xps(1), tip(1), kermit(1), getty(8)
  198. X.SH FILES
  199. X.nf
  200. X/var/spool/uucp/LCK..*
  201. X/var/run/LCK..*
  202. X.fi
  203. X.SH AUTHOR
  204. X.nf
  205. XPaul Vixie <paul@vix.com>
  206. END_OF_FILE
  207. if test 2124 -ne `wc -c <'uulocks.1'`; then
  208.     echo shar: \"'uulocks.1'\" unpacked with wrong size!
  209. fi
  210. # end of 'uulocks.1'
  211. fi
  212. if test -f 'uulocks.c' -a "${1}" != "-c" ; then 
  213.   echo shar: Will not clobber existing file \"'uulocks.c'\"
  214. else
  215. echo shar: Extracting \"'uulocks.c'\" \(3412 characters\)
  216. sed "s/^X//" >'uulocks.c' <<'END_OF_FILE'
  217. X/* uulocks - show status of UUCP ("modem") locks
  218. X * vixie 30jun93 [original]
  219. X *
  220. X * $Id: uulocks.c,v 1.1 1993/07/01 20:17:02 vixie Exp $
  221. X */
  222. X
  223. X#include <sys/param.h>
  224. X#include <sys/types.h>
  225. X#include <sys/stat.h>
  226. X#include <dirent.h>
  227. X#include <stdio.h>
  228. X#include <errno.h>
  229. X#include <string.h>
  230. X
  231. Xstatic char LckPre[] = "LCK..";
  232. Xstatic int LPLen = (sizeof LckPre) - 1;
  233. Xstatic int BadExit = 1, GoodExit = 0;
  234. Xstatic int PS = 0, Fix = 0, NoHdr = 0;
  235. X#if (BSD >= 199103)
  236. Xstatic char *PScmd = "ps %s -p %d";
  237. Xstatic char *PSarg = "-o user,tt,ucomm,state,start";
  238. X#else
  239. Xstatic char *PScmd = "ps %s%d";
  240. Xstatic char *PSarg = "u";
  241. X#endif
  242. Xstatic char AsciiBS = 0x08;
  243. Xstatic time_t Now;
  244. X
  245. Xmain(argc, argv)
  246. X    int argc;
  247. X    char *argv[];
  248. X{
  249. X    time(&Now);
  250. X    args(argc, argv);
  251. X    do_hdr();
  252. X    do_dir("/var/spool/uucp");
  253. X    do_dir("/var/run");
  254. X    exit(GoodExit);
  255. X}
  256. X
  257. Xusage() {
  258. X    fprintf(stderr, "usage:  uulocks [-h] [-p] [-a psargs] [-f]\n");
  259. X    exit(BadExit);
  260. X}
  261. X
  262. Xargs(argc, argv)
  263. X    int argc;
  264. X    char *argv[];
  265. X{
  266. X    extern char *optarg;
  267. X    char ch;
  268. X
  269. X    while ((ch = getopt(argc, argv, "hpa:f")) != EOF) {
  270. X        switch (ch) {
  271. X        case 'h': NoHdr++; break;
  272. X        case 'p': PS++; break;
  273. X        case 'a': PSarg = optarg; break;
  274. X        case 'f': Fix++; break;
  275. X        default: usage(); break;
  276. X        }
  277. X    }
  278. X}
  279. X
  280. Xdo_hdr() {
  281. X    if (!NoHdr) {
  282. X        fputs("\
  283. XResource           PID   Lock Age   PStat\
  284. X"            , stdout);
  285. X        if (PS) {
  286. X            fputs(          " (PS Result)", stdout);
  287. X        }
  288. X/*
  289. Xttya1            23321 001+00:35:51 alive (root 23321 ?? getty IE Wed12PM)
  290. X*/
  291. X        putchar('\n');
  292. X    }
  293. X}
  294. X
  295. Xdo_dir(path)
  296. X    char *path;
  297. X{
  298. X    DIR *dir;
  299. X    struct dirent *dp;
  300. X
  301. X    if (chdir(path) < 0) {
  302. X        perror(path);
  303. X        exit(BadExit);
  304. X    }
  305. X    if (!(dir = opendir("."))) {
  306. X        perror(path);
  307. X        exit(BadExit);
  308. X    }
  309. X    while (dp = readdir(dir)) {
  310. X        FILE *lck;
  311. X        pid_t pid;
  312. X        struct stat sb;
  313. X        time_t age;
  314. X        char timbuf[(sizeof "000+00:00:00")];
  315. X        int j;
  316. X
  317. X        if ((dp->d_namlen < LPLen)
  318. X            || strncmp("LCK..", dp->d_name, LPLen))
  319. X            continue;
  320. X        if (!(lck = fopen(dp->d_name, "r"))) {
  321. X            perror(dp->d_name);
  322. X            continue;
  323. X        }
  324. X        if (fstat(fileno(lck), &sb)) {
  325. X            perror(dp->d_name);
  326. X            continue;
  327. X        }
  328. X        pid = (pid_t) getw(lck);
  329. X        fclose(lck);
  330. X        age = Now - sb.st_mtime;
  331. X        strftime(timbuf, sizeof timbuf, "%j+%H:%M:%S", gmtime(&age));
  332. X        sprintf(timbuf, "%03d", atoi(timbuf)-1);
  333. X        timbuf[3] = '+';    /* ugly */
  334. X        if (kill(pid, 0) >= 0)
  335. X            j = 1;
  336. X        else
  337. X            j = (errno != ESRCH);
  338. X        printf("%-12s%10d %s %-5s",
  339. X               dp->d_name+LPLen, pid, timbuf,
  340. X               j ?"alive" :"dead");
  341. X        if (Fix && !j) {
  342. X            printf(" [unlink: %s]",
  343. X                   (unlink(dp->d_name)>=0) ?"ok" :strerror(errno));
  344. X        }
  345. X        if (PS && j) {
  346. X            char cmdbuf[100];    /* ugly */
  347. X            FILE *ps;
  348. X
  349. X            sprintf(cmdbuf, PScmd, PSarg, pid);
  350. X            if (!(ps = popen(cmdbuf, "r"))) {
  351. X                printf(" [ps: %s]", strerror(errno));
  352. X            } else {
  353. X                char ch, last_ch;
  354. X
  355. X                /* eat header */
  356. X                while (((ch = getc(ps)) != '\n')
  357. X                       && (ch != EOF)
  358. X                       )
  359. X                    ;
  360. X                /* get first line, compress multiple blanks */
  361. X                if ((last_ch = ch) != EOF) {
  362. X                    fputs(" (", stdout);
  363. X                    while (((ch = getc(ps)) != '\n')
  364. X                           && (ch != EOF)
  365. X                           ) {
  366. X                        if (ch == ' '
  367. X                            && last_ch == ' '
  368. X                            ) {
  369. X                            continue;
  370. X                        }
  371. X                        putchar(ch);
  372. X                        last_ch = ch;
  373. X                    }
  374. X                    if (last_ch == ' ') {
  375. X                        putchar(AsciiBS);  /* ugly */
  376. X                    }
  377. X                    putchar(')');
  378. X                }
  379. X                /* run it out */
  380. X                while (ch != EOF) {
  381. X                    last_ch = ch;
  382. X                    ch = getc(ps);
  383. X                }
  384. X            }
  385. X            pclose(ps);
  386. X        }
  387. X        putchar('\n');
  388. X    }
  389. X    closedir(dir);
  390. X}
  391. END_OF_FILE
  392. if test 3412 -ne `wc -c <'uulocks.c'`; then
  393.     echo shar: \"'uulocks.c'\" unpacked with wrong size!
  394. fi
  395. # end of 'uulocks.c'
  396. fi
  397. echo shar: End of shell archive.
  398. exit 0
  399.