/* Read the specified configuration file and call the routine to create the ports. */ int readconfig(char *filename) { FILE *instream; char inbuf[MAX_LINE_SIZE]; int rv = 0; lineno = 0; instream = fopen(filename, "r"); if (instream == NULL) { syslog(LOG_ERR, "Unable to open config file '%s': %m", filename); return -1; } free_longstrs(); free_tracefiles(); config_num++; while (fgets(inbuf, MAX_LINE_SIZE, instream) != NULL) { int len = strlen(inbuf); if (inbuf[len-1] != '\n') { lineno++; syslog(LOG_ERR, "line %d is too long in config file", lineno); continue; } /* Remove the '\n' */ inbuf[len-1] = '\0'; handle_config_line(inbuf); } /* Delete anything that wasn't in the new config file. */ clear_old_port_config(config_num); fclose(instream); return rv; }
int main(int argc, char *argv[]) { int i; int err; err = sel_alloc_selector(&ser2net_sel); if (err) { fprintf(stderr, "Could not initialize ser2net selector: '%s'\n", strerror(err)); return -1; } for (i=1; i<argc; i++) { if ((argv[i][0] != '-') || (strlen(argv[i]) != 2)) { fprintf(stderr, "Invalid argument: '%s'\n", argv[i]); arg_error(argv[0]); } switch (argv[i][1]) { case 'n': detach = 0; break; case 'd': detach = 0; debug = 1; break; case 'b': cisco_ios_baud_rates = 1; break; case 'C': /* Get a config line. */ i++; if (i == argc) { fprintf(stderr, "No config line specified with -C\n"); arg_error(argv[0]); } handle_config_line(argv[i]); config_file = NULL; break; case 'c': /* Get a config file. */ i++; if (i == argc) { fprintf(stderr, "No config file specified with -c\n"); arg_error(argv[0]); } config_file = argv[i]; break; case 'p': /* Get the control port. */ i++; if (i == argc) { fprintf(stderr, "No control port specified with -p\n"); arg_error(argv[0]); } config_port = argv[i]; break; case 'P': i++; if (i == argc) { fprintf(stderr, "No pid file specified with -P\n"); arg_error(argv[0]); } pid_file = argv[i]; break; #ifdef USE_UUCP_LOCKING case 'u': uucp_locking_enabled = 0; break; #endif case 'v': printf("%s version %s\n", argv[0], VERSION); exit(0); default: fprintf(stderr, "Invalid option: '%s'\n", argv[i]); arg_error(argv[0]); } } setup_sighup(); if (config_port != NULL) { if (controller_init(config_port) == -1) { fprintf(stderr, "Invalid control port specified with -p\n"); arg_error(argv[0]); } } if (debug && !detach) openlog("ser2net", LOG_PID | LOG_CONS | LOG_PERROR, LOG_DAEMON); if (config_file) { if (readconfig(config_file) == -1) { return 1; } } if (detach) { int pid; /* Detach from the calling terminal. */ openlog("ser2net", LOG_PID | LOG_CONS, LOG_DAEMON); syslog(LOG_NOTICE, "ser2net startup"); if ((pid = fork()) > 0) { exit(0); } else if (pid < 0) { syslog(LOG_ERR, "Error forking first fork"); exit(1); } else { /* setsid() is necessary if we really want to demonize */ setsid(); /* Second fork to really deamonize me. */ if ((pid = fork()) > 0) { exit(0); } else if (pid < 0) { syslog(LOG_ERR, "Error forking second fork"); exit(1); } } /* Close all my standard I/O. */ chdir("/"); close(0); close(1); close(2); } /* write pid file */ make_pidfile(pid_file); /* Ignore SIGPIPEs so they don't kill us. */ signal(SIGPIPE, SIG_IGN); set_sighup_handler(reread_config); sel_select_loop(ser2net_sel); return 0; }