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