]> git.ozlabs.org Git - ppp.git/blob - pppd/utils.c
e873ed65244f6fd2a55f80f2166edb0189dd604c
[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.12 2001/03/12 22:56:45 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                 eol = strchr(p, '\n');
556         }
557
558         /* assumes sizeof(buf) <= sizeof(line) */
559         l = buf + n - p;
560         if (l > 0) {
561                 memcpy(line, p, n);
562                 linep = line + l;
563         }
564 }
565
566 /*
567  * print_string - print a readable representation of a string using
568  * printer.
569  */
570 void
571 print_string(p, len, printer, arg)
572     char *p;
573     int len;
574     void (*printer) __P((void *, char *, ...));
575     void *arg;
576 {
577     int c;
578
579     printer(arg, "\"");
580     for (; len > 0; --len) {
581         c = *p++;
582         if (' ' <= c && c <= '~') {
583             if (c == '\\' || c == '"')
584                 printer(arg, "\\");
585             printer(arg, "%c", c);
586         } else {
587             switch (c) {
588             case '\n':
589                 printer(arg, "\\n");
590                 break;
591             case '\r':
592                 printer(arg, "\\r");
593                 break;
594             case '\t':
595                 printer(arg, "\\t");
596                 break;
597             default:
598                 printer(arg, "\\%.3o", c);
599             }
600         }
601     }
602     printer(arg, "\"");
603 }
604
605 /*
606  * logit - does the hard work for fatal et al.
607  */
608 static void
609 logit(level, fmt, args)
610     int level;
611     char *fmt;
612     va_list args;
613 {
614     int n;
615     char buf[1024];
616
617     n = vslprintf(buf, sizeof(buf), fmt, args);
618     log_write(level, buf);
619 }
620
621 static void
622 log_write(level, buf)
623     int level;
624     char *buf;
625 {
626     syslog(level, "%s", buf);
627     if (log_to_fd >= 0 && (level != LOG_DEBUG || debug)) {
628         int n = strlen(buf);
629
630         if (n > 0 && buf[n-1] == '\n')
631             --n;
632         if (write(log_to_fd, buf, n) != n
633             || write(log_to_fd, "\n", 1) != 1)
634             log_to_fd = -1;
635     }
636 }
637
638 /*
639  * fatal - log an error message and die horribly.
640  */
641 void
642 fatal __V((char *fmt, ...))
643 {
644     va_list pvar;
645
646 #if defined(__STDC__)
647     va_start(pvar, fmt);
648 #else
649     char *fmt;
650     va_start(pvar);
651     fmt = va_arg(pvar, char *);
652 #endif
653
654     logit(LOG_ERR, fmt, pvar);
655     va_end(pvar);
656
657     die(1);                     /* as promised */
658 }
659
660 /*
661  * error - log an error message.
662  */
663 void
664 error __V((char *fmt, ...))
665 {
666     va_list pvar;
667
668 #if defined(__STDC__)
669     va_start(pvar, fmt);
670 #else
671     char *fmt;
672     va_start(pvar);
673     fmt = va_arg(pvar, char *);
674 #endif
675
676     logit(LOG_ERR, fmt, pvar);
677     va_end(pvar);
678 }
679
680 /*
681  * warn - log a warning message.
682  */
683 void
684 warn __V((char *fmt, ...))
685 {
686     va_list pvar;
687
688 #if defined(__STDC__)
689     va_start(pvar, fmt);
690 #else
691     char *fmt;
692     va_start(pvar);
693     fmt = va_arg(pvar, char *);
694 #endif
695
696     logit(LOG_WARNING, fmt, pvar);
697     va_end(pvar);
698 }
699
700 /*
701  * notice - log a notice-level message.
702  */
703 void
704 notice __V((char *fmt, ...))
705 {
706     va_list pvar;
707
708 #if defined(__STDC__)
709     va_start(pvar, fmt);
710 #else
711     char *fmt;
712     va_start(pvar);
713     fmt = va_arg(pvar, char *);
714 #endif
715
716     logit(LOG_NOTICE, fmt, pvar);
717     va_end(pvar);
718 }
719
720 /*
721  * info - log an informational message.
722  */
723 void
724 info __V((char *fmt, ...))
725 {
726     va_list pvar;
727
728 #if defined(__STDC__)
729     va_start(pvar, fmt);
730 #else
731     char *fmt;
732     va_start(pvar);
733     fmt = va_arg(pvar, char *);
734 #endif
735
736     logit(LOG_INFO, fmt, pvar);
737     va_end(pvar);
738 }
739
740 /*
741  * dbglog - log a debug message.
742  */
743 void
744 dbglog __V((char *fmt, ...))
745 {
746     va_list pvar;
747
748 #if defined(__STDC__)
749     va_start(pvar, fmt);
750 #else
751     char *fmt;
752     va_start(pvar);
753     fmt = va_arg(pvar, char *);
754 #endif
755
756     logit(LOG_DEBUG, fmt, pvar);
757     va_end(pvar);
758 }
759
760 /* Procedures for locking the serial device using a lock file. */
761 #ifndef LOCK_DIR
762 #ifdef _linux_
763 #define LOCK_DIR        "/var/lock"
764 #else
765 #ifdef SVR4
766 #define LOCK_DIR        "/var/spool/locks"
767 #else
768 #define LOCK_DIR        "/var/spool/lock"
769 #endif
770 #endif
771 #endif /* LOCK_DIR */
772
773 static char lock_file[MAXPATHLEN];
774
775 /*
776  * lock - create a lock file for the named device
777  */
778 int
779 lock(dev)
780     char *dev;
781 {
782 #ifdef LOCKLIB
783     int result;
784
785     result = mklock (dev, (void *) 0);
786     if (result == 0) {
787         strlcpy(lock_file, sizeof(lock_file), dev);
788         return 0;
789     }
790
791     if (result > 0)
792         notice("Device %s is locked by pid %d", dev, result);
793     else
794         error("Can't create lock file %s", lock_file);
795     return -1;
796
797 #else /* LOCKLIB */
798
799     char lock_buffer[12];
800     int fd, pid, n;
801
802 #ifdef SVR4
803     struct stat sbuf;
804
805     if (stat(dev, &sbuf) < 0) {
806         error("Can't get device number for %s: %m", dev);
807         return -1;
808     }
809     if ((sbuf.st_mode & S_IFMT) != S_IFCHR) {
810         error("Can't lock %s: not a character device", dev);
811         return -1;
812     }
813     slprintf(lock_file, sizeof(lock_file), "%s/LK.%03d.%03d.%03d",
814              LOCK_DIR, major(sbuf.st_dev),
815              major(sbuf.st_rdev), minor(sbuf.st_rdev));
816 #else
817     char *p;
818
819     if ((p = strrchr(dev, '/')) != NULL)
820         dev = p + 1;
821     slprintf(lock_file, sizeof(lock_file), "%s/LCK..%s", LOCK_DIR, dev);
822 #endif
823
824     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
825         if (errno != EEXIST) {
826             error("Can't create lock file %s: %m", lock_file);
827             break;
828         }
829
830         /* Read the lock file to find out who has the device locked. */
831         fd = open(lock_file, O_RDONLY, 0);
832         if (fd < 0) {
833             if (errno == ENOENT) /* This is just a timing problem. */
834                 continue;
835             error("Can't open existing lock file %s: %m", lock_file);
836             break;
837         }
838 #ifndef LOCK_BINARY
839         n = read(fd, lock_buffer, 11);
840 #else
841         n = read(fd, &pid, sizeof(pid));
842 #endif /* LOCK_BINARY */
843         close(fd);
844         fd = -1;
845         if (n <= 0) {
846             error("Can't read pid from lock file %s", lock_file);
847             break;
848         }
849
850         /* See if the process still exists. */
851 #ifndef LOCK_BINARY
852         lock_buffer[n] = 0;
853         pid = atoi(lock_buffer);
854 #endif /* LOCK_BINARY */
855         if (pid == getpid())
856             return 1;           /* somebody else locked it for us */
857         if (pid == 0
858             || (kill(pid, 0) == -1 && errno == ESRCH)) {
859             if (unlink (lock_file) == 0) {
860                 notice("Removed stale lock on %s (pid %d)", dev, pid);
861                 continue;
862             }
863             warn("Couldn't remove stale lock on %s", dev);
864         } else
865             notice("Device %s is locked by pid %d", dev, pid);
866         break;
867     }
868
869     if (fd < 0) {
870         lock_file[0] = 0;
871         return -1;
872     }
873
874     pid = getpid();
875 #ifndef LOCK_BINARY
876     slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
877     write (fd, lock_buffer, 11);
878 #else
879     write(fd, &pid, sizeof (pid));
880 #endif
881     close(fd);
882     return 0;
883
884 #endif
885 }
886
887 /*
888  * relock - called to update our lockfile when we are about to detach,
889  * thus changing our pid (we fork, the child carries on, and the parent dies).
890  * Note that this is called by the parent, with pid equal to the pid
891  * of the child.  This avoids a potential race which would exist if
892  * we had the child rewrite the lockfile (the parent might die first,
893  * and another process could think the lock was stale if it checked
894  * between when the parent died and the child rewrote the lockfile).
895  */
896 int
897 relock(pid)
898     int pid;
899 {
900 #ifdef LOCKLIB
901     /* XXX is there a way to do this? */
902     return -1;
903 #else /* LOCKLIB */
904
905     int fd;
906     char lock_buffer[12];
907
908     if (lock_file[0] == 0)
909         return -1;
910     fd = open(lock_file, O_WRONLY, 0);
911     if (fd < 0) {
912         error("Couldn't reopen lock file %s: %m", lock_file);
913         lock_file[0] = 0;
914         return -1;
915     }
916
917 #ifndef LOCK_BINARY
918     slprintf(lock_buffer, sizeof(lock_buffer), "%10d\n", pid);
919     write (fd, lock_buffer, 11);
920 #else
921     write(fd, &pid, sizeof(pid));
922 #endif /* LOCK_BINARY */
923     close(fd);
924     return 0;
925
926 #endif /* LOCKLIB */
927 }
928
929 /*
930  * unlock - remove our lockfile
931  */
932 void
933 unlock()
934 {
935     if (lock_file[0]) {
936 #ifdef LOCKLIB
937         (void) rmlock(lock_file, (void *) 0);
938 #else
939         unlink(lock_file);
940 #endif
941         lock_file[0] = 0;
942     }
943 }
944