]> git.ozlabs.org Git - ppp.git/blob - pppd/utils.c
New options system with priorities for option values, with options
[ppp.git] / pppd / utils.c
1 /*
2  * utils.c - various utility functions used in pppd.
3  *
4  * Copyright (c) 1999 The Australian National University.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms are permitted
8  * provided that the above copyright notice and this paragraph are
9  * duplicated in all such forms and that any documentation,
10  * advertising materials, and other materials related to such
11  * distribution and use acknowledge that the software was developed
12  * by the Australian National University.  The name of the University
13  * may not be used to endorse or promote products derived from this
14  * software without specific prior written permission.
15  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18  */
19
20 #define RCSID   "$Id: utils.c,v 1.11 2001/03/08 05:11:16 paulus Exp $"
21
22 #include <stdio.h>
23 #include <ctype.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <signal.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <syslog.h>
31 #include <netdb.h>
32 #include <utmp.h>
33 #include <pwd.h>
34 #include <sys/param.h>
35 #include <sys/types.h>
36 #include <sys/wait.h>
37 #include <sys/time.h>
38 #include <sys/resource.h>
39 #include <sys/stat.h>
40 #include <sys/socket.h>
41 #include <netinet/in.h>
42 #ifdef SVR4
43 #include <sys/mkdev.h>
44 #endif
45
46 #include "pppd.h"
47
48 static const char rcsid[] = RCSID;
49
50 #if defined(SUNOS4)
51 extern char *strerror();
52 #endif
53
54 static void logit __P((int, char *, va_list));
55 static void log_write __P((int, char *));
56 static void vslp_printer __P((void *, char *, ...));
57 static void format_packet __P((u_char *, int, void (*) (void *, char *, ...),
58                                void *));
59
60 struct buffer_info {
61     char *ptr;
62     int len;
63 };
64
65 /*
66  * strlcpy - like strcpy/strncpy, doesn't overflow destination buffer,
67  * always leaves destination null-terminated (for len > 0).
68  */
69 size_t
70 strlcpy(dest, src, len)
71     char *dest;
72     const char *src;
73     size_t len;
74 {
75     size_t ret = strlen(src);
76
77     if (len != 0) {
78         if (ret < len)
79             strcpy(dest, src);
80         else {
81             strncpy(dest, src, len - 1);
82             dest[len-1] = 0;
83         }
84     }
85     return ret;
86 }
87
88 /*
89  * strlcat - like strcat/strncat, doesn't overflow destination buffer,
90  * always leaves destination null-terminated (for len > 0).
91  */
92 size_t
93 strlcat(dest, src, len)
94     char *dest;
95     const char *src;
96     size_t len;
97 {
98     size_t dlen = strlen(dest);
99
100     return dlen + strlcpy(dest + dlen, src, (len > dlen? len - dlen: 0));
101 }
102
103
104 /*
105  * slprintf - format a message into a buffer.  Like sprintf except we
106  * also specify the length of the output buffer, and we handle
107  * %r (recursive format), %m (error message), %v (visible string),
108  * %q (quoted string), %t (current time) and %I (IP address) formats.
109  * Doesn't do floating-point formats.
110  * Returns the number of chars put into buf.
111  */
112 int
113 slprintf __V((char *buf, int buflen, char *fmt, ...))
114 {
115     va_list args;
116     int n;
117
118 #if defined(__STDC__)
119     va_start(args, fmt);
120 #else
121     char *buf;
122     int buflen;
123     char *fmt;
124     va_start(args);
125     buf = va_arg(args, char *);
126     buflen = va_arg(args, int);
127     fmt = va_arg(args, char *);
128 #endif
129     n = vslprintf(buf, buflen, fmt, args);
130     va_end(args);
131     return n;
132 }
133
134 /*
135  * vslprintf - like slprintf, takes a va_list instead of a list of args.
136  */
137 #define OUTCHAR(c)      (buflen > 0? (--buflen, *buf++ = (c)): 0)
138
139 int
140 vslprintf(buf, buflen, fmt, args)
141     char *buf;
142     int buflen;
143     char *fmt;
144     va_list args;
145 {
146     int c, i, n;
147     int width, prec, fillch;
148     int base, len, neg, quoted;
149     unsigned long val = 0;
150     char *str, *f, *buf0;
151     unsigned char *p;
152     char num[32];
153     time_t t;
154     u_int32_t ip;
155     static char hexchars[] = "0123456789abcdef";
156     struct buffer_info bufinfo;
157
158     buf0 = buf;
159     --buflen;
160     while (buflen > 0) {
161         for (f = fmt; *f != '%' && *f != 0; ++f)
162             ;
163         if (f > fmt) {
164             len = f - fmt;
165             if (len > buflen)
166                 len = buflen;
167             memcpy(buf, fmt, len);
168             buf += len;
169             buflen -= len;
170             fmt = f;
171         }
172         if (*fmt == 0)
173             break;
174         c = *++fmt;
175         width = 0;
176         prec = -1;
177         fillch = ' ';
178         if (c == '0') {
179             fillch = '0';
180             c = *++fmt;
181         }
182         if (c == '*') {
183             width = va_arg(args, int);
184             c = *++fmt;
185         } else {
186             while (isdigit(c)) {
187                 width = width * 10 + c - '0';
188                 c = *++fmt;
189             }
190         }
191         if (c == '.') {
192             c = *++fmt;
193             if (c == '*') {
194                 prec = va_arg(args, int);
195                 c = *++fmt;
196             } else {
197                 prec = 0;
198                 while (isdigit(c)) {
199                     prec = prec * 10 + c - '0';
200                     c = *++fmt;
201                 }
202             }
203         }
204         str = 0;
205         base = 0;
206         neg = 0;
207         ++fmt;
208         switch (c) {
209         case 'd':
210             i = va_arg(args, int);
211             if (i < 0) {
212                 neg = 1;
213                 val = -i;
214             } else
215                 val = i;
216             base = 10;
217             break;
218         case 'o':
219             val = va_arg(args, unsigned int);
220             base = 8;
221             break;
222         case 'x':
223         case 'X':
224             val = va_arg(args, unsigned int);
225             base = 16;
226             break;
227         case 'p':
228             val = (unsigned long) va_arg(args, void *);
229             base = 16;
230             neg = 2;
231             break;
232         case 's':
233             str = va_arg(args, char *);
234             break;
235         case 'c':
236             num[0] = va_arg(args, int);
237             num[1] = 0;
238             str = num;
239             break;
240         case 'm':
241             str = strerror(errno);
242             break;
243         case 'I':
244             ip = va_arg(args, u_int32_t);
245             ip = ntohl(ip);
246             slprintf(num, sizeof(num), "%d.%d.%d.%d", (ip >> 24) & 0xff,
247                      (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff);
248             str = num;
249             break;
250         case 'r':
251             f = va_arg(args, char *);
252 #ifndef __powerpc__
253             n = vslprintf(buf, buflen + 1, f, va_arg(args, va_list));
254 #else
255             /* On the powerpc, a va_list is an array of 1 structure */
256             n = vslprintf(buf, buflen + 1, f, va_arg(args, void *));
257 #endif
258             buf += n;
259             buflen -= n;
260             continue;
261         case 't':
262             time(&t);
263             str = ctime(&t);
264             str += 4;           /* chop off the day name */
265             str[15] = 0;        /* chop off year and newline */
266             break;
267         case 'v':               /* "visible" string */
268         case 'q':               /* quoted string */
269             quoted = c == 'q';
270             p = va_arg(args, unsigned char *);
271             if (fillch == '0' && prec >= 0) {
272                 n = prec;
273             } else {
274                 n = strlen((char *)p);
275                 if (prec >= 0 && n > prec)
276                     n = prec;
277             }
278             while (n > 0 && buflen > 0) {
279                 c = *p++;
280                 --n;
281                 if (!quoted && c >= 0x80) {
282                     OUTCHAR('M');
283                     OUTCHAR('-');
284                     c -= 0x80;
285                 }
286                 if (quoted && (c == '"' || c == '\\'))
287                     OUTCHAR('\\');
288                 if (c < 0x20 || (0x7f <= c && c < 0xa0)) {
289                     if (quoted) {
290                         OUTCHAR('\\');
291                         switch (c) {
292                         case '\t':      OUTCHAR('t');   break;
293                         case '\n':      OUTCHAR('n');   break;
294                         case '\b':      OUTCHAR('b');   break;
295                         case '\f':      OUTCHAR('f');   break;
296                         default:
297                             OUTCHAR('x');
298                             OUTCHAR(hexchars[c >> 4]);
299                             OUTCHAR(hexchars[c & 0xf]);
300                         }
301                     } else {
302                         if (c == '\t')
303                             OUTCHAR(c);
304                         else {
305                             OUTCHAR('^');
306                             OUTCHAR(c ^ 0x40);
307                         }
308                     }
309                 } else
310                     OUTCHAR(c);
311             }
312             continue;
313         case 'P':               /* print PPP packet */
314             bufinfo.ptr = buf;
315             bufinfo.len = buflen + 1;
316             p = va_arg(args, unsigned char *);
317             n = va_arg(args, int);
318             format_packet(p, n, vslp_printer, &bufinfo);
319             buf = bufinfo.ptr;
320             buflen = bufinfo.len - 1;
321             continue;
322         case 'B':
323             p = va_arg(args, unsigned char *);
324             for (n = prec; n > 0; --n) {
325                 c = *p++;
326                 if (fillch == ' ')
327                     OUTCHAR(' ');
328                 OUTCHAR(hexchars[(c >> 4) & 0xf]);
329                 OUTCHAR(hexchars[c & 0xf]);
330             }
331             continue;
332         default:
333             *buf++ = '%';
334             if (c != '%')
335                 --fmt;          /* so %z outputs %z etc. */
336             --buflen;
337             continue;
338         }
339         if (base != 0) {
340             str = num + sizeof(num);
341             *--str = 0;
342             while (str > num + neg) {
343                 *--str = hexchars[val % base];
344                 val = val / base;
345                 if (--prec <= 0 && val == 0)
346                     break;
347             }
348             switch (neg) {
349             case 1:
350                 *--str = '-';
351                 break;
352             case 2:
353                 *--str = 'x';
354                 *--str = '0';
355                 break;
356             }
357             len = num + sizeof(num) - 1 - str;
358         } else {
359             len = strlen(str);
360             if (prec >= 0 && len > prec)
361                 len = prec;
362         }
363         if (width > 0) {
364             if (width > buflen)
365                 width = buflen;
366             if ((n = width - len) > 0) {
367                 buflen -= n;
368                 for (; n > 0; --n)
369                     *buf++ = fillch;
370             }
371         }
372         if (len > buflen)
373             len = buflen;
374         memcpy(buf, str, len);
375         buf += len;
376         buflen -= len;
377     }
378     *buf = 0;
379     return buf - buf0;
380 }
381
382 /*
383  * vslp_printer - used in processing a %P format
384  */
385 static void
386 vslp_printer __V((void *arg, char *fmt, ...))
387 {
388     int n;
389     va_list pvar;
390     struct buffer_info *bi;
391
392 #if defined(__STDC__)
393     va_start(pvar, fmt);
394 #else
395     void *arg;
396     char *fmt;
397     va_start(pvar);
398     arg = va_arg(pvar, void *);
399     fmt = va_arg(pvar, char *);
400 #endif
401
402     bi = (struct buffer_info *) arg;
403     n = vslprintf(bi->ptr, bi->len, fmt, pvar);
404     va_end(pvar);
405
406     bi->ptr += n;
407     bi->len -= n;
408 }
409
410 #ifdef unused
411 /*
412  * log_packet - format a packet and log it.
413  */
414
415 void
416 log_packet(p, len, prefix, level)
417     u_char *p;
418     int len;
419     char *prefix;
420     int level;
421 {
422         init_pr_log(prefix, level);
423         format_packet(p, len, pr_log, &level);
424         end_pr_log();
425 }
426 #endif /* unused */
427
428 /*
429  * format_packet - make a readable representation of a packet,
430  * calling `printer(arg, format, ...)' to output it.
431  */
432 static void
433 format_packet(p, len, printer, arg)
434     u_char *p;
435     int len;
436     void (*printer) __P((void *, char *, ...));
437     void *arg;
438 {
439     int i, n;
440     u_short proto;
441     struct protent *protp;
442
443     if (len >= PPP_HDRLEN && p[0] == PPP_ALLSTATIONS && p[1] == PPP_UI) {
444         p += 2;
445         GETSHORT(proto, p);
446         len -= PPP_HDRLEN;
447         for (i = 0; (protp = protocols[i]) != NULL; ++i)
448             if (proto == protp->protocol)
449                 break;
450         if (protp != NULL) {
451             printer(arg, "[%s", protp->name);
452             n = (*protp->printpkt)(p, len, printer, arg);
453             printer(arg, "]");
454             p += n;
455             len -= n;
456         } else {
457             for (i = 0; (protp = protocols[i]) != NULL; ++i)
458                 if (proto == (protp->protocol & ~0x8000))
459                     break;
460             if (protp != 0 && protp->data_name != 0) {
461                 printer(arg, "[%s data]", protp->data_name);
462                 if (len > 8)
463                     printer(arg, "%.8B ...", p);
464                 else
465                     printer(arg, "%.*B", len, p);
466                 len = 0;
467             } else
468                 printer(arg, "[proto=0x%x]", proto);
469         }
470     }
471
472     if (len > 32)
473         printer(arg, "%.32B ...", p);
474     else
475         printer(arg, "%.*B", len, p);
476 }
477
478 /*
479  * init_pr_log, end_pr_log - initialize and finish use of pr_log.
480  */
481
482 static char line[256];          /* line to be logged accumulated here */
483 static char *linep;             /* current pointer within line */
484 static int llevel;              /* level for logging */
485
486 void
487 init_pr_log(prefix, level)
488      char *prefix;
489      int level;
490 {
491         linep = line;
492         if (prefix != NULL) {
493                 strlcpy(line, prefix, sizeof(line));
494                 linep = line + strlen(line);
495         }
496         llevel = level;
497 }
498
499 void
500 end_pr_log()
501 {
502         if (linep != line) {
503                 *linep = 0;
504                 log_write(llevel, line);
505         }
506 }
507
508 /*
509  * pr_log - printer routine for outputting to syslog
510  */
511 void
512 pr_log __V((void *arg, char *fmt, ...))
513 {
514         int l, n;
515         va_list pvar;
516         char *p, *eol;
517         char buf[256];
518
519 #if defined(__STDC__)
520         va_start(pvar, fmt);
521 #else
522         void *arg;
523         char *fmt;
524         va_start(pvar);
525         arg = va_arg(pvar, void *);
526         fmt = va_arg(pvar, char *);
527 #endif
528
529         n = vslprintf(buf, sizeof(buf), fmt, pvar);
530         va_end(pvar);
531
532         p = buf;
533         eol = strchr(buf, '\n');
534         if (linep != line) {
535                 l = (eol == NULL)? n: eol - buf;
536                 if (linep + l < line + sizeof(line)) {
537                         if (l > 0) {
538                                 memcpy(linep, buf, l);
539                                 linep += l;
540                         }
541                         if (eol == NULL)
542                                 return;
543                         p = eol + 1;
544                         eol = strchr(p, '\n');
545                 }
546                 *linep = 0;
547                 log_write(llevel, line);
548                 linep = line;
549         }
550
551         while (eol != NULL) {
552                 *eol = 0;
553                 log_write(llevel, p);
554                 p = eol + 1;
555         }
556
557         /* assumes sizeof(buf) <= sizeof(line) */
558         l = buf + n - p;
559         if (l > 0) {
560                 memcpy(line, p, n);
561                 linep = line + l;
562         }
563 }
564
565 /*
566  * print_string - print a readable representation of a string using
567  * printer.
568  */
569 void
570 print_string(p, len, printer, arg)
571     char *p;
572     int len;
573     void (*printer) __P((void *, char *, ...));
574     void *arg;
575 {
576     int c;
577
578     printer(arg, "\"");
579     for (; len > 0; --len) {
580         c = *p++;
581         if (' ' <= c && c <= '~') {
582             if (c == '\\' || c == '"')
583                 printer(arg, "\\");
584             printer(arg, "%c", c);
585         } else {
586             switch (c) {
587             case '\n':
588                 printer(arg, "\\n");
589                 break;
590             case '\r':
591                 printer(arg, "\\r");
592                 break;
593             case '\t':
594                 printer(arg, "\\t");
595                 break;
596             default:
597                 printer(arg, "\\%.3o", c);
598             }
599         }
600     }
601     printer(arg, "\"");
602 }
603
604 /*
605  * logit - does the hard work for fatal et al.
606  */
607 static void
608 logit(level, fmt, args)
609     int level;
610     char *fmt;
611     va_list args;
612 {
613     int n;
614     char buf[1024];
615
616     n = vslprintf(buf, sizeof(buf), fmt, args);
617     log_write(level, buf);
618 }
619
620 static void
621 log_write(level, buf)
622     int level;
623     char *buf;
624 {
625     syslog(level, "%s", buf);
626     if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
627         int n = strlen(buf);
628
629         if (n > 0 && buf[n-1] == '\n')
630             --n;
631         if (write(log_to_fd, buf, n) != n
632             || write(log_to_fd, "\n", 1) != 1)
633             log_to_fd = -1;
634     }
635 }
636
637 /*
638  * fatal - log an error message and die horribly.
639  */
640 void
641 fatal __V((char *fmt, ...))
642 {
643     va_list pvar;
644
645 #if defined(__STDC__)
646     va_start(pvar, fmt);
647 #else
648     char *fmt;
649     va_start(pvar);
650     fmt = va_arg(pvar, char *);
651 #endif
652
653     logit(LOG_ERR, fmt, pvar);
654     va_end(pvar);
655
656     die(1);                     /* as promised */
657 }
658
659 /*
660  * error - log an error message.
661  */
662 void
663 error __V((char *fmt, ...))
664 {
665     va_list pvar;
666
667 #if defined(__STDC__)
668     va_start(pvar, fmt);
669 #else
670     char *fmt;
671     va_start(pvar);
672     fmt = va_arg(pvar, char *);
673 #endif
674
675     logit(LOG_ERR, fmt, pvar);
676     va_end(pvar);
677 }
678
679 /*
680  * warn - log a warning message.
681  */
682 void
683 warn __V((char *fmt, ...))
684 {
685     va_list pvar;
686
687 #if defined(__STDC__)
688     va_start(pvar, fmt);
689 #else
690     char *fmt;
691     va_start(pvar);
692     fmt = va_arg(pvar, char *);
693 #endif
694
695     logit(LOG_WARNING, fmt, pvar);
696     va_end(pvar);
697 }
698
699 /*
700  * notice - log a notice-level message.
701  */
702 void
703 notice __V((char *fmt, ...))
704 {
705     va_list pvar;
706
707 #if defined(__STDC__)
708     va_start(pvar, fmt);
709 #else
710     char *fmt;
711     va_start(pvar);
712     fmt = va_arg(pvar, char *);
713 #endif
714
715     logit(LOG_NOTICE, fmt, pvar);
716     va_end(pvar);
717 }
718
719 /*
720  * info - log an informational message.
721  */
722 void
723 info __V((char *fmt, ...))
724 {
725     va_list pvar;
726
727 #if defined(__STDC__)
728     va_start(pvar, fmt);
729 #else
730     char *fmt;
731     va_start(pvar);
732     fmt = va_arg(pvar, char *);
733 #endif
734
735     logit(LOG_INFO, fmt, pvar);
736     va_end(pvar);
737 }
738
739 /*
740  * dbglog - log a debug message.
741  */
742 void
743 dbglog __V((char *fmt, ...))
744 {
745     va_list pvar;
746
747 #if defined(__STDC__)
748     va_start(pvar, fmt);
749 #else
750     char *fmt;
751     va_start(pvar);
752     fmt = va_arg(pvar, char *);
753 #endif
754
755     logit(LOG_DEBUG, fmt, pvar);
756     va_end(pvar);
757 }
758
759 /* Procedures for locking the serial device using a lock file. */
760 #ifndef LOCK_DIR
761 #ifdef _linux_
762 #define LOCK_DIR        "/var/lock"
763 #else
764 #ifdef SVR4
765 #define LOCK_DIR        "/var/spool/locks"
766 #else
767 #define LOCK_DIR        "/var/spool/lock"
768 #endif
769 #endif
770 #endif /* LOCK_DIR */
771
772 static char lock_file[MAXPATHLEN];
773
774 /*
775  * lock - create a lock file for the named device
776  */
777 int
778 lock(dev)
779     char *dev;
780 {
781 #ifdef LOCKLIB
782     int result;
783
784     result = mklock (dev, (void *) 0);
785     if (result == 0) {
786         strlcpy(lock_file, sizeof(lock_file), dev);
787         return 0;
788     }
789
790     if (result > 0)
791         notice("Device %s is locked by pid %d", dev, result);
792     else
793         error("Can't create lock file %s", lock_file);
794     return -1;
795
796 #else /* LOCKLIB */
797
798     char lock_buffer[12];
799     int fd, pid, n;
800
801 #ifdef SVR4
802     struct stat sbuf;
803
804     if (stat(dev, &sbuf) < 0) {
805         error("Can't get device number for %s: %m", dev);
806         return -1;
807     }
808     if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
809         error("Can't lock %s: not a character device", dev);
810         return -1;
811     }
812     slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d",
813              LOCK_DIR, major(sbuf.st_dev),
814              major(sbuf.st_rdev), minor(sbuf.st_rdev));
815 #else
816     char *p;
817
818     if ((p = strrchr(dev, '/')) != NULL)
819         dev = p + 1;
820     slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev);
821 #endif
822
823     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
824         if (errno != EEXIST) {
825             error("Can't create lock file %s: %m", lock_file);
826             break;
827         }
828
829         /* Read the lock file to find out who has the device locked. */
830         fd = open(lock_file, O_RDONLY, 0);
831         if (fd < 0) {
832             if (errno == ENOENT) /* This is just a timing problem. */
833                 continue;
834             error("Can't open existing lock file %s: %m", lock_file);
835             break;
836         }
837 #ifndef LOCK_BINARY
838         n = read(fd, lock_buffer, 11);
839 #else
840         n = read(fd, &pid, sizeof(pid));
841 #endif /* LOCK_BINARY */
842         close(fd);
843         fd = -1;
844         if (n <= 0) {
845             error("Can't read pid from lock file %s", lock_file);
846             break;
847         }
848
849         /* See if the process still exists. */
850 #ifndef LOCK_BINARY
851         lock_buffer[n] = 0;
852         pid = atoi(lock_buffer);
853 #endif /* LOCK_BINARY */
854         if (pid == getpid())
855             return 1;           /* somebody else locked it for us */
856         if (pid == 0
857             || (kill(pid, 0) == -1 && errno == ESRCH)) {
858             if (unlink (lock_file) == 0) {
859                 notice("Removed stale lock on %s (pid %d)", dev, pid);
860                 continue;
861             }
862             warn("Couldn't remove stale lock on %s", dev);
863         } else
864             notice("Device %s is locked by pid %d", dev, pid);
865         break;
866     }
867
868     if (fd < 0) {
869         lock_file[0] = 0;
870         return -1;
871     }
872
873     pid = getpid();
874 #ifndef LOCK_BINARY
875     slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
876     write (fd, lock_buffer, 11);
877 #else
878     write(fd, &pid, sizeof (pid));
879 #endif
880     close(fd);
881     return 0;
882
883 #endif
884 }
885
886 /*
887  * relock - called to update our lockfile when we are about to detach,
888  * thus changing our pid (we fork, the child carries on, and the parent dies).
889  * Note that this is called by the parent, with pid equal to the pid
890  * of the child.  This avoids a potential race which would exist if
891  * we had the child rewrite the lockfile (the parent might die first,
892  * and another process could think the lock was stale if it checked
893  * between when the parent died and the child rewrote the lockfile).
894  */
895 int
896 relock(pid)
897     int pid;
898 {
899 #ifdef LOCKLIB
900     /* XXX is there a way to do this? */
901     return -1;
902 #else /* LOCKLIB */
903
904     int fd;
905     char lock_buffer[12];
906
907     if (lock_file[0] == 0)
908         return -1;
909     fd = open(lock_file, O_WRONLY, 0);
910     if (fd < 0) {
911         error("Couldn't reopen lock file %s: %m", lock_file);
912         lock_file[0] = 0;
913         return -1;
914     }
915
916 #ifndef LOCK_BINARY
917     slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
918     write (fd, lock_buffer, 11);
919 #else
920     write(fd, &pid, sizeof(pid));
921 #endif /* LOCK_BINARY */
922     close(fd);
923     return 0;
924
925 #endif /* LOCKLIB */
926 }
927
928 /*
929  * unlock - remove our lockfile
930  */
931 void
932 unlock()
933 {
934     if (lock_file[0]) {
935 #ifdef LOCKLIB
936         (void) rmlock(lock_file, (void *) 0);
937 #else
938         unlink(lock_file);
939 #endif
940         lock_file[0] = 0;
941     }
942 }
943