]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-sunos4.c
minor changes so it compiles with pre-ansi compiler;
[ppp.git] / pppd / sys-sunos4.c
1 /*
2  * System-dependent procedures for pppd under SunOS 4.
3  *
4  * Copyright (c) 1994 The Australian National University.
5  * All rights reserved.
6  *
7  * Permission to use, copy, modify, and distribute this software and its
8  * documentation is hereby granted, provided that the above copyright
9  * notice appears in all copies.  This software is provided without any
10  * warranty, express or implied. The Australian National University
11  * makes no representations about the suitability of this software for
12  * any purpose.
13  *
14  * IN NO EVENT SHALL THE AUSTRALIAN NATIONAL UNIVERSITY BE LIABLE TO ANY
15  * PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
16  * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
17  * THE AUSTRALIAN NATIONAL UNIVERSITY HAVE BEEN ADVISED OF THE POSSIBILITY
18  * OF SUCH DAMAGE.
19  *
20  * THE AUSTRALIAN NATIONAL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
21  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
23  * ON AN "AS IS" BASIS, AND THE AUSTRALIAN NATIONAL UNIVERSITY HAS NO
24  * OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS,
25  * OR MODIFICATIONS.
26  */
27
28 #ifndef lint
29 static char rcsid[] = "$Id: sys-sunos4.c,v 1.12 1999/03/08 05:34:46 paulus Exp $";
30 #endif
31
32 #include <stdio.h>
33 #include <stddef.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <unistd.h>
40 #include <termios.h>
41 #include <signal.h>
42 #include <malloc.h>
43 #include <utmp.h>
44 #include <sys/types.h>
45 #include <sys/param.h>
46 #include <sys/socket.h>
47 #include <sys/sockio.h>
48 #include <sys/stream.h>
49 #include <sys/stropts.h>
50 #include <sys/syslog.h>
51 #include <sys/stat.h>
52 #include <sys/time.h>
53 #include <sys/poll.h>
54 #include <net/if.h>
55 #include <net/if_arp.h>
56 #include <net/nit_if.h>
57 #include <net/route.h>
58 #include <net/ppp_defs.h>
59 #include <net/pppio.h>
60 #include <netinet/in.h>
61
62 #include "pppd.h"
63
64 #if defined(sun) && defined(sparc)
65 #include <alloca.h>
66 #ifndef __GNUC__
67 extern void *alloca();
68 #endif
69 #endif /*sparc*/
70
71 static int      pppfd;
72 static int      fdmuxid = -1;
73 static int      iffd;
74 static int      sockfd;
75
76 static int      restore_term;
77 static struct termios inittermios;
78 static struct winsize wsinfo;   /* Initial window size info */
79 static pid_t    parent_pid;     /* PID of our parent */
80
81 extern u_char   inpacket_buf[]; /* borrowed from main.c */
82
83 static int      link_mtu, link_mru;
84
85 #define NMODULES        32
86 static int      tty_nmodules;
87 static char     tty_modules[NMODULES][FMNAMESZ+1];
88
89 static int      if_is_up;       /* Interface has been marked up */
90 static u_int32_t ifaddrs[2];    /* local and remote addresses */
91 static u_int32_t default_route_gateway; /* Gateway for default route added */
92 static u_int32_t proxy_arp_addr;        /* Addr for proxy arp entry added */
93
94 /* Prototypes for procedures local to this file. */
95 static int translate_speed __P((int));
96 static int baud_rate_of __P((int));
97 static int get_ether_addr __P((u_int32_t, struct sockaddr *));
98 static int strioctl __P((int, int, void *, int, int));
99
100
101 /*
102  * sys_init - System-dependent initialization.
103  */
104 void
105 sys_init()
106 {
107     int x;
108
109     /* Get an internet socket for doing socket ioctl's on. */
110     if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
111         syslog(LOG_ERR, "Couldn't create IP socket: %m");
112         die(1);
113     }
114
115     /*
116      * We may want to send a SIGHUP to the session leader associated
117      * with our controlling terminal later.  Because SunOS doesn't
118      * have getsid(), we make do with sending the signal to our
119      * parent process.
120      */
121     parent_pid = getppid();
122
123     /*
124      * Open the ppp device.
125      */
126     pppfd = open("/dev/ppp", O_RDWR | O_NONBLOCK, 0);
127     if (pppfd < 0) {
128         syslog(LOG_ERR, "Can't open /dev/ppp: %m");
129         die(1);
130     }
131     if (kdebugflag) {
132         x = PPPDBG_LOG + PPPDBG_DRIVER;
133         strioctl(pppfd, PPPIO_DEBUG, &x, sizeof(int), 0);
134     }
135
136     /* Assign a new PPA and get its unit number. */
137     if (strioctl(pppfd, PPPIO_NEWPPA, &ifunit, 0, sizeof(int)) < 0) {
138         syslog(LOG_ERR, "Can't create new PPP interface: %m");
139         die(1);
140     }
141
142     /*
143      * Open the ppp device again and push the if_ppp module on it.
144      */
145     iffd = open("/dev/ppp", O_RDWR, 0);
146     if (iffd < 0) {
147         syslog(LOG_ERR, "Can't open /dev/ppp (2): %m");
148         die(1);
149     }
150     if (kdebugflag) {
151         x = PPPDBG_LOG + PPPDBG_DRIVER;
152         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
153     }
154     if (strioctl(iffd, PPPIO_ATTACH, &ifunit, sizeof(int), 0) < 0) {
155         syslog(LOG_ERR, "Couldn't attach ppp interface to device: %m");
156         die(1);
157     }
158     if (ioctl(iffd, I_PUSH, "if_ppp") < 0) {
159         syslog(LOG_ERR, "Can't push ppp interface module: %m");
160         die(1);
161     }
162     if (kdebugflag) {
163         x = PPPDBG_LOG + PPPDBG_IF;
164         strioctl(iffd, PPPIO_DEBUG, &x, sizeof(int), 0);
165     }
166     if (strioctl(iffd, PPPIO_NEWPPA, &ifunit, sizeof(int), 0) < 0) {
167         syslog(LOG_ERR, "Couldn't create ppp interface unit: %m");
168         die(1);
169     }
170     x = PPP_IP;
171     if (strioctl(iffd, PPPIO_BIND, &x, sizeof(int), 0) < 0) {
172         syslog(LOG_ERR, "Couldn't bind ppp interface to IP SAP: %m");
173         die(1);
174     }
175 }
176
177 /*
178  * sys_cleanup - restore any system state we modified before exiting:
179  * mark the interface down, delete default route and/or proxy arp entry.
180  * This shouldn't call die() because it's called from die().
181  */
182 void
183 sys_cleanup()
184 {
185     if (if_is_up)
186         sifdown(0);
187     if (ifaddrs[0])
188         cifaddr(0, ifaddrs[0], ifaddrs[1]);
189     if (default_route_gateway)
190         cifdefaultroute(0, 0, default_route_gateway);
191     if (proxy_arp_addr)
192         cifproxyarp(0, proxy_arp_addr);
193 }
194
195 /*
196  * sys_close - Clean up in a child process before execing.
197  */
198 void
199 sys_close()
200 {
201     close(iffd);
202     close(pppfd);
203     close(sockfd);
204 }
205
206 /*
207  * sys_check_options - check the options that the user specified
208  */
209 int
210 sys_check_options()
211 {
212     return 1;
213 }
214
215
216 /*
217  * daemon - Detach us from controlling terminal session.
218  */
219 int
220 daemon(nochdir, noclose)
221     int nochdir, noclose;
222 {
223     int pid;
224
225     if ((pid = fork()) < 0)
226         return -1;
227     if (pid != 0)
228         exit(0);                /* parent dies */
229     setsid();
230     if (!nochdir)
231         chdir("/");
232     if (!noclose) {
233         fclose(stdin);          /* don't need stdin, stdout, stderr */
234         fclose(stdout);
235         fclose(stderr);
236     }
237     return 0;
238 }
239
240 /*
241  * ppp_available - check whether the system has any ppp interfaces
242  */
243 int
244 ppp_available()
245 {
246     struct stat buf;
247
248     return stat("/dev/ppp", &buf) >= 0;
249 }
250
251 /*
252  * establish_ppp - Turn the serial port into a ppp interface.
253  */
254 void
255 establish_ppp(fd)
256     int fd;
257 {
258     int i;
259
260     /* Pop any existing modules off the tty stream. */
261     for (i = 0;; ++i)
262         if (ioctl(fd, I_LOOK, tty_modules[i]) < 0
263             || ioctl(fd, I_POP, 0) < 0)
264             break;
265     tty_nmodules = i;
266
267     /* Push the async hdlc module and the compressor module. */
268     if (ioctl(fd, I_PUSH, "ppp_ahdl") < 0) {
269         syslog(LOG_ERR, "Couldn't push PPP Async HDLC module: %m");
270         die(1);
271     }
272     if (ioctl(fd, I_PUSH, "ppp_comp") < 0) {
273         syslog(LOG_ERR, "Couldn't push PPP compression module: %m");
274 /*      die(1); */
275     }
276
277     /* Link the serial port under the PPP multiplexor. */
278     if ((fdmuxid = ioctl(pppfd, I_LINK, fd)) < 0) {
279         syslog(LOG_ERR, "Can't link tty to PPP mux: %m");
280         die(1);
281     }
282 }
283
284 /*
285  * restore_loop - reattach the ppp unit to the loopback.
286  * This doesn't need to do anything because disestablish_ppp does it.
287  */
288 void
289 restore_loop()
290 {
291 }
292
293 /*
294  * disestablish_ppp - Restore the serial port to normal operation.
295  * It attempts to reconstruct the stream with the previously popped
296  * modules.  This shouldn't call die() because it's called from die().
297  */
298 void
299 disestablish_ppp(fd)
300     int fd;
301 {
302     int i;
303
304     if (fdmuxid >= 0) {
305         if (ioctl(pppfd, I_UNLINK, fdmuxid) < 0) {
306             if (!hungup)
307                 syslog(LOG_ERR, "Can't unlink tty from PPP mux: %m");
308         }
309         fdmuxid = -1;
310
311         if (!hungup) {
312             while (ioctl(fd, I_POP, 0) >= 0)
313                 ;
314             for (i = tty_nmodules - 1; i >= 0; --i)
315                 if (ioctl(fd, I_PUSH, tty_modules[i]) < 0)
316                     syslog(LOG_ERR, "Couldn't restore tty module %s: %m",
317                            tty_modules[i]);
318         }
319         if (hungup && default_device && parent_pid > 0) {
320             /*
321              * If we have received a hangup, we need to send a SIGHUP
322              * to the terminal's controlling process.  The reason is
323              * that the original stream head for the terminal hasn't
324              * seen the M_HANGUP message (it went up through the ppp
325              * driver to the stream head for our fd to /dev/ppp).
326              * Actually we send the signal to the process that invoked
327              * pppd, since SunOS doesn't have getsid().
328              */
329             kill(parent_pid, SIGHUP);
330         }
331     }
332 }
333
334 /*
335  * Check whether the link seems not to be 8-bit clean.
336  */
337 void
338 clean_check()
339 {
340     int x;
341     char *s;
342
343     if (strioctl(pppfd, PPPIO_GCLEAN, &x, 0, sizeof(x)) < 0)
344         return;
345     s = NULL;
346     switch (~x) {
347     case RCV_B7_0:
348         s = "bit 7 set to 1";
349         break;
350     case RCV_B7_1:
351         s = "bit 7 set to 0";
352         break;
353     case RCV_EVNP:
354         s = "odd parity";
355         break;
356     case RCV_ODDP:
357         s = "even parity";
358         break;
359     }
360     if (s != NULL) {
361         syslog(LOG_WARNING, "Serial link is not 8-bit clean:");
362         syslog(LOG_WARNING, "All received characters had %s", s);
363     }
364 }
365
366 /*
367  * List of valid speeds.
368  */
369 struct speed {
370     int speed_int, speed_val;
371 } speeds[] = {
372 #ifdef B50
373     { 50, B50 },
374 #endif
375 #ifdef B75
376     { 75, B75 },
377 #endif
378 #ifdef B110
379     { 110, B110 },
380 #endif
381 #ifdef B134
382     { 134, B134 },
383 #endif
384 #ifdef B150
385     { 150, B150 },
386 #endif
387 #ifdef B200
388     { 200, B200 },
389 #endif
390 #ifdef B300
391     { 300, B300 },
392 #endif
393 #ifdef B600
394     { 600, B600 },
395 #endif
396 #ifdef B1200
397     { 1200, B1200 },
398 #endif
399 #ifdef B1800
400     { 1800, B1800 },
401 #endif
402 #ifdef B2000
403     { 2000, B2000 },
404 #endif
405 #ifdef B2400
406     { 2400, B2400 },
407 #endif
408 #ifdef B3600
409     { 3600, B3600 },
410 #endif
411 #ifdef B4800
412     { 4800, B4800 },
413 #endif
414 #ifdef B7200
415     { 7200, B7200 },
416 #endif
417 #ifdef B9600
418     { 9600, B9600 },
419 #endif
420 #ifdef B19200
421     { 19200, B19200 },
422 #endif
423 #ifdef B38400
424     { 38400, B38400 },
425 #endif
426 #ifdef EXTA
427     { 19200, EXTA },
428 #endif
429 #ifdef EXTB
430     { 38400, EXTB },
431 #endif
432 #ifdef B57600
433     { 57600, B57600 },
434 #endif
435 #ifdef B115200
436     { 115200, B115200 },
437 #endif
438     { 0, 0 }
439 };
440
441 /*
442  * Translate from bits/second to a speed_t.
443  */
444 static int
445 translate_speed(bps)
446     int bps;
447 {
448     struct speed *speedp;
449
450     if (bps == 0)
451         return 0;
452     for (speedp = speeds; speedp->speed_int; speedp++)
453         if (bps == speedp->speed_int)
454             return speedp->speed_val;
455     syslog(LOG_WARNING, "speed %d not supported", bps);
456     return 0;
457 }
458
459 /*
460  * Translate from a speed_t to bits/second.
461  */
462 static int
463 baud_rate_of(speed)
464     int speed;
465 {
466     struct speed *speedp;
467
468     if (speed == 0)
469         return 0;
470     for (speedp = speeds; speedp->speed_int; speedp++)
471         if (speed == speedp->speed_val)
472             return speedp->speed_int;
473     return 0;
474 }
475
476 /*
477  * set_up_tty: Set up the serial port on `fd' for 8 bits, no parity,
478  * at the requested speed, etc.  If `local' is true, set CLOCAL
479  * regardless of whether the modem option was specified.
480  */
481 void
482 set_up_tty(fd, local)
483     int fd, local;
484 {
485     int speed;
486     struct termios tios;
487
488     if (tcgetattr(fd, &tios) < 0) {
489         syslog(LOG_ERR, "tcgetattr: %m");
490         die(1);
491     }
492
493     if (!restore_term) {
494         inittermios = tios;
495         ioctl(fd, TIOCGWINSZ, &wsinfo);
496     }
497
498     tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB | CLOCAL);
499     if (crtscts > 0)
500         tios.c_cflag |= CRTSCTS;
501     else if (crtscts < 0)
502         tios.c_cflag &= ~CRTSCTS;
503
504     tios.c_cflag |= CS8 | CREAD | HUPCL;
505     if (local || !modem)
506         tios.c_cflag |= CLOCAL;
507     tios.c_iflag = IGNBRK | IGNPAR;
508     tios.c_oflag = 0;
509     tios.c_lflag = 0;
510     tios.c_cc[VMIN] = 1;
511     tios.c_cc[VTIME] = 0;
512
513     if (crtscts == -2) {
514         tios.c_iflag |= IXON | IXOFF;
515         tios.c_cc[VSTOP] = 0x13;        /* DC3 = XOFF = ^S */
516         tios.c_cc[VSTART] = 0x11;       /* DC1 = XON  = ^Q */
517     }
518
519     speed = translate_speed(inspeed);
520     if (speed) {
521         cfsetospeed(&tios, speed);
522         cfsetispeed(&tios, speed);
523     } else {
524         speed = cfgetospeed(&tios);
525         /*
526          * We can't proceed if the serial port speed is 0,
527          * since that implies that the serial port is disabled.
528          */
529         if (speed == B0) {
530             syslog(LOG_ERR, "Baud rate for %s is 0; need explicit baud rate",
531                    devnam);
532             die(1);
533         }
534     }
535
536     if (tcsetattr(fd, TCSAFLUSH, &tios) < 0) {
537         syslog(LOG_ERR, "tcsetattr: %m");
538         die(1);
539     }
540
541     baud_rate = inspeed = baud_rate_of(speed);
542     restore_term = 1;
543 }
544
545 /*
546  * restore_tty - restore the terminal to the saved settings.
547  */
548 void
549 restore_tty(fd)
550     int fd;
551 {
552     if (restore_term) {
553         if (!default_device) {
554             /*
555              * Turn off echoing, because otherwise we can get into
556              * a loop with the tty and the modem echoing to each other.
557              * We presume we are the sole user of this tty device, so
558              * when we close it, it will revert to its defaults anyway.
559              */
560             inittermios.c_lflag &= ~(ECHO | ECHONL);
561         }
562         if (tcsetattr(fd, TCSAFLUSH, &inittermios) < 0)
563             if (!hungup && errno != ENXIO)
564                 syslog(LOG_WARNING, "tcsetattr: %m");
565         ioctl(fd, TIOCSWINSZ, &wsinfo);
566         restore_term = 0;
567     }
568 }
569
570 /*
571  * setdtr - control the DTR line on the serial port.
572  * This is called from die(), so it shouldn't call die().
573  */
574 void
575 setdtr(fd, on)
576 int fd, on;
577 {
578     int modembits = TIOCM_DTR;
579
580     ioctl(fd, (on? TIOCMBIS: TIOCMBIC), &modembits);
581 }
582
583 /*
584  * open_loopback - open the device we use for getting packets
585  * in demand mode.  Under Solaris 2, we use our existing fd
586  * to the ppp driver.
587  */
588 void
589 open_ppp_loopback()
590 {
591 }
592
593 /*
594  * output - Output PPP packet.
595  */
596 void
597 output(unit, p, len)
598     int unit;
599     u_char *p;
600     int len;
601 {
602     struct strbuf data;
603     int retries;
604     struct pollfd pfd;
605
606     if (debug)
607         log_packet(p, len, "sent ", LOG_DEBUG);
608
609     data.len = len;
610     data.buf = (caddr_t) p;
611     retries = 4;
612     while (putmsg(pppfd, NULL, &data, 0) < 0) {
613         if (--retries < 0 || (errno != EWOULDBLOCK && errno != EAGAIN)) {
614             if (errno != ENXIO)
615                 syslog(LOG_ERR, "Couldn't send packet: %m");
616             break;
617         }
618         pfd.fd = pppfd;
619         pfd.events = POLLOUT;
620         poll(&pfd, 1, 250);     /* wait for up to 0.25 seconds */
621     }
622 }
623
624
625 /*
626  * wait_input - wait until there is data available on fd,
627  * for the length of time specified by *timo (indefinite
628  * if timo is NULL).
629  */
630 void
631 wait_input(timo)
632     struct timeval *timo;
633 {
634     int t;
635     struct pollfd pfd;
636
637     t = timo == NULL? -1: timo->tv_sec * 1000 + timo->tv_usec / 1000;
638     pfd.fd = pppfd;
639     pfd.events = POLLIN | POLLPRI | POLLHUP;
640     if (poll(&pfd, 1, t) < 0 && errno != EINTR) {
641         syslog(LOG_ERR, "poll: %m");
642         die(1);
643     }
644 }
645
646 /*
647  * wait_loop_output - wait until there is data available on the
648  * loopback, for the length of time specified by *timo (indefinite
649  * if timo is NULL).
650  */
651 void
652 wait_loop_output(timo)
653     struct timeval *timo;
654 {
655     wait_input(timo);
656 }
657
658 /*
659  * wait_time - wait for a given length of time or until a
660  * signal is received.
661  */
662 void
663 wait_time(timo)
664     struct timeval *timo;
665 {
666     int n;
667
668     n = select(0, NULL, NULL, NULL, timo);
669     if (n < 0 && errno != EINTR) {
670         syslog(LOG_ERR, "select: %m");
671         die(1);
672     }
673 }
674
675
676 /*
677  * read_packet - get a PPP packet from the serial device.
678  */
679 int
680 read_packet(buf)
681     u_char *buf;
682 {
683     struct strbuf ctrl, data;
684     int flags, len;
685     unsigned char ctrlbuf[64];
686
687     for (;;) {
688         data.maxlen = PPP_MRU + PPP_HDRLEN;
689         data.buf = (caddr_t) buf;
690         ctrl.maxlen = sizeof(ctrlbuf);
691         ctrl.buf = (caddr_t) ctrlbuf;
692         flags = 0;
693         len = getmsg(pppfd, &ctrl, &data, &flags);
694         if (len < 0) {
695             if (errno = EAGAIN || errno == EINTR)
696                 return -1;
697             syslog(LOG_ERR, "Error reading packet: %m");
698             die(1);
699         }
700
701         if (ctrl.len <= 0)
702             return data.len;
703
704         /*
705          * Got a M_PROTO or M_PCPROTO message.  Huh?
706          */
707         if (debug)
708             syslog(LOG_DEBUG, "got ctrl msg len=%d", ctrl.len);
709
710     }
711 }
712
713 /*
714  * get_loop_output - get outgoing packets from the ppp device,
715  * and detect when we want to bring the real link up.
716  * Return value is 1 if we need to bring up the link, 0 otherwise.
717  */
718 int
719 get_loop_output()
720 {
721     int len;
722     int rv = 0;
723
724     while ((len = read_packet(inpacket_buf)) > 0) {
725         if (loop_frame(inpacket_buf, len))
726             rv = 1;
727     }
728     return rv;
729 }
730
731 /*
732  * ppp_send_config - configure the transmit characteristics of
733  * the ppp interface.
734  */
735 void
736 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
737     int unit, mtu;
738     u_int32_t asyncmap;
739     int pcomp, accomp;
740 {
741     int cf[2];
742     struct ifreq ifr;
743
744     link_mtu = mtu;
745     if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
746         if (hungup && errno == ENXIO)
747             return;
748         syslog(LOG_ERR, "Couldn't set MTU: %m");
749     }
750     if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
751         syslog(LOG_ERR, "Couldn't set transmit ACCM: %m");
752     }
753     cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
754     cf[1] = COMP_PROT | COMP_AC;
755     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
756         syslog(LOG_ERR, "Couldn't set prot/AC compression: %m");
757     }
758
759     /* set mtu for ip as well */
760     memset(&ifr, 0, sizeof(ifr));
761     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
762     ifr.ifr_metric = link_mtu;
763     if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
764         syslog(LOG_ERR, "Couldn't set IP MTU: %m");
765     }
766 }
767
768 /*
769  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
770  */
771 void
772 ppp_set_xaccm(unit, accm)
773     int unit;
774     ext_accm accm;
775 {
776     if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
777         if (!hungup || errno != ENXIO)
778             syslog(LOG_WARNING, "Couldn't set extended ACCM: %m");
779     }
780 }
781
782 /*
783  * ppp_recv_config - configure the receive-side characteristics of
784  * the ppp interface.
785  */
786 void
787 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
788     int unit, mru;
789     u_int32_t asyncmap;
790     int pcomp, accomp;
791 {
792     int cf[2];
793
794     link_mru = mru;
795     if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
796         if (hungup && errno == ENXIO)
797             return;
798         syslog(LOG_ERR, "Couldn't set MRU: %m");
799     }
800     if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
801         syslog(LOG_ERR, "Couldn't set receive ACCM: %m");
802     }
803     cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
804     cf[1] = DECOMP_PROT | DECOMP_AC;
805     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
806         syslog(LOG_ERR, "Couldn't set prot/AC decompression: %m");
807     }
808 }
809
810 /*
811  * ccp_test - ask kernel whether a given compression method
812  * is acceptable for use.
813  */
814 int
815 ccp_test(unit, opt_ptr, opt_len, for_transmit)
816     int unit, opt_len, for_transmit;
817     u_char *opt_ptr;
818 {
819     if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
820                  opt_ptr, opt_len, 0) >= 0)
821         return 1;
822     return (errno == ENOSR)? 0: -1;
823 }
824
825 /*
826  * ccp_flags_set - inform kernel about the current state of CCP.
827  */
828 void
829 ccp_flags_set(unit, isopen, isup)
830     int unit, isopen, isup;
831 {
832     int cf[2];
833
834     cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
835     cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
836     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
837         if (!hungup || errno != ENXIO)
838             syslog(LOG_ERR, "Couldn't set kernel CCP state: %m");
839     }
840 }
841
842 /*
843  * get_idle_time - return how long the link has been idle.
844  */
845 int
846 get_idle_time(u, ip)
847     int u;
848     struct ppp_idle *ip;
849 {
850     return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
851 }
852
853
854 /*
855  * ccp_fatal_error - returns 1 if decompression was disabled as a
856  * result of an error detected after decompression of a packet,
857  * 0 otherwise.  This is necessary because of patent nonsense.
858  */
859 int
860 ccp_fatal_error(unit)
861     int unit;
862 {
863     int cf[2];
864
865     cf[0] = cf[1] = 0;
866     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
867         if (errno != ENXIO && errno != EINVAL)
868             syslog(LOG_ERR, "Couldn't get compression flags: %m");
869         return 0;
870     }
871     return cf[0] & CCP_FATALERROR;
872 }
873
874 /*
875  * sifvjcomp - config tcp header compression
876  */
877 int
878 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
879     int u, vjcomp, xcidcomp, xmaxcid;
880 {
881     int cf[2];
882     char maxcid[2];
883
884     if (vjcomp) {
885         maxcid[0] = xcidcomp;
886         maxcid[1] = 15;         /* XXX should be rmaxcid */
887         if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
888             syslog(LOG_ERR, "Couldn't initialize VJ compression: %m");
889         }
890     }
891
892     cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0)  /* XXX this is wrong */
893         + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
894     cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
895     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
896         if (vjcomp)
897             syslog(LOG_ERR, "Couldn't enable VJ compression: %m");
898     }
899
900     return 1;
901 }
902
903 /*
904  * sifup - Config the interface up and enable IP packets to pass.
905  */
906 int
907 sifup(u)
908     int u;
909 {
910     struct ifreq ifr;
911
912     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
913     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
914         syslog(LOG_ERR, "Couldn't mark interface up (get): %m");
915         return 0;
916     }
917     ifr.ifr_flags |= IFF_UP;
918     if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
919         syslog(LOG_ERR, "Couldn't mark interface up (set): %m");
920         return 0;
921     }
922     if_is_up = 1;
923     return 1;
924 }
925
926 /*
927  * sifdown - Config the interface down and disable IP.
928  */
929 int
930 sifdown(u)
931     int u;
932 {
933     struct ifreq ifr;
934
935     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
936     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
937         syslog(LOG_ERR, "Couldn't mark interface down (get): %m");
938         return 0;
939     }
940     if ((ifr.ifr_flags & IFF_UP) != 0) {
941         ifr.ifr_flags &= ~IFF_UP;
942         if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
943             syslog(LOG_ERR, "Couldn't mark interface down (set): %m");
944             return 0;
945         }
946     }
947     if_is_up = 0;
948     return 1;
949 }
950
951 /*
952  * sifnpmode - Set the mode for handling packets for a given NP.
953  */
954 int
955 sifnpmode(u, proto, mode)
956     int u;
957     int proto;
958     enum NPmode mode;
959 {
960     int npi[2];
961
962     npi[0] = proto;
963     npi[1] = (int) mode;
964     if (strioctl(pppfd, PPPIO_NPMODE, npi, 2 * sizeof(int), 0) < 0) {
965         syslog(LOG_ERR, "ioctl(set NP %d mode to %d): %m", proto, mode);
966         return 0;
967     }
968     return 1;
969 }
970
971 #define INET_ADDR(x)    (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
972
973 /*
974  * sifaddr - Config the interface IP addresses and netmask.
975  */
976 int
977 sifaddr(u, o, h, m)
978     int u;
979     u_int32_t o, h, m;
980 {
981     struct ifreq ifr;
982
983     memset(&ifr, 0, sizeof(ifr));
984     strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
985     ifr.ifr_addr.sa_family = AF_INET;
986     INET_ADDR(ifr.ifr_addr) = m;
987     if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
988         syslog(LOG_ERR, "Couldn't set IP netmask: %m");
989     }
990     ifr.ifr_addr.sa_family = AF_INET;
991     INET_ADDR(ifr.ifr_addr) = o;
992     if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
993         syslog(LOG_ERR, "Couldn't set local IP address: %m");
994     }
995     ifr.ifr_dstaddr.sa_family = AF_INET;
996     INET_ADDR(ifr.ifr_dstaddr) = h;
997     if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
998         syslog(LOG_ERR, "Couldn't set remote IP address: %m");
999     }
1000 #if 0   /* now done in ppp_send_config */
1001     ifr.ifr_metric = link_mtu;
1002     if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
1003         syslog(LOG_ERR, "Couldn't set IP MTU: %m");
1004     }
1005 #endif
1006     ifaddrs[0] = o;
1007     ifaddrs[1] = h;
1008
1009     return 1;
1010 }
1011
1012 /*
1013  * cifaddr - Clear the interface IP addresses, and delete routes
1014  * through the interface if possible.
1015  */
1016 int
1017 cifaddr(u, o, h)
1018     int u;
1019     u_int32_t o, h;
1020 {
1021     struct rtentry rt;
1022
1023     bzero(&rt, sizeof(rt));
1024     rt.rt_dst.sa_family = AF_INET;
1025     INET_ADDR(rt.rt_dst) = h;
1026     rt.rt_gateway.sa_family = AF_INET;
1027     INET_ADDR(rt.rt_gateway) = o;
1028     rt.rt_flags = RTF_HOST;
1029     if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1030         syslog(LOG_ERR, "Couldn't delete route through interface: %m");
1031     ifaddrs[0] = 0;
1032     return 1;
1033 }
1034
1035 /*
1036  * sifdefaultroute - assign a default route through the address given.
1037  */
1038 int
1039 sifdefaultroute(u, l, g)
1040     int u;
1041     u_int32_t l, g;
1042 {
1043     struct rtentry rt;
1044
1045     bzero(&rt, sizeof(rt));
1046     rt.rt_dst.sa_family = AF_INET;
1047     INET_ADDR(rt.rt_dst) = 0;
1048     rt.rt_gateway.sa_family = AF_INET;
1049     INET_ADDR(rt.rt_gateway) = g;
1050     rt.rt_flags = RTF_GATEWAY;
1051
1052     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1053         syslog(LOG_ERR, "Can't add default route: %m");
1054         return 0;
1055     }
1056
1057     default_route_gateway = g;
1058     return 1;
1059 }
1060
1061 /*
1062  * cifdefaultroute - delete a default route through the address given.
1063  */
1064 int
1065 cifdefaultroute(u, l, g)
1066     int u;
1067     u_int32_t l, g;
1068 {
1069     struct rtentry rt;
1070
1071     bzero(&rt, sizeof(rt));
1072     rt.rt_dst.sa_family = AF_INET;
1073     INET_ADDR(rt.rt_dst) = 0;
1074     rt.rt_gateway.sa_family = AF_INET;
1075     INET_ADDR(rt.rt_gateway) = g;
1076     rt.rt_flags = RTF_GATEWAY;
1077
1078     if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
1079         syslog(LOG_ERR, "Can't delete default route: %m");
1080         return 0;
1081     }
1082
1083     default_route_gateway = 0;
1084     return 1;
1085 }
1086
1087 /*
1088  * sifproxyarp - Make a proxy ARP entry for the peer.
1089  */
1090 int
1091 sifproxyarp(unit, hisaddr)
1092     int unit;
1093     u_int32_t hisaddr;
1094 {
1095     struct arpreq arpreq;
1096
1097     bzero(&arpreq, sizeof(arpreq));
1098     if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1099         return 0;
1100
1101     arpreq.arp_pa.sa_family = AF_INET;
1102     INET_ADDR(arpreq.arp_pa) = hisaddr;
1103     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1104     if (ioctl(sockfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1105         syslog(LOG_ERR, "Couldn't set proxy ARP entry: %m");
1106         return 0;
1107     }
1108
1109     proxy_arp_addr = hisaddr;
1110     return 1;
1111 }
1112
1113 /*
1114  * cifproxyarp - Delete the proxy ARP entry for the peer.
1115  */
1116 int
1117 cifproxyarp(unit, hisaddr)
1118     int unit;
1119     u_int32_t hisaddr;
1120 {
1121     struct arpreq arpreq;
1122
1123     bzero(&arpreq, sizeof(arpreq));
1124     arpreq.arp_pa.sa_family = AF_INET;
1125     INET_ADDR(arpreq.arp_pa) = hisaddr;
1126     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1127         syslog(LOG_ERR, "Couldn't delete proxy ARP entry: %m");
1128         return 0;
1129     }
1130
1131     proxy_arp_addr = 0;
1132     return 1;
1133 }
1134
1135 /*
1136  * get_ether_addr - get the hardware address of an interface on the
1137  * the same subnet as ipaddr.
1138  */
1139 #define MAX_IFS         32
1140
1141 static int
1142 get_ether_addr(ipaddr, hwaddr)
1143     u_int32_t ipaddr;
1144     struct sockaddr *hwaddr;
1145 {
1146     struct ifreq *ifr, *ifend;
1147     u_int32_t ina, mask;
1148     struct ifreq ifreq;
1149     struct ifconf ifc;
1150     struct ifreq ifs[MAX_IFS];
1151     int nit_fd;
1152
1153     ifc.ifc_len = sizeof(ifs);
1154     ifc.ifc_req = ifs;
1155     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1156         syslog(LOG_ERR, "ioctl(SIOCGIFCONF): %m");
1157         return 0;
1158     }
1159
1160     /*
1161      * Scan through looking for an interface with an Internet
1162      * address on the same subnet as `ipaddr'.
1163      */
1164     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1165     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1166             ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1167         if (ifr->ifr_addr.sa_family == AF_INET) {
1168
1169             /*
1170              * Check that the interface is up, and not point-to-point
1171              * or loopback.
1172              */
1173             strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1174             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1175                 continue;
1176             if ((ifreq.ifr_flags &
1177                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1178                  != (IFF_UP|IFF_BROADCAST))
1179                 continue;
1180
1181             /*
1182              * Get its netmask and check that it's on the right subnet.
1183              */
1184             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1185                 continue;
1186             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1187             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1188             if ((ipaddr & mask) != (ina & mask))
1189                 continue;
1190
1191             break;
1192         }
1193     }
1194
1195     if (ifr >= ifend)
1196         return 0;
1197     syslog(LOG_INFO, "found interface %s for proxy arp", ifr->ifr_name);
1198
1199     /*
1200      * Grab the physical address for this interface.
1201      */
1202     if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) {
1203         syslog(LOG_ERR, "Couldn't open /dev/nit: %m");
1204         return 0;
1205     }
1206     strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1207     if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0
1208         || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) {
1209         syslog(LOG_ERR, "Couldn't get hardware address for %s: %m",
1210                ifreq.ifr_name);
1211         close(nit_fd);
1212         return 0;
1213     }
1214
1215     hwaddr->sa_family = AF_UNSPEC;
1216     memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6);
1217     close(nit_fd);
1218     return 1;
1219 }
1220
1221 /*
1222  * have_route_to - determine if the system has any route to
1223  * a given IP address.
1224  * For demand mode to work properly, we have to ignore routes
1225  * through our own interface.
1226  */
1227 int have_route_to(addr)
1228     u_int32_t addr;
1229 {
1230     return -1;
1231 }
1232
1233 #define WTMPFILE        "/usr/adm/wtmp"
1234
1235 void
1236 logwtmp(line, name, host)
1237     const char *line, *name, *host;
1238 {
1239     int fd;
1240     struct stat buf;
1241     struct utmp ut;
1242
1243     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1244         return;
1245     if (!fstat(fd, &buf)) {
1246         (void)strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1247         (void)strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1248         (void)strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1249         (void)time(&ut.ut_time);
1250         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1251             (void)ftruncate(fd, buf.st_size);
1252     }
1253     close(fd);
1254 }
1255
1256 /*
1257  * Return user specified netmask, modified by any mask we might determine
1258  * for address `addr' (in network byte order).
1259  * Here we scan through the system's list of interfaces, looking for
1260  * any non-point-to-point interfaces which might appear to be on the same
1261  * network as `addr'.  If we find any, we OR in their netmask to the
1262  * user-specified netmask.
1263  */
1264 u_int32_t
1265 GetMask(addr)
1266     u_int32_t addr;
1267 {
1268     u_int32_t mask, nmask, ina;
1269     struct ifreq *ifr, *ifend, ifreq;
1270     struct ifconf ifc;
1271
1272     addr = ntohl(addr);
1273     if (IN_CLASSA(addr))        /* determine network mask for address class */
1274         nmask = IN_CLASSA_NET;
1275     else if (IN_CLASSB(addr))
1276         nmask = IN_CLASSB_NET;
1277     else
1278         nmask = IN_CLASSC_NET;
1279     /* class D nets are disallowed by bad_ip_adrs */
1280     mask = netmask | htonl(nmask);
1281
1282     /*
1283      * Scan through the system's network interfaces.
1284      */
1285     ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1286     ifc.ifc_req = alloca(ifc.ifc_len);
1287     if (ifc.ifc_req == 0)
1288         return mask;
1289     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1290         syslog(LOG_WARNING, "Couldn't get system interface list: %m");
1291         return mask;
1292     }
1293     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1294     for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1295         /*
1296          * Check the interface's internet address.
1297          */
1298         if (ifr->ifr_addr.sa_family != AF_INET)
1299             continue;
1300         ina = INET_ADDR(ifr->ifr_addr);
1301         if ((ntohl(ina) & nmask) != (addr & nmask))
1302             continue;
1303         /*
1304          * Check that the interface is up, and not point-to-point or loopback.
1305          */
1306         strncpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1307         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1308             continue;
1309         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1310             != IFF_UP)
1311             continue;
1312         /*
1313          * Get its netmask and OR it into our mask.
1314          */
1315         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1316             continue;
1317         mask |= INET_ADDR(ifreq.ifr_addr);
1318     }
1319
1320     return mask;
1321 }
1322
1323 static int
1324 strioctl(fd, cmd, ptr, ilen, olen)
1325     int fd, cmd, ilen, olen;
1326     void *ptr;
1327 {
1328     struct strioctl str;
1329
1330     str.ic_cmd = cmd;
1331     str.ic_timout = 0;
1332     str.ic_len = ilen;
1333     str.ic_dp = ptr;
1334     if (ioctl(fd, I_STR, &str) == -1)
1335         return -1;
1336     if (str.ic_len != olen)
1337         syslog(LOG_DEBUG, "strioctl: expected %d bytes, got %d for cmd %x\n",
1338                olen, str.ic_len, cmd);
1339     return 0;
1340 }
1341
1342 /*
1343  * Use the hostid as part of the random number seed.
1344  */
1345 int
1346 get_host_seed()
1347 {
1348     return gethostid();
1349 }
1350
1351 /*
1352  * Code for locking/unlocking the serial device.
1353  * This code is derived from chat.c.
1354  */
1355
1356 #if !defined(HDB) && !defined(SUNOS3)
1357 #define HDB     1               /* ascii lock files are the default */
1358 #endif
1359
1360 #ifndef LOCK_DIR
1361 # if HDB
1362 #  define       PIDSTRING
1363 #  define       LOCK_PREFIX     "/usr/spool/locks/LCK.."
1364 # else /* HDB */
1365 #  define       LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1366 # endif /* HDB */
1367 #endif /* LOCK_DIR */
1368
1369 static char *lock_file;         /* name of lock file created */
1370
1371 /*
1372  * lock - create a lock file for the named device.
1373  */
1374 int
1375 lock(dev)
1376     char *dev;
1377 {
1378     char hdb_lock_buffer[12];
1379     int fd, pid, n;
1380     char *p;
1381
1382     if ((p = strrchr(dev, '/')) != NULL)
1383         dev = p + 1;
1384     lock_file = malloc(strlen(LOCK_PREFIX) + strlen(dev) + 1);
1385     if (lock_file == NULL)
1386         novm("lock file name");
1387     strcat(strcpy(lock_file, LOCK_PREFIX), dev);
1388
1389     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1390         if (errno == EEXIST
1391             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1392             /* Read the lock file to find out who has the device locked */
1393 #ifdef PIDSTRING
1394             n = read(fd, hdb_lock_buffer, 11);
1395             if (n > 0) {
1396                 hdb_lock_buffer[n] = 0;
1397                 pid = atoi(hdb_lock_buffer);
1398             }
1399 #else
1400             n = read(fd, &pid, sizeof(pid));
1401 #endif
1402             if (n <= 0) {
1403                 syslog(LOG_ERR, "Can't read pid from lock file %s", lock_file);
1404                 close(fd);
1405             } else {
1406                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1407                     /* pid no longer exists - remove the lock file */
1408                     if (unlink(lock_file) == 0) {
1409                         close(fd);
1410                         syslog(LOG_NOTICE, "Removed stale lock on %s (pid %d)",
1411                                dev, pid);
1412                         continue;
1413                     } else
1414                         syslog(LOG_WARNING, "Couldn't remove stale lock on %s",
1415                                dev);
1416                 } else
1417                     syslog(LOG_NOTICE, "Device %s is locked by pid %d",
1418                            dev, pid);
1419             }
1420             close(fd);
1421         } else
1422             syslog(LOG_ERR, "Can't create lock file %s: %m", lock_file);
1423         free(lock_file);
1424         lock_file = NULL;
1425         return -1;
1426     }
1427
1428 #ifdef PIDSTRING
1429     sprintf(hdb_lock_buffer, "%10d\n", getpid());
1430     write(fd, hdb_lock_buffer, 11);
1431 #else
1432     pid = getpid();
1433     write(fd, &pid, sizeof pid);
1434 #endif
1435
1436     close(fd);
1437     return 0;
1438 }
1439
1440 /*
1441  * unlock - remove our lockfile
1442  */
1443 void
1444 unlock()
1445 {
1446     if (lock_file) {
1447         unlink(lock_file);
1448         free(lock_file);
1449         lock_file = NULL;
1450     }
1451 }
1452
1453 /*
1454  * SunOS doesn't have strtoul :-(
1455  */
1456 unsigned long
1457 strtoul(str, ptr, base)
1458     char *str, **ptr;
1459     int base;
1460 {
1461     return (unsigned long) strtol(str, ptr, base);
1462 }
1463
1464 /*
1465  * Or strerror :-(
1466  */
1467 extern char *sys_errlist[];
1468 extern int sys_nerr;
1469
1470 char *
1471 strerror(n)
1472     int n;
1473 {
1474     static char unknown[32];
1475
1476     if (n > 0 && n < sys_nerr)
1477         return sys_errlist[n];
1478     sprintf(unknown, "Error %d", n);
1479     return unknown;
1480 }