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