int main(int argc, char *argv[]) { int i, c; char *port = NULL; prog = basename(argv[0]); while ((c = GETOPT(argc, argv, OPTIONS, longopts)) != -1) { switch (c) { case 'p': /* --port n */ port = xstrdup(optarg); break; default: usage(); } } if (optind < argc) usage(); if (signal(SIGPIPE, _noop_handler) == SIG_ERR) { perror("signal"); exit(1); } if (port) _setup_socket(port); for (i = 0; i < NUM_PLUGS; i++) { plug[i] = 0; beacon[i] = 0; temp[i] = 83 + i; } _prompt_loop(); exit(0); }
int main(int argc, char *argv[]) { int p; int pid; int do_fork = 0; int ttyfd, appfd; pthread_t id; argv++; if (!*argv || !strcmp(*argv, "--help") || !strcmp(*argv, "-h")) { printf("usage: netserve [-d] <port> <program> [args]\n"); exit(0); } if (!strcmp(*argv, "-d")) { do_fork = 1; argv++; } if ((p = atoi(*argv++)) == 0) { fprintf(stderr, "bad port number\n"); exit(0); } if (do_fork) { /* Daemonize */ pid = fork(); if (pid < 0) { perror("fork()"); exit(0); } if (pid > 0) { exit(0); } setsid(); } /* Broken pipes are not a problem */ signal(SIGPIPE, SIG_IGN); /* Get a pseudo tty */ if (openpty(&ttyfd, &appfd, NULL, NULL, NULL) < 0) { perror("open pty"); exit(0); } /* Start the application up with sv[1] as its stdio */ pid = _start_app(argv, appfd); /* Start proxy for input */ if (pthread_create(&id, NULL, _net2tty, (void *)&ttyfd) < 0) { perror("pthread_create"); exit(0); } /* Start proxy for output */ if (pthread_create(&id, NULL, _tty2net, (void *)&ttyfd) < 0) { perror("pthread_create"); exit(0); } /* Setup server */ _server_socket = _setup_socket(p); /* SIGUSR2 restarts the server when the client connection closes */ signal(SIGUSR2, _sig); /* Start the first server */ raise(SIGUSR2); /* Wait for our child to exit */ waitpid(pid, &p, 0); return 0; }