/** read configuration options from the command line */ static void parse_cmdline(struct config *config, int argc, char **argv) { int ret; #ifdef __GLIBC__ static const struct option long_options[] = { {"help", 0, 0, 'h'}, {"version", 0, 0, 'V'}, {"verbose", 0, 0, 'v'}, {"quiet", 1, 0, 'q'}, {"output", 1, 0, 'o'}, {"decode", 0, 0, 'd'}, {"map", 0, 0, 'm'}, {0,0,0,0} }; #endif memset(config, 0, sizeof(*config)); config->verbose = 1; while (1) { #ifdef __GLIBC__ int option_index = 0; ret = getopt_long(argc, argv, "hVvqo:dm", long_options, &option_index); #else ret = getopt(argc, argv, "hVvqo:dm"); #endif if (ret == -1) break; switch (ret) { case 'h': usage(); exit(0); case 'V': puts("loggertools v" VERSION " (C) 2004-2007 Max Kellermann <*****@*****.**>\n" "http://max.kellermann.name/projects/loggertools/\n"); exit(0); case 'v': ++config->verbose; break; case 'q': config->verbose = 0; break; case 'o': config->output_path = optarg; break; case 'd': config->map = 0; config->decode = 1; break; case 'm': config->decode = 0; config->map = 1; break; default: exit(1); } } if (optind == argc) arg_error("No input file specified"); if (optind < argc - 1) arg_error("Too many arguments"); config->input_path = argv[optind]; if (strcmp(config->input_path, "-") == 0) config->input_path = NULL; if (config->output_path != NULL && strcmp(config->output_path, "-") == 0) config->output_path = NULL; if (config->decode && config->output_path == NULL) arg_error("Cannot decode to stdout, please specify a file with the '-o' option"); }
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; }