]> git.ozlabs.org Git - ppp.git/blob - pppd/sys-sunos4.c
cope with EAGAIN return from poll
[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.25 1999/12/23 01:38: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         if (errno != EAGAIN)
619             fatal("poll: %m");
620         /* we can get EAGAIN on a heavily loaded system,
621          * just wait a short time and try again. */
622         usleep(50000);
623     }
624 }
625
626 /*
627  * add_fd - add an fd to the set that wait_input waits for.
628  */
629 void add_fd(fd)
630     int fd;
631 {
632     int n;
633
634     for (n = 0; n < n_pollfds; ++n)
635         if (pollfds[n].fd == fd)
636             return;
637     if (n_pollfds < MAX_POLLFDS) {
638         pollfds[n_pollfds].fd = fd;
639         pollfds[n_pollfds].events = POLLIN | POLLPRI | POLLHUP;
640         ++n_pollfds;
641     } else
642         error("Too many inputs!");
643 }
644
645 /*
646  * remove_fd - remove an fd from the set that wait_input waits for.
647  */
648 void remove_fd(fd)
649     int fd;
650 {
651     int n;
652
653     for (n = 0; n < n_pollfds; ++n) {
654         if (pollfds[n].fd == fd) {
655             while (++n < n_pollfds)
656                 pollfds[n-1] = pollfds[n];
657             --n_pollfds;
658             break;
659         }
660     }
661 }
662
663 #if 0
664 /*
665  * wait_loop_output - wait until there is data available on the
666  * loopback, for the length of time specified by *timo (indefinite
667  * if timo is NULL).
668  */
669 void
670 wait_loop_output(timo)
671     struct timeval *timo;
672 {
673     wait_input(timo);
674 }
675
676 /*
677  * wait_time - wait for a given length of time or until a
678  * signal is received.
679  */
680 void
681 wait_time(timo)
682     struct timeval *timo;
683 {
684     int n;
685
686     n = select(0, NULL, NULL, NULL, timo);
687     if (n < 0 && errno != EINTR)
688         fatal("select: %m");
689 }
690 #endif
691
692 /*
693  * read_packet - get a PPP packet from the serial device.
694  */
695 int
696 read_packet(buf)
697     u_char *buf;
698 {
699     struct strbuf ctrl, data;
700     int flags, len;
701     unsigned char ctrlbuf[64];
702
703     for (;;) {
704         data.maxlen = PPP_MRU + PPP_HDRLEN;
705         data.buf = (caddr_t) buf;
706         ctrl.maxlen = sizeof(ctrlbuf);
707         ctrl.buf = (caddr_t) ctrlbuf;
708         flags = 0;
709         len = getmsg(pppfd, &ctrl, &data, &flags);
710         if (len < 0) {
711             if (errno == EAGAIN || errno == EINTR)
712                 return -1;
713             fatal("Error reading packet: %m");
714         }
715
716         if (ctrl.len <= 0)
717             return data.len;
718
719         /*
720          * Got a M_PROTO or M_PCPROTO message.  Huh?
721          */
722         if (debug)
723             dbglog("got ctrl msg len=%d", ctrl.len);
724
725     }
726 }
727
728 /*
729  * get_loop_output - get outgoing packets from the ppp device,
730  * and detect when we want to bring the real link up.
731  * Return value is 1 if we need to bring up the link, 0 otherwise.
732  */
733 int
734 get_loop_output()
735 {
736     int len;
737     int rv = 0;
738
739     while ((len = read_packet(inpacket_buf)) > 0) {
740         if (loop_frame(inpacket_buf, len))
741             rv = 1;
742     }
743     return rv;
744 }
745
746 /*
747  * ppp_send_config - configure the transmit characteristics of
748  * the ppp interface.
749  */
750 void
751 ppp_send_config(unit, mtu, asyncmap, pcomp, accomp)
752     int unit, mtu;
753     u_int32_t asyncmap;
754     int pcomp, accomp;
755 {
756     int cf[2];
757     struct ifreq ifr;
758
759     link_mtu = mtu;
760     if (strioctl(pppfd, PPPIO_MTU, &mtu, sizeof(mtu), 0) < 0) {
761         if (hungup && errno == ENXIO)
762             return;
763         error("Couldn't set MTU: %m");
764     }
765     if (strioctl(pppfd, PPPIO_XACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
766         error("Couldn't set transmit ACCM: %m");
767     }
768     cf[0] = (pcomp? COMP_PROT: 0) + (accomp? COMP_AC: 0);
769     cf[1] = COMP_PROT | COMP_AC;
770     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
771         error("Couldn't set prot/AC compression: %m");
772     }
773
774     /* set mtu for ip as well */
775     memset(&ifr, 0, sizeof(ifr));
776     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
777     ifr.ifr_metric = link_mtu;
778     if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
779         error("Couldn't set IP MTU: %m");
780     }
781 }
782
783 /*
784  * ppp_set_xaccm - set the extended transmit ACCM for the interface.
785  */
786 void
787 ppp_set_xaccm(unit, accm)
788     int unit;
789     ext_accm accm;
790 {
791     if (strioctl(pppfd, PPPIO_XACCM, accm, sizeof(ext_accm), 0) < 0) {
792         if (!hungup || errno != ENXIO)
793             warn("Couldn't set extended ACCM: %m");
794     }
795 }
796
797 /*
798  * ppp_recv_config - configure the receive-side characteristics of
799  * the ppp interface.
800  */
801 void
802 ppp_recv_config(unit, mru, asyncmap, pcomp, accomp)
803     int unit, mru;
804     u_int32_t asyncmap;
805     int pcomp, accomp;
806 {
807     int cf[2];
808
809     link_mru = mru;
810     if (strioctl(pppfd, PPPIO_MRU, &mru, sizeof(mru), 0) < 0) {
811         if (hungup && errno == ENXIO)
812             return;
813         error("Couldn't set MRU: %m");
814     }
815     if (strioctl(pppfd, PPPIO_RACCM, &asyncmap, sizeof(asyncmap), 0) < 0) {
816         error("Couldn't set receive ACCM: %m");
817     }
818     cf[0] = (pcomp? DECOMP_PROT: 0) + (accomp? DECOMP_AC: 0);
819     cf[1] = DECOMP_PROT | DECOMP_AC;
820     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
821         error("Couldn't set prot/AC decompression: %m");
822     }
823 }
824
825 /*
826  * ccp_test - ask kernel whether a given compression method
827  * is acceptable for use.
828  */
829 int
830 ccp_test(unit, opt_ptr, opt_len, for_transmit)
831     int unit, opt_len, for_transmit;
832     u_char *opt_ptr;
833 {
834     if (strioctl(pppfd, (for_transmit? PPPIO_XCOMP: PPPIO_RCOMP),
835                  opt_ptr, opt_len, 0) >= 0)
836         return 1;
837     return (errno == ENOSR)? 0: -1;
838 }
839
840 /*
841  * ccp_flags_set - inform kernel about the current state of CCP.
842  */
843 void
844 ccp_flags_set(unit, isopen, isup)
845     int unit, isopen, isup;
846 {
847     int cf[2];
848
849     cf[0] = (isopen? CCP_ISOPEN: 0) + (isup? CCP_ISUP: 0);
850     cf[1] = CCP_ISOPEN | CCP_ISUP | CCP_ERROR | CCP_FATALERROR;
851     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
852         if (!hungup || errno != ENXIO)
853             error("Couldn't set kernel CCP state: %m");
854     }
855 }
856
857 /*
858  * get_idle_time - return how long the link has been idle.
859  */
860 int
861 get_idle_time(u, ip)
862     int u;
863     struct ppp_idle *ip;
864 {
865     return strioctl(pppfd, PPPIO_GIDLE, ip, 0, sizeof(struct ppp_idle)) >= 0;
866 }
867
868 /*
869  * get_ppp_stats - return statistics for the link.
870  */
871 int
872 get_ppp_stats(u, stats)
873     int u;
874     struct pppd_stats *stats;
875 {
876     struct ppp_stats s;
877
878     if (strioctl(pppfd, PPPIO_GETSTAT, &s, 0, sizeof(s)) < 0) {
879         error("Couldn't get link statistics: %m");
880         return 0;
881     }
882     stats->bytes_in = s.p.ppp_ibytes;
883     stats->bytes_out = s.p.ppp_obytes;
884     return 1;
885 }
886
887
888 /*
889  * ccp_fatal_error - returns 1 if decompression was disabled as a
890  * result of an error detected after decompression of a packet,
891  * 0 otherwise.  This is necessary because of patent nonsense.
892  */
893 int
894 ccp_fatal_error(unit)
895     int unit;
896 {
897     int cf[2];
898
899     cf[0] = cf[1] = 0;
900     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
901         if (errno != ENXIO && errno != EINVAL)
902             error("Couldn't get compression flags: %m");
903         return 0;
904     }
905     return cf[0] & CCP_FATALERROR;
906 }
907
908 /*
909  * sifvjcomp - config tcp header compression
910  */
911 int
912 sifvjcomp(u, vjcomp, xcidcomp, xmaxcid)
913     int u, vjcomp, xcidcomp, xmaxcid;
914 {
915     int cf[2];
916     char maxcid[2];
917
918     if (vjcomp) {
919         maxcid[0] = xcidcomp;
920         maxcid[1] = 15;         /* XXX should be rmaxcid */
921         if (strioctl(pppfd, PPPIO_VJINIT, maxcid, sizeof(maxcid), 0) < 0) {
922             error("Couldn't initialize VJ compression: %m");
923         }
924     }
925
926     cf[0] = (vjcomp? COMP_VJC + DECOMP_VJC: 0)  /* XXX this is wrong */
927         + (xcidcomp? COMP_VJCCID + DECOMP_VJCCID: 0);
928     cf[1] = COMP_VJC + DECOMP_VJC + COMP_VJCCID + DECOMP_VJCCID;
929     if (strioctl(pppfd, PPPIO_CFLAGS, cf, sizeof(cf), sizeof(int)) < 0) {
930         if (vjcomp)
931             error("Couldn't enable VJ compression: %m");
932     }
933
934     return 1;
935 }
936
937 /*
938  * sifup - Config the interface up and enable IP packets to pass.
939  */
940 int
941 sifup(u)
942     int u;
943 {
944     struct ifreq ifr;
945
946     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
947     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
948         error("Couldn't mark interface up (get): %m");
949         return 0;
950     }
951     ifr.ifr_flags |= IFF_UP;
952     if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
953         error("Couldn't mark interface up (set): %m");
954         return 0;
955     }
956     if_is_up = 1;
957     return 1;
958 }
959
960 /*
961  * sifdown - Config the interface down and disable IP.
962  */
963 int
964 sifdown(u)
965     int u;
966 {
967     struct ifreq ifr;
968
969     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
970     if (ioctl(sockfd, SIOCGIFFLAGS, &ifr) < 0) {
971         error("Couldn't mark interface down (get): %m");
972         return 0;
973     }
974     if ((ifr.ifr_flags & IFF_UP) != 0) {
975         ifr.ifr_flags &= ~IFF_UP;
976         if (ioctl(sockfd, SIOCSIFFLAGS, &ifr) < 0) {
977             error("Couldn't mark interface down (set): %m");
978             return 0;
979         }
980     }
981     if_is_up = 0;
982     return 1;
983 }
984
985 /*
986  * sifnpmode - Set the mode for handling packets for a given NP.
987  */
988 int
989 sifnpmode(u, proto, mode)
990     int u;
991     int proto;
992     enum NPmode mode;
993 {
994     int npi[2];
995
996     npi[0] = proto;
997     npi[1] = (int) mode;
998     if (strioctl(pppfd, PPPIO_NPMODE, npi, 2 * sizeof(int), 0) < 0) {
999         error("ioctl(set NP %d mode to %d): %m", proto, mode);
1000         return 0;
1001     }
1002     return 1;
1003 }
1004
1005 #define INET_ADDR(x)    (((struct sockaddr_in *) &(x))->sin_addr.s_addr)
1006
1007 /*
1008  * sifaddr - Config the interface IP addresses and netmask.
1009  */
1010 int
1011 sifaddr(u, o, h, m)
1012     int u;
1013     u_int32_t o, h, m;
1014 {
1015     struct ifreq ifr;
1016
1017     memset(&ifr, 0, sizeof(ifr));
1018     strlcpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
1019     ifr.ifr_addr.sa_family = AF_INET;
1020     INET_ADDR(ifr.ifr_addr) = m;
1021     if (ioctl(sockfd, SIOCSIFNETMASK, &ifr) < 0) {
1022         error("Couldn't set IP netmask: %m");
1023     }
1024     ifr.ifr_addr.sa_family = AF_INET;
1025     INET_ADDR(ifr.ifr_addr) = o;
1026     if (ioctl(sockfd, SIOCSIFADDR, &ifr) < 0) {
1027         error("Couldn't set local IP address: %m");
1028     }
1029     ifr.ifr_dstaddr.sa_family = AF_INET;
1030     INET_ADDR(ifr.ifr_dstaddr) = h;
1031     if (ioctl(sockfd, SIOCSIFDSTADDR, &ifr) < 0) {
1032         error("Couldn't set remote IP address: %m");
1033     }
1034 #if 0   /* now done in ppp_send_config */
1035     ifr.ifr_metric = link_mtu;
1036     if (ioctl(sockfd, SIOCSIFMTU, &ifr) < 0) {
1037         error("Couldn't set IP MTU: %m");
1038     }
1039 #endif
1040     ifaddrs[0] = o;
1041     ifaddrs[1] = h;
1042
1043     return 1;
1044 }
1045
1046 /*
1047  * cifaddr - Clear the interface IP addresses, and delete routes
1048  * through the interface if possible.
1049  */
1050 int
1051 cifaddr(u, o, h)
1052     int u;
1053     u_int32_t o, h;
1054 {
1055     struct rtentry rt;
1056
1057     bzero(&rt, sizeof(rt));
1058     rt.rt_dst.sa_family = AF_INET;
1059     INET_ADDR(rt.rt_dst) = h;
1060     rt.rt_gateway.sa_family = AF_INET;
1061     INET_ADDR(rt.rt_gateway) = o;
1062     rt.rt_flags = RTF_HOST;
1063     if (ioctl(sockfd, SIOCDELRT, &rt) < 0)
1064         error("Couldn't delete route through interface: %m");
1065     ifaddrs[0] = 0;
1066     return 1;
1067 }
1068
1069 /*
1070  * sifdefaultroute - assign a default route through the address given.
1071  */
1072 int
1073 sifdefaultroute(u, l, g)
1074     int u;
1075     u_int32_t l, g;
1076 {
1077     struct rtentry rt;
1078
1079     bzero(&rt, sizeof(rt));
1080     rt.rt_dst.sa_family = AF_INET;
1081     INET_ADDR(rt.rt_dst) = 0;
1082     rt.rt_gateway.sa_family = AF_INET;
1083     INET_ADDR(rt.rt_gateway) = g;
1084     rt.rt_flags = RTF_GATEWAY;
1085
1086     if (ioctl(sockfd, SIOCADDRT, &rt) < 0) {
1087         error("Can't add default route: %m");
1088         return 0;
1089     }
1090
1091     default_route_gateway = g;
1092     return 1;
1093 }
1094
1095 /*
1096  * cifdefaultroute - delete a default route through the address given.
1097  */
1098 int
1099 cifdefaultroute(u, l, g)
1100     int u;
1101     u_int32_t l, g;
1102 {
1103     struct rtentry rt;
1104
1105     bzero(&rt, sizeof(rt));
1106     rt.rt_dst.sa_family = AF_INET;
1107     INET_ADDR(rt.rt_dst) = 0;
1108     rt.rt_gateway.sa_family = AF_INET;
1109     INET_ADDR(rt.rt_gateway) = g;
1110     rt.rt_flags = RTF_GATEWAY;
1111
1112     if (ioctl(sockfd, SIOCDELRT, &rt) < 0) {
1113         error("Can't delete default route: %m");
1114         return 0;
1115     }
1116
1117     default_route_gateway = 0;
1118     return 1;
1119 }
1120
1121 /*
1122  * sifproxyarp - Make a proxy ARP entry for the peer.
1123  */
1124 int
1125 sifproxyarp(unit, hisaddr)
1126     int unit;
1127     u_int32_t hisaddr;
1128 {
1129     struct arpreq arpreq;
1130
1131     bzero(&arpreq, sizeof(arpreq));
1132     if (!get_ether_addr(hisaddr, &arpreq.arp_ha))
1133         return 0;
1134
1135     arpreq.arp_pa.sa_family = AF_INET;
1136     INET_ADDR(arpreq.arp_pa) = hisaddr;
1137     arpreq.arp_flags = ATF_PERM | ATF_PUBL;
1138     if (ioctl(sockfd, SIOCSARP, (caddr_t) &arpreq) < 0) {
1139         error("Couldn't set proxy ARP entry: %m");
1140         return 0;
1141     }
1142
1143     proxy_arp_addr = hisaddr;
1144     return 1;
1145 }
1146
1147 /*
1148  * cifproxyarp - Delete the proxy ARP entry for the peer.
1149  */
1150 int
1151 cifproxyarp(unit, hisaddr)
1152     int unit;
1153     u_int32_t hisaddr;
1154 {
1155     struct arpreq arpreq;
1156
1157     bzero(&arpreq, sizeof(arpreq));
1158     arpreq.arp_pa.sa_family = AF_INET;
1159     INET_ADDR(arpreq.arp_pa) = hisaddr;
1160     if (ioctl(sockfd, SIOCDARP, (caddr_t)&arpreq) < 0) {
1161         error("Couldn't delete proxy ARP entry: %m");
1162         return 0;
1163     }
1164
1165     proxy_arp_addr = 0;
1166     return 1;
1167 }
1168
1169 /*
1170  * get_ether_addr - get the hardware address of an interface on the
1171  * the same subnet as ipaddr.
1172  */
1173 #define MAX_IFS         32
1174
1175 static int
1176 get_ether_addr(ipaddr, hwaddr)
1177     u_int32_t ipaddr;
1178     struct sockaddr *hwaddr;
1179 {
1180     struct ifreq *ifr, *ifend;
1181     u_int32_t ina, mask;
1182     struct ifreq ifreq;
1183     struct ifconf ifc;
1184     struct ifreq ifs[MAX_IFS];
1185     int nit_fd;
1186
1187     ifc.ifc_len = sizeof(ifs);
1188     ifc.ifc_req = ifs;
1189     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1190         error("ioctl(SIOCGIFCONF): %m");
1191         return 0;
1192     }
1193
1194     /*
1195      * Scan through looking for an interface with an Internet
1196      * address on the same subnet as `ipaddr'.
1197      */
1198     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1199     for (ifr = ifc.ifc_req; ifr < ifend; ifr = (struct ifreq *)
1200             ((char *)&ifr->ifr_addr + sizeof(struct sockaddr))) {
1201         if (ifr->ifr_addr.sa_family == AF_INET) {
1202
1203             /*
1204              * Check that the interface is up, and not point-to-point
1205              * or loopback.
1206              */
1207             strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1208             if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1209                 continue;
1210             if ((ifreq.ifr_flags &
1211                  (IFF_UP|IFF_BROADCAST|IFF_POINTOPOINT|IFF_LOOPBACK|IFF_NOARP))
1212                  != (IFF_UP|IFF_BROADCAST))
1213                 continue;
1214
1215             /*
1216              * Get its netmask and check that it's on the right subnet.
1217              */
1218             if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1219                 continue;
1220             ina = ((struct sockaddr_in *) &ifr->ifr_addr)->sin_addr.s_addr;
1221             mask = ((struct sockaddr_in *) &ifreq.ifr_addr)->sin_addr.s_addr;
1222             if ((ipaddr & mask) != (ina & mask))
1223                 continue;
1224
1225             break;
1226         }
1227     }
1228
1229     if (ifr >= ifend)
1230         return 0;
1231     info("found interface %s for proxy arp", ifr->ifr_name);
1232
1233     /*
1234      * Grab the physical address for this interface.
1235      */
1236     if ((nit_fd = open("/dev/nit", O_RDONLY)) < 0) {
1237         error("Couldn't open /dev/nit: %m");
1238         return 0;
1239     }
1240     strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1241     if (ioctl(nit_fd, NIOCBIND, &ifreq) < 0
1242         || ioctl(nit_fd, SIOCGIFADDR, &ifreq) < 0) {
1243         error("Couldn't get hardware address for %s: %m",
1244                ifreq.ifr_name);
1245         close(nit_fd);
1246         return 0;
1247     }
1248
1249     hwaddr->sa_family = AF_UNSPEC;
1250     memcpy(hwaddr->sa_data, ifreq.ifr_addr.sa_data, 6);
1251     close(nit_fd);
1252     return 1;
1253 }
1254
1255 /*
1256  * have_route_to - determine if the system has any route to
1257  * a given IP address.
1258  * For demand mode to work properly, we have to ignore routes
1259  * through our own interface.
1260  */
1261 int have_route_to(addr)
1262     u_int32_t addr;
1263 {
1264     return -1;
1265 }
1266
1267 #define WTMPFILE        "/usr/adm/wtmp"
1268
1269 void
1270 logwtmp(line, name, host)
1271     const char *line, *name, *host;
1272 {
1273     int fd;
1274     struct stat buf;
1275     struct utmp ut;
1276
1277     if ((fd = open(WTMPFILE, O_WRONLY|O_APPEND, 0)) < 0)
1278         return;
1279     if (!fstat(fd, &buf)) {
1280         strncpy(ut.ut_line, line, sizeof(ut.ut_line));
1281         strncpy(ut.ut_name, name, sizeof(ut.ut_name));
1282         strncpy(ut.ut_host, host, sizeof(ut.ut_host));
1283         (void)time(&ut.ut_time);
1284         if (write(fd, (char *)&ut, sizeof(struct utmp)) != sizeof(struct utmp))
1285             (void)ftruncate(fd, buf.st_size);
1286     }
1287     close(fd);
1288 }
1289
1290 /*
1291  * Return user specified netmask, modified by any mask we might determine
1292  * for address `addr' (in network byte order).
1293  * Here we scan through the system's list of interfaces, looking for
1294  * any non-point-to-point interfaces which might appear to be on the same
1295  * network as `addr'.  If we find any, we OR in their netmask to the
1296  * user-specified netmask.
1297  */
1298 u_int32_t
1299 GetMask(addr)
1300     u_int32_t addr;
1301 {
1302     u_int32_t mask, nmask, ina;
1303     struct ifreq *ifr, *ifend, ifreq;
1304     struct ifconf ifc;
1305
1306     addr = ntohl(addr);
1307     if (IN_CLASSA(addr))        /* determine network mask for address class */
1308         nmask = IN_CLASSA_NET;
1309     else if (IN_CLASSB(addr))
1310         nmask = IN_CLASSB_NET;
1311     else
1312         nmask = IN_CLASSC_NET;
1313     /* class D nets are disallowed by bad_ip_adrs */
1314     mask = netmask | htonl(nmask);
1315
1316     /*
1317      * Scan through the system's network interfaces.
1318      */
1319     ifc.ifc_len = MAX_IFS * sizeof(struct ifreq);
1320     ifc.ifc_req = alloca(ifc.ifc_len);
1321     if (ifc.ifc_req == 0)
1322         return mask;
1323     if (ioctl(sockfd, SIOCGIFCONF, &ifc) < 0) {
1324         warn("Couldn't get system interface list: %m");
1325         return mask;
1326     }
1327     ifend = (struct ifreq *) (ifc.ifc_buf + ifc.ifc_len);
1328     for (ifr = ifc.ifc_req; ifr < ifend; ++ifr) {
1329         /*
1330          * Check the interface's internet address.
1331          */
1332         if (ifr->ifr_addr.sa_family != AF_INET)
1333             continue;
1334         ina = INET_ADDR(ifr->ifr_addr);
1335         if ((ntohl(ina) & nmask) != (addr & nmask))
1336             continue;
1337         /*
1338          * Check that the interface is up, and not point-to-point or loopback.
1339          */
1340         strlcpy(ifreq.ifr_name, ifr->ifr_name, sizeof(ifreq.ifr_name));
1341         if (ioctl(sockfd, SIOCGIFFLAGS, &ifreq) < 0)
1342             continue;
1343         if ((ifreq.ifr_flags & (IFF_UP|IFF_POINTOPOINT|IFF_LOOPBACK))
1344             != IFF_UP)
1345             continue;
1346         /*
1347          * Get its netmask and OR it into our mask.
1348          */
1349         if (ioctl(sockfd, SIOCGIFNETMASK, &ifreq) < 0)
1350             continue;
1351         mask |= INET_ADDR(ifreq.ifr_addr);
1352     }
1353
1354     return mask;
1355 }
1356
1357 static int
1358 strioctl(fd, cmd, ptr, ilen, olen)
1359     int fd, cmd, ilen, olen;
1360     void *ptr;
1361 {
1362     struct strioctl str;
1363
1364     str.ic_cmd = cmd;
1365     str.ic_timout = 0;
1366     str.ic_len = ilen;
1367     str.ic_dp = ptr;
1368     if (ioctl(fd, I_STR, &str) == -1)
1369         return -1;
1370     if (str.ic_len != olen)
1371         dbglog("strioctl: expected %d bytes, got %d for cmd %x\n",
1372                olen, str.ic_len, cmd);
1373     return 0;
1374 }
1375
1376 /*
1377  * Use the hostid as part of the random number seed.
1378  */
1379 int
1380 get_host_seed()
1381 {
1382     return gethostid();
1383 }
1384
1385 #if 0
1386 /*
1387  * Code for locking/unlocking the serial device.
1388  * This code is derived from chat.c.
1389  */
1390
1391 #if !defined(HDB) && !defined(SUNOS3)
1392 #define HDB     1               /* ascii lock files are the default */
1393 #endif
1394
1395 #ifndef LOCK_DIR
1396 # if HDB
1397 #  define       PIDSTRING
1398 #  define       LOCK_PREFIX     "/usr/spool/locks/LCK.."
1399 # else /* HDB */
1400 #  define       LOCK_PREFIX     "/usr/spool/uucp/LCK.."
1401 # endif /* HDB */
1402 #endif /* LOCK_DIR */
1403
1404 static char *lock_file;         /* name of lock file created */
1405
1406 /*
1407  * lock - create a lock file for the named device.
1408  */
1409 int
1410 lock(dev)
1411     char *dev;
1412 {
1413     char hdb_lock_buffer[12];
1414     int fd, pid, n;
1415     char *p;
1416     size_t l;
1417
1418     if ((p = strrchr(dev, '/')) != NULL)
1419         dev = p + 1;
1420     l = strlen(LOCK_PREFIX) + strlen(dev) + 1;
1421     lock_file = malloc(l);
1422     if (lock_file == NULL)
1423         novm("lock file name");
1424     slprintf(lock_file, l, "%s%s", LOCK_PREFIX, dev);
1425
1426     while ((fd = open(lock_file, O_EXCL | O_CREAT | O_RDWR, 0644)) < 0) {
1427         if (errno == EEXIST
1428             && (fd = open(lock_file, O_RDONLY, 0)) >= 0) {
1429             /* Read the lock file to find out who has the device locked */
1430 #ifdef PIDSTRING
1431             n = read(fd, hdb_lock_buffer, 11);
1432             if (n > 0) {
1433                 hdb_lock_buffer[n] = 0;
1434                 pid = atoi(hdb_lock_buffer);
1435             }
1436 #else
1437             n = read(fd, &pid, sizeof(pid));
1438 #endif
1439             if (n <= 0) {
1440                 error("Can't read pid from lock file %s", lock_file);
1441                 close(fd);
1442             } else {
1443                 if (kill(pid, 0) == -1 && errno == ESRCH) {
1444                     /* pid no longer exists - remove the lock file */
1445                     if (unlink(lock_file) == 0) {
1446                         close(fd);
1447                         notice("Removed stale lock on %s (pid %d)",
1448                                dev, pid);
1449                         continue;
1450                     } else
1451                         warn("Couldn't remove stale lock on %s",
1452                                dev);
1453                 } else
1454                     notice("Device %s is locked by pid %d",
1455                            dev, pid);
1456             }
1457             close(fd);
1458         } else
1459             error("Can't create lock file %s: %m", lock_file);
1460         free(lock_file);
1461         lock_file = NULL;
1462         return -1;
1463     }
1464
1465 #ifdef PIDSTRING
1466     slprintf(hdb_lock_buffer, sizeof(hdb_lock_buffer), "%10d\n", getpid());
1467     write(fd, hdb_lock_buffer, 11);
1468 #else
1469     pid = getpid();
1470     write(fd, &pid, sizeof pid);
1471 #endif
1472
1473     close(fd);
1474     return 0;
1475 }
1476
1477 /*
1478  * unlock - remove our lockfile
1479  */
1480 void
1481 unlock()
1482 {
1483     if (lock_file) {
1484         unlink(lock_file);
1485         free(lock_file);
1486         lock_file = NULL;
1487     }
1488 }
1489 #endif /* lock stuff removed */
1490
1491 /*
1492  * get_pty - get a pty master/slave pair and chown the slave side
1493  * to the uid given.  Assumes slave_name points to >= 12 bytes of space.
1494  */
1495 int
1496 get_pty(master_fdp, slave_fdp, slave_name, uid)
1497     int *master_fdp;
1498     int *slave_fdp;
1499     char *slave_name;
1500     int uid;
1501 {
1502     int i, mfd, sfd;
1503     char pty_name[12];
1504     struct termios tios;
1505
1506     sfd = -1;
1507     for (i = 0; i < 64; ++i) {
1508         slprintf(pty_name, sizeof(pty_name), "/dev/pty%c%x",
1509                  'p' + i / 16, i % 16);
1510         mfd = open(pty_name, O_RDWR, 0);
1511         if (mfd >= 0) {
1512             pty_name[5] = 't';
1513             sfd = open(pty_name, O_RDWR | O_NOCTTY, 0);
1514             if (sfd >= 0)
1515                 break;
1516             close(mfd);
1517         }
1518     }
1519     if (sfd < 0)
1520         return 0;
1521
1522     strlcpy(slave_name, pty_name, 12);
1523     *master_fdp = mfd;
1524     *slave_fdp = sfd;
1525     fchown(sfd, uid, -1);
1526     fchmod(sfd, S_IRUSR | S_IWUSR);
1527     if (tcgetattr(sfd, &tios) == 0) {
1528         tios.c_cflag &= ~(CSIZE | CSTOPB | PARENB);
1529         tios.c_cflag |= CS8 | CREAD;
1530         tios.c_iflag  = IGNPAR | CLOCAL;
1531         tios.c_oflag  = 0;
1532         tios.c_lflag  = 0;
1533         if (tcsetattr(sfd, TCSAFLUSH, &tios) < 0)
1534             warn("couldn't set attributes on pty: %m");
1535     } else
1536         warn("couldn't get attributes on pty: %m");
1537
1538     return 1;
1539 }
1540
1541 /*
1542  * SunOS doesn't have strtoul :-(
1543  */
1544 unsigned long
1545 strtoul(str, ptr, base)
1546     char *str, **ptr;
1547     int base;
1548 {
1549     return (unsigned long) strtol(str, ptr, base);
1550 }
1551
1552 /*
1553  * Or strerror :-(
1554  */
1555 extern char *sys_errlist[];
1556 extern int sys_nerr;
1557
1558 char *
1559 strerror(n)
1560     int n;
1561 {
1562     static char unknown[32];
1563
1564     if (n > 0 && n < sys_nerr)
1565         return sys_errlist[n];
1566     slprintf(unknown, sizeof(unknown), "Error %d", n);
1567     return unknown;
1568 }