From 22a123ac402e7786ab4472c561a9e9a21cd21567 Mon Sep 17 00:00:00 2001 From: Thomas Osterried Date: Sat, 31 Jan 2009 21:25:45 +0000 Subject: Better integration in unix98 PTY environments. Revised the getopt_long() part. Assured strcpy cases. Thanks to Christoph for contribution. --- ax25ipd/ax25ipd.8 | 8 ++++++++ ax25ipd/ax25ipd.c | 55 ++++++++++++++++++++++++++++++------------------------- ax25ipd/ax25ipd.h | 2 +- ax25ipd/config.c | 14 +++++++++++--- 4 files changed, 50 insertions(+), 29 deletions(-) (limited to 'ax25ipd') diff --git a/ax25ipd/ax25ipd.8 b/ax25ipd/ax25ipd.8 index f343494..a96e1b2 100644 --- a/ax25ipd/ax25ipd.8 +++ b/ax25ipd/ax25ipd.8 @@ -37,6 +37,14 @@ Set the logging level to NUM. .TP 10 .BI \-c,--configfile FILE Use the configuration file named by FILE instead of the default. +.TP 10 +.BI \-d,--ttydevice TTYDEV +Use ttydevice instead of the configured. This is useful because on systems +with unix98 PTYs the device is allocated dynamicaly and thus the name is +not predictable. +.TP 10 +.BI \-f,--nofork +Do not become a daemon. Run in foreground. .SH FILES /etc/ax25/ax25ipd.conf .SH "SEE ALSO" diff --git a/ax25ipd/ax25ipd.c b/ax25ipd/ax25ipd.c index 2151238..585b775 100644 --- a/ax25ipd/ax25ipd.c +++ b/ax25ipd/ax25ipd.c @@ -32,14 +32,16 @@ int opt_version = 0; int opt_loglevel = 0; int opt_nofork = 0; int opt_help = 0; -char opt_configfile[1024]; +char opt_configfile[PATH_MAX]; +char opt_ttydevice[PATH_MAX]; struct option options[] = { - {"version", 0, &opt_version, 1}, - {"loglevel", 1, &opt_loglevel, 1}, - {"help", 0, &opt_help, 1}, - {"configfile", 1, NULL, 0}, - {"nofork", 0, &opt_nofork, 1}, + {"version", 0, NULL, 'v'}, + {"loglevel", 1, NULL, 'l'}, + {"help", 0, NULL, 'h'}, + {"configfile", 1, NULL, 'c'}, + {"ttydevice", 1, NULL, 'd'}, + {"nofork", 0, NULL, 'f'}, {0, 0, 0, 0} }; @@ -49,41 +51,36 @@ int main(int argc, char **argv) signal(SIGHUP, hupper); } + *opt_configfile = 0; + *opt_ttydevice = 0; + /* set up the handler for statistics reporting */ signal(SIGUSR1, usr1_handler); signal(SIGINT, int_handler); signal(SIGTERM, term_handler); while (1) { - int option_index = 0; int c; - c = getopt_long(argc, argv, "c:fhl:v", options, &option_index); + c = getopt_long(argc, argv, "c:d:fhl:v", options, NULL); if (c == -1) break; switch (c) { - case 0: - break; - switch (option_index) { - case 0: - break; - case 1: - opt_loglevel = atoi(optarg); - break; - case 2: - break; - case 3: - strncpy(opt_configfile, optarg, 1023); - break; - } - break; case 'c': - strncpy(opt_configfile, optarg, 1023); + strncpy(opt_configfile, optarg, sizeof(opt_configfile)-1); + opt_configfile[sizeof(opt_configfile)-1] = 0; + break; + case 'd': + strncpy(opt_ttydevice, optarg, sizeof(opt_ttydevice)-1); + opt_ttydevice[sizeof(opt_ttydevice)-1] = 0; break; case 'f': opt_nofork = 1; break; + case 'h': + opt_help = 1; + break; case 'v': opt_version = 1; break; @@ -94,7 +91,8 @@ int main(int argc, char **argv) } if (optind < argc) { - printf("config %s\n", argv[optind++]); + printf("Unknown argument '%s' ...\n\n", argv[optind++]); + opt_help = 1; } if (opt_version == 1) { @@ -113,6 +111,8 @@ int main(int argc, char **argv) (" --loglevel NUM, -l NUM Set logging level to NUM\n"); printf (" --configfile FILE, -c FILE Set configuration file to FILE\n"); + printf + (" --device TTYDEV, -d TTYDEV Set configuration file to FILE\n"); printf (" --nofork, -f Do not put daemon in background\n"); exit(0); @@ -128,6 +128,11 @@ int main(int argc, char **argv) /* read config file */ config_read(opt_configfile); + if (opt_ttydevice[0] != '\0') { + strncpy(ttydevice, opt_ttydevice, sizeof(ttydevice)-1); + ttydevice[sizeof(ttydevice)-1] = '\0'; + } + /* print the current config and route info */ dump_config(); dump_routes(); diff --git a/ax25ipd/ax25ipd.h b/ax25ipd/ax25ipd.h index 3b0a894..29ebb5e 100644 --- a/ax25ipd/ax25ipd.h +++ b/ax25ipd/ax25ipd.h @@ -74,7 +74,7 @@ int udp_mode; /* true if we need a UDP socket */ int ip_mode; /* true if we need the raw IP socket */ unsigned short my_udp; /* the UDP port to use (network byte order) */ -char ttydevice[128]; /* the tty device for serial comms */ +char ttydevice[PATH_MAX]; /* the tty device for serial comms */ int ttyspeed; /* The baud rate on the tty device */ unsigned char mycallsign[7]; /* My callsign, shifted ASCII with SSID */ unsigned char mycallsign2[7]; /* My seconds port callsign, shifted ASCII with SSID */ diff --git a/ax25ipd/config.c b/ax25ipd/config.c index 4c1db85..e6ae790 100644 --- a/ax25ipd/config.c +++ b/ax25ipd/config.c @@ -205,10 +205,17 @@ char *buf; return 0; } else if (strcmp(p, "device") == 0) { + /* already set? i.e. with commandline option, which overwrites + * the preconfigured setting. useful for systems with unix98 + * style pty's + */ + if (*ttydevice) + return 0; q = strtok(NULL, " \t\n\r"); if (q == NULL) return -1; - strcpy(ttydevice, q); + strncpy(ttydevice, q, sizeof(ttydevice)-1); + ttydevice[sizeof(ttydevice)-1] = 0; return 0; } else if (strcmp(p, "mode") == 0) { @@ -272,10 +279,11 @@ char *buf; q = p + strlen(p) + 1; if (strlen(q) < 2) return -1; /* line ends with a \n */ - if (strlen(q) > sizeof bc_text) + if (strlen(q) > sizeof(bc_text)) return -7; q[strlen(q) - 1] = '\0'; - strcpy(bc_text, q); + strncpy(bc_text, q, sizeof(bc_text)-1); + bc_text[sizeof(bc_text)-1] = 0; return 0; } else if (strcmp(p, "loglevel") == 0) { -- cgit v1.2.3