]> git.ozlabs.org Git - ppp.git/blobdiff - pppd/tty.c
Header file reorganization and cleaning up the public API for pppd version 2.5.0...
[ppp.git] / pppd / tty.c
index ad743539ec806c8fcfa2b7cfc9ea95195c063bf8..127878117e6f7146a29cd2cd75606feb2b38853f 100644 (file)
@@ -95,7 +95,8 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 
-#include "pppd.h"
+#include "pppd-private.h"
+#include "options.h"
 #include "fsm.h"
 #include "lcp.h"
 
@@ -111,7 +112,7 @@ static int setdevname(char *, char **, int);
 static int setspeed(char *, char **, int);
 static int setxonxoff(char **);
 static int setescape(char **);
-static void printescape(option_t *, void (*)(void *, char *,...),void *);
+static void printescape(struct option *, void (*)(void *, char *,...),void *);
 static void finish_tty(void);
 static int start_charshunt(int, int);
 static void stop_charshunt(void *, int);
@@ -136,6 +137,8 @@ int locked;                 /* lock() has succeeded */
 struct stat devstat;           /* result of stat() on devnam */
 
 /* option variables */
+char   devnam[MAXPATHLEN];     /* Device name */
+char   ppp_devname[MAXPATHLEN];/* name of PPP tty (maybe ttypx) */
 int    crtscts = 0;            /* Use hardware flow control */
 int    stop_bits = 1;          /* Number of serial port stop bits */
 bool   modem = 1;              /* Use modem control lines */
@@ -164,7 +167,7 @@ extern int privopen;                /* don't lock, open device as root */
 u_int32_t xmit_accm[8];                /* extended transmit ACCM */
 
 /* option descriptors */
-option_t tty_options[] = {
+static struct option tty_options[] = {
     /* device name must be first, or change connect_tty() below! */
     { "device name", o_wild, (void *) &setdevname,
       "Serial port device name",
@@ -261,6 +264,61 @@ struct channel tty_channel = {
        &tty_close_fds
 };
 
+bool
+ppp_sync_serial()
+{
+    return sync_serial;
+}
+
+bool
+ppp_get_modem()
+{
+    return modem;
+}
+
+void
+ppp_set_modem(bool on)
+{
+    modem = on;
+}
+
+bool
+ppp_using_pty()
+{
+    return using_pty;
+}
+
+int
+ppp_set_pppdevnam(const char *name)
+{
+    if (name) {
+        return strlcpy(ppp_devname, name, sizeof(ppp_devname));
+    }
+    return -1;
+}
+
+const char *
+ppp_pppdevnam()
+{
+    return ppp_devname;
+}
+
+const char *
+ppp_devnam()
+{
+    return devnam;
+}
+
+int
+ppp_set_devnam(const char *name)
+{
+    if (name) {
+        return strlcpy(devnam, name, sizeof(devnam));
+    }
+    return -1;
+}
+
+
 /*
  * setspeed - Set the serial port baud rate.
  * If doit is 0, the call is to check whether this option is
@@ -309,12 +367,12 @@ setdevname(char *cp, char **argv, int doit)
        if (stat(cp, &statbuf) < 0) {
                if (!doit)
                        return errno != ENOENT;
-               option_error("Couldn't stat %s: %m", cp);
+               ppp_option_error("Couldn't stat %s: %m", cp);
                return 0;
        }
        if (!S_ISCHR(statbuf.st_mode)) {
                if (doit)
-                       option_error("%s is not a character device", cp);
+                       ppp_option_error("%s is not a character device", cp);
                return 0;
        }
 
@@ -351,13 +409,13 @@ setescape(char **argv)
     while (*p) {
        n = strtol(p, &endp, 16);
        if (p == endp) {
-           option_error("escape parameter contains invalid hex number '%s'",
+           ppp_option_error("escape parameter contains invalid hex number '%s'",
                         p);
            return 0;
        }
        p = endp;
        if (n < 0 || n == 0x5E || n > 0xFF) {
-           option_error("can't escape character 0x%x", n);
+           ppp_option_error("can't escape character 0x%x", n);
            ret = 0;
        } else
            xmit_accm[n >> 5] |= 1 << (n & 0x1F);
@@ -369,7 +427,7 @@ setescape(char **argv)
 }
 
 static void
-printescape(option_t *opt, void (*printer)(void *, char *, ...), void *arg)
+printescape(struct option *opt, void (*printer)(void *, char *, ...), void *arg)
 {
        int n;
        int first = 1;
@@ -394,7 +452,7 @@ printescape(option_t *opt, void (*printer)(void *, char *, ...), void *arg)
  */
 void tty_init(void)
 {
-    add_notifier(&pidchange, maybe_relock, 0);
+    ppp_add_notify(NF_PID_CHANGE, maybe_relock, 0);
     the_channel = &tty_channel;
     xmit_accm[3] = 0x60000000;
 }
@@ -411,7 +469,7 @@ void tty_process_extra_options(void)
        if (default_device) {
                char *p;
                if (!isatty(0) || (p = ttyname(0)) == NULL) {
-                       option_error("no device specified and stdin is not a tty");
+                       ppp_option_error("no device specified and stdin is not a tty");
                        exit(EXIT_OPTION_ERROR);
                }
                strlcpy(devnam, p, MAXPATHLEN);
@@ -441,12 +499,12 @@ tty_check_options(void)
        int fdflags;
 
        if (demand && notty) {
-               option_error("demand-dialling is incompatible with notty");
+               ppp_option_error("demand-dialling is incompatible with notty");
                exit(EXIT_OPTION_ERROR);
        }
        if (demand && connect_script == 0 && ptycommand == NULL
            && pty_socket == NULL) {
-               option_error("connect script is required for demand-dialling\n");
+               ppp_option_error("connect script is required for demand-dialling\n");
                exit(EXIT_OPTION_ERROR);
        }
        /* default holdoff to 0 if no connect script has been given */
@@ -455,16 +513,16 @@ tty_check_options(void)
 
        if (using_pty) {
                if (!default_device) {
-                       option_error("%s option precludes specifying device name",
+                       ppp_option_error("%s option precludes specifying device name",
                                     pty_socket? "socket": notty? "notty": "pty");
                        exit(EXIT_OPTION_ERROR);
                }
                if (ptycommand != NULL && notty) {
-                       option_error("pty option is incompatible with notty option");
+                       ppp_option_error("pty option is incompatible with notty option");
                        exit(EXIT_OPTION_ERROR);
                }
                if (pty_socket != NULL && (ptycommand != NULL || notty)) {
-                       option_error("socket option is incompatible with pty and notty");
+                       ppp_option_error("socket option is incompatible with pty and notty");
                        exit(EXIT_OPTION_ERROR);
                }
                default_device = notty;
@@ -518,14 +576,14 @@ int connect_tty(void)
         * Get a pty master/slave pair if the pty, notty, socket,
         * or record options were specified.
         */
-       strlcpy(ppp_devnam, devnam, MAXPATHLEN);
+       strlcpy(ppp_devname, devnam, MAXPATHLEN);
        pty_master = -1;
        pty_slave = -1;
        real_ttyfd = -1;
        if (using_pty || record_file != NULL) {
-               if (!get_pty(&pty_master, &pty_slave, ppp_devnam, uid)) {
+               if (!get_pty(&pty_master, &pty_slave, ppp_devname, uid)) {
                        error("Couldn't allocate pseudo-tty");
-                       status = EXIT_FATAL_ERROR;
+                       ppp_set_status(EXIT_FATAL_ERROR);
                        return -1;
                }
                set_up_tty(pty_slave, 1);
@@ -534,7 +592,7 @@ int connect_tty(void)
        /*
         * Lock the device if we've been asked to.
         */
-       status = EXIT_LOCK_FAILED;
+       ppp_set_status(EXIT_LOCK_FAILED);
        if (lockflag && !privopen) {
                if (lock(devnam) < 0)
                        goto errret;
@@ -560,7 +618,7 @@ int connect_tty(void)
                        if (prio < OPRIO_ROOT && seteuid(uid) == -1) {
                                error("Unable to drop privileges before opening %s: %m\n",
                                      devnam);
-                               status = EXIT_OPEN_FAILED;
+                               ppp_set_status(EXIT_OPEN_FAILED);
                                goto errret;
                        }
                        real_ttyfd = open(devnam, O_NONBLOCK | O_RDWR, 0);
@@ -572,7 +630,7 @@ int connect_tty(void)
                        errno = err;
                        if (err != EINTR) {
                                error("Failed to open %s: %m", devnam);
-                               status = EXIT_OPEN_FAILED;
+                               ppp_set_status(EXIT_OPEN_FAILED);
                        }
                        if (!persist || err != EINTR)
                                goto errret;
@@ -615,7 +673,7 @@ int connect_tty(void)
         * If the pty, socket, notty and/or record option was specified,
         * start up the character shunt now.
         */
-       status = EXIT_PTYCMD_FAILED;
+       ppp_set_status(EXIT_PTYCMD_FAILED);
        if (ptycommand != NULL) {
                if (record_file != NULL) {
                        int ipipe[2], opipe[2], ok;
@@ -681,7 +739,7 @@ int connect_tty(void)
                if (initializer && initializer[0]) {
                        if (device_script(initializer, ttyfd, ttyfd, 0) < 0) {
                                error("Initializer script failed");
-                               status = EXIT_INIT_FAILED;
+                               ppp_set_status(EXIT_INIT_FAILED);
                                goto errretf;
                        }
                        if (got_sigterm) {
@@ -694,7 +752,7 @@ int connect_tty(void)
                if (connector && connector[0]) {
                        if (device_script(connector, ttyfd, ttyfd, 0) < 0) {
                                error("Connect script failed");
-                               status = EXIT_CONNECT_FAILED;
+                               ppp_set_status(EXIT_CONNECT_FAILED);
                                goto errretf;
                        }
                        if (got_sigterm) {
@@ -721,7 +779,7 @@ int connect_tty(void)
                                break;
                        if (errno != EINTR) {
                                error("Failed to reopen %s: %m", devnam);
-                               status = EXIT_OPEN_FAILED;
+                               ppp_set_status(EXIT_OPEN_FAILED);
                        }
                        if (!persist || errno != EINTR || hungup || got_sigterm)
                                goto errret;
@@ -730,7 +788,7 @@ int connect_tty(void)
        }
 
        slprintf(numbuf, sizeof(numbuf), "%d", baud_rate);
-       script_setenv("SPEED", numbuf, 0);
+       ppp_script_setenv("SPEED", numbuf, 0);
 
        /* run welcome script, if any */
        if (welcomer && welcomer[0]) {
@@ -911,7 +969,7 @@ start_charshunt(int ifd, int ofd)
 {
     int cpid, ret;
 
-    cpid = safe_fork(ifd, ofd, (log_to_fd >= 0? log_to_fd: 2));
+    cpid = ppp_safe_fork(ifd, ofd, (log_to_fd >= 0? log_to_fd: 2));
     if (cpid == -1) {
        error("Can't fork process for character shunt: %m");
        return 0;
@@ -1054,7 +1112,7 @@ charshunt(int ifd, int ofd, char *record_file)
     pty_readable = stdin_readable = 1;
 
     ilevel = olevel = 0;
-    get_time(&levelt);
+    ppp_get_time(&levelt);
     if (max_data_rate) {
        max_level = max_data_rate / 10;
        if (max_level < 100)
@@ -1103,7 +1161,7 @@ charshunt(int ifd, int ofd, char *record_file)
            int nbt;
            struct timeval now;
 
-           get_time(&now);
+           ppp_get_time(&now);
            dt = (now.tv_sec - levelt.tv_sec
                  + (now.tv_usec - levelt.tv_usec) / 1e6);
            nbt = (int)(dt * max_data_rate);