int main() { catch_signal(SIGALRM,times_up); catch_signal(SIGINT,end_game); while(1) { int a = random() % 11; int b = random() % 11; char txt[4]; alarm(5); printf("\n what is %i times %i?",a,b); fgets(txt,4,stdin); int answer = atoi(txt); if (answer == a*b) score++; else printf("\nWrong! Score: %i\n",score); } return 0; }
int main(int argc, char const *argv[]) { catch_signal(SIGALRM, times_up); catch_signal(SIGINT, end_game); srandom(time (0)); while(1){ //a and b will be randum numbers from 0 to 10 int a = random() % 11; int b = random() % 11; char txt[4]; alarm(5); //set interval timer printf("\nWhat is %i times %i\n", a, b); fgets(txt, 4, stdin); int answer = atoi(txt); if(answer == a * b) score++; else printf("\nWrong! Score: %i\n", score); } return 0; }
int main() { if (catch_signal(SIGINT, diediedie) == -1) { fprintf(stderr, "Can't map the handler\n"); exit(2); } char name[30]; printf("Enter your name:"); fgets(name, sizeof(name) / sizeof(char), stdin); if (name[strlen(name) - 1] == '\n') name[strlen(name) - 1] = 0; printf("Hello %s\n", name); return 0; }
int main(void) { int a, b, answer; char txt[4]; catch_signal(SIGALRM, times_up); catch_signal(SIGINT, end_game); srandom((unsigned int) time(NULL)); while(1) { a = rand() % 11; b = rand() % 11; printf("\nWhat is %d times %d? ", a, b); alarm(5); fgets(txt, 4, stdin); answer = atoi(txt); if (answer == a * b) { score++; } else { printf("\nWrong! Score: %i\n", score); } } return 0; }
int main(int argc, char *argv[]) { int connect_d = 0, rc = 0; char intro_msg[] = "Internet Knock-Knock Protocol Server\nKnock, knock.\n"; if (catch_signal(SIGINT, handle_shutdown) == -1) error("Setting interrupt handler"); int port = 30000; listener_d = open_listener_socket(); bind_to_port(listener_d, port); if (listen(listener_d, 10) == -1) error("Can't listen"); printf("Waiting for connection on port %d\n", port); char buf[255]; while (1) { connect_d = open_client_socket(); if (say(connect_d, intro_msg) == -1) { close(connect_d); continue; } read_in(connect_d, buf, sizeof(buf)); // check to make sure they said "Who's there?" if (say(connect_d, "Surrealist giraffe.\n") == -1) { close(connect_d); continue; } read_in(connect_d, buf, sizeof(buf)); // check to make sure they said "Surrealist giraffe who?" if (say(connect_d, "Bathtub full of brightly-colored machine tools.\n") == -1) { close(connect_d); continue; } close(connect_d); } return 0; }
/* 主函数 */ int main(int argc, char *argv[]) { if (catch_signal(SIGINT, handle_shutdown) == -1) error("Can't set the interrupt handler");// 如果有人按了Ctrl-C就调用handle_shutdown()。 listener_d = open_listener_socket();// 创建套接字。 bind_to_port(listener_d, 30000); if (listen(listener_d, 10) == -1) {// 把监听长度设为10。 error("listen error: "); } struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); puts("Waiting for connection"); char buf[255]; while (1) {// 需要循环“接受连接,然后开始对话”。 int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size);// 接受来自客户端的连接。 if (connect_d == -1) { error("connect error:"); } if (!fork()) {// 创建子进程,如果fork()调用返回0,就说明你在子进程中。 close(listener_d);// 在子进程中,需要关闭主监听套接字。子进程只用connect_d套接字和客户端通信。 if (say(connect_d, "Internet Knock-Knock Server \r\nVersion 1.0\r\nKnock! Knock!\r\n>") != -1){ read_in(connect_d, buf, sizeof(buf)); if (strncasecmp("Who's there?", buf, 12)) say(connect_d, "You should say 'Who's there?' !"); else { if (say(connect_d, "Oscar\r\n>") != -1) { read_in(connect_d, buf, sizeof(buf)); if (strncasecmp("Oscar who?", buf, 10)) say(connect_d, "You should say 'Oscar who?' !\r\n"); else say(connect_d, "Oscar silly question, you set a silly answer\r\n"); } } } close(connect_d);// 一旦通信结束,子进程就可以关闭通信套接字了。 exit(0);// 通信结束以后,子进程应该退出程序。这样就能防止子进程进入服务器的主循环。 } close(connect_d); } return 0; }
int main(int ac, char **av) { t_win *win; char *origin_term; catch_signal(); if (ac > 1) { win = check_static(); if ((origin_term = getenv("TERM")) == NULL) exit(0); if (tgetent(NULL, origin_term) == ERR) exit(0); if (tcgetattr(0, &(win->ori_term)) == -1) exit(0); tputs(tgetstr("ti", NULL), 0, &ft_putterm); create_list(av, &(win->arg)); get_list(); } return (0); }
/** Installing signal handler to handle the Ctrl C and other kill handlers Please note that this does not handle the SIGKILL or kill -9 and other ungracefull crashes. */ static void install_signal_handler(void) { struct sigaction action, tmpaction; /* Asynchronous signals that result in attempted graceful exit */ /* Set up the structure to specify the new action. */ printf("Installing signal handler...\n"); sigemptyset(&action.sa_mask); action.sa_sigaction = &sigproc; action.sa_flags = SA_SIGINFO; catch_signal(SIGHUP, action, tmpaction); catch_signal(SIGINT, action, tmpaction); catch_signal(SIGQUIT, action, tmpaction); catch_signal(SIGTERM, action, tmpaction); catch_signal(SIGSEGV, action, tmpaction); catch_signal(SIGABRT, action, tmpaction); }
int main(int argc, char *argv[]) { if (argc != 2) { puts("Please specify port."); exit(1); } int port = atoi(argv[1]); //al teclear CTRL+C se termina el servidor if (catch_signal(SIGINT, handle_shutdown) == -1) error("Can't set the interrupt handler"); listener_d = open_listener_socket(); bind_to_port(listener_d, port); //maximo de clientes 10 if (listen(listener_d, MAX_CLIENTS) == -1) error("Can't listen"); struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); printf("Listening in port %d\n", port); char buf[255]; while (1) { printf("Esperando la petición\n"); int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size); if (connect_d == -1){ error("Can't open secondary socket"); } //ejecución normal threads pthread_create(&threads[client_counter], NULL, new_client, (void*)connect_d); } return 0; }
int main (int argc, char *argv[]) { if (catch_signal (SIGINT, handle_shutdown) == -1) error ("割り込みハンドラを設定できません。"); listener_d = open_listener_socket (); bind_to_port (listener_d, 30000); if (listen (listener_d, 10) == -1) error ("接続待できません。"); struct sockaddr_storage client_addr; unsigned int address_size = sizeof (client_addr); puts ("接続を待っています。"); char buf[256]; while (1) { int connect_d = accept (listener_d, (struct sockaddr*)&client_addr, &address_size); if (connect_d == -1) error ("第2のソケットを開けません。"); if (!fork()) { close (!fork()); if (say (connect_d, "インターネット ノックノックプロトコル\r\nバージョン1.0\r\n") != -1) { read_in (connect_d, buf, sizeof (buf)); if (strncasecmp ("Who's there?", buf, 12)) say (connect_d, "「Who's there?」と入力しなければいけません。"); else { if (say (connect_d, "Oscar\r\n") != -1) { read_in (connect_d, buf, sizeof (buf)); if (strncasecmp ("Oscar who?", buf, 10)) say (connect_d, "「Who's there?」と入力しなければいけません。"); else say (connect_d, "Oscar silly question, you'll get silly answer."); } } } close (connect_d); exit (0); } close (connect_d); } return 0; }
int main(int argc, char *argv[]) { int listenfd, connfd; pid_t childpid; socklen_t socklen; struct sockaddr_in svaddr, cliaddr; int ret; listenfd = socket(AF_INET, SOCK_STREAM, 0); if (listenfd == -1) { fprintf(stderr, "socket() failed: %s\n", strerror(errno)); return -1; } memset(&svaddr, 0, sizeof(svaddr)); svaddr.sin_family = AF_INET; svaddr.sin_addr.s_addr = htonl(INADDR_ANY); svaddr.sin_port = htons(SERV_PORT); ret = bind(listenfd, (struct sockaddr *) &svaddr, sizeof(svaddr)); if (ret == -1) { fprintf(stderr, "bind() failed: %s\n", strerror(errno)); close(listenfd); return -1; } ret = listen(listenfd, BACK_LOG); if (ret == -1) { fprintf(stderr, "listen() failed: %s\n", strerror(errno)); close(listenfd); return -1; } /* 处理子进程 */ if (catch_signal(SIGCHLD, sigchld_handler) == -1) fprintf(stderr, "catch_signal(SIGCHLD) function failed.\n"); socklen = sizeof(cliaddr); while (1) { connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &socklen); if (connfd == -1) { if (errno == EINTR) continue; fprintf(stderr, "accept() failed: %s\n", strerror(errno)); continue; } childpid = fork(); switch (childpid) { case -1: /* error */ fprintf(stderr, "fock() failed: %s\n", strerror(errno)); break; case 0: /* child */ close(listenfd); str_echo(connfd); close(connfd); exit(0); default: /* parent */ close(connfd); break; } } close(listenfd); return 0; }
int main(int argc, char **argv) { int opt; int do_list = 0; char *do_load = NULL; while ((opt = getopt(argc, argv, "h?b:lf:a:n:kR:B:")) != EOF) { switch (opt) { case 'b': dbname = optarg; break; case 'f': if (do_load) { fprintf(stderr, "Duplicate option -f\n"); usage(); } do_load = optarg; break; case 'l': do_list = 1; break; case 'a': active_probing = atoi(optarg); break; case 'n': negative_timeout = atoi(optarg); break; case 'k': no_kernel_broadcasts = 1; break; case 'R': if ((broadcast_rate = atoi(optarg)) <= 0 || (broadcast_rate = 1000/broadcast_rate) <= 0) { fprintf(stderr, "Invalid ARP rate\n"); exit(-1); } break; case 'B': if ((broadcast_burst = atoi(optarg)) <= 0 || (broadcast_burst = 1000*broadcast_burst) <= 0) { fprintf(stderr, "Invalid ARP burst\n"); exit(-1); } break; case 'h': case '?': default: usage(); } } argc -= optind; argv += optind; if (argc > 0) { ifnum = argc; ifnames = argv; ifvec = malloc(argc*sizeof(int)); if (!ifvec) { perror("malloc"); exit(-1); } } if ((udp_sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { perror("socket"); exit(-1); } if (ifnum) { int i; struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); for (i=0; i<ifnum; i++) { strncpy(ifr.ifr_name, ifnames[i], IFNAMSIZ); if (ioctl(udp_sock, SIOCGIFINDEX, &ifr)) { perror("ioctl(SIOCGIFINDEX)"); exit(-1);; } ifvec[i] = ifr.ifr_ifindex; } } dbase = dbopen(dbname, O_CREAT|O_RDWR, 0644, DB_HASH, NULL); if (dbase == NULL) { perror("db_open"); exit(-1); } if (do_load) { char buf[128]; FILE *fp; struct dbkey k; DBT dbkey, dbdat; dbkey.data = &k; dbkey.size = sizeof(k); if (strcmp(do_load, "-") == 0 || strcmp(do_load, "--") == 0) { fp = stdin; } else if ((fp = fopen(do_load, "r")) == NULL) { perror("fopen"); goto do_abort; } buf[sizeof(buf)-1] = 0; while (fgets(buf, sizeof(buf)-1, fp)) { __u8 b1[6]; char ipbuf[128]; char macbuf[128]; if (buf[0] == '#') continue; if (sscanf(buf, "%u%s%s", &k.iface, ipbuf, macbuf) != 3) { fprintf(stderr, "Wrong format of input file \"%s\"\n", do_load); goto do_abort; } if (strncmp(macbuf, "FAILED:", 7) == 0) continue; if (!inet_aton(ipbuf, (struct in_addr*)&k.addr)) { fprintf(stderr, "Invalid IP address: \"%s\"\n", ipbuf); goto do_abort; } dbdat.data = hexstring_a2n(macbuf, b1, 6); if (dbdat.data == NULL) goto do_abort; dbdat.size = 6; if (dbase->put(dbase, &dbkey, &dbdat, 0)) { perror("hash->put"); goto do_abort; } } dbase->sync(dbase, 0); if (fp != stdin) fclose(fp); } if (do_list) { DBT dbkey, dbdat; printf("%-8s %-15s %s\n", "#Ifindex", "IP", "MAC"); while (dbase->seq(dbase, &dbkey, &dbdat, R_NEXT) == 0) { struct dbkey *key = dbkey.data; if (handle_if(key->iface)) { if (!IS_NEG(dbdat.data)) { char b1[18]; printf("%-8d %-15s %s\n", key->iface, inet_ntoa(*(struct in_addr*)&key->addr), hexstring_n2a(dbdat.data, 6, b1, 18)); } else { printf("%-8d %-15s FAILED: %dsec ago\n", key->iface, inet_ntoa(*(struct in_addr*)&key->addr), NEG_AGE(dbdat.data)); } } } } if (do_load || do_list) goto out; pset[0].fd = socket(PF_PACKET, SOCK_DGRAM, 0); if (pset[0].fd < 0) { perror("socket"); exit(-1); } if (1) { struct sockaddr_ll sll; memset(&sll, 0, sizeof(sll)); sll.sll_family = AF_PACKET; sll.sll_protocol = htons(ETH_P_ARP); sll.sll_ifindex = (ifnum == 1 ? ifvec[0] : 0); if (bind(pset[0].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) { perror("bind"); goto do_abort; } } if (rtnl_open(&rth, RTMGRP_NEIGH) < 0) { perror("rtnl_open"); goto do_abort; } pset[1].fd = rth.fd; load_initial_table(); if (daemon(0, 0)) { perror("arpd: daemon"); goto do_abort; } openlog("arpd", LOG_PID | LOG_CONS, LOG_DAEMON); catch_signal(SIGINT, sig_exit); catch_signal(SIGTERM, sig_exit); catch_signal(SIGHUP, sig_sync); catch_signal(SIGUSR1, sig_stats); #define EVENTS (POLLIN|POLLPRI|POLLERR|POLLHUP) pset[0].events = EVENTS; pset[0].revents = 0; pset[1].events = EVENTS; pset[1].revents = 0; sigsetjmp(env, 1); for (;;) { in_poll = 1; if (do_exit) break; if (do_sync) { in_poll = 0; dbase->sync(dbase, 0); do_sync = 0; in_poll = 1; } if (do_stats) send_stats(); if (poll(pset, 2, 30000) > 0) { in_poll = 0; if (pset[0].revents&EVENTS) get_arp_pkt(); if (pset[1].revents&EVENTS) get_kern_msg(); } else { do_sync = 1; } } undo_sysctl_adjustments(); out: dbase->close(dbase); exit(0); do_abort: dbase->close(dbase); exit(-1); }
int main(int argc, char *argv[]) { struct server_settings settings = {30000, 0, 1}; handle_arguments(argc, argv, &settings); struct player_character hero; init_player_character(&hero); char stat_buffer[256]; randomize_player_character_stats(&hero); snprintf(stat_buffer, 255, "STR - %i\r\nDEX - %i\r\nCON - %i\r\n", hero.strength, hero.dexterity, hero.constitution); if (catch_signal(SIGINT, handle_shutdown) == -1) { error("Can't set the interrupt handler"); } else if (settings.verbose) { puts("Interrupt handler set"); } listener_d = open_listener_socket(); bind_to_port(listener_d, settings.port_number); if (listen(listener_d, 10) == -1) { error("Can't listen"); } else if (settings.verbose) { printf("Listening on port %i", settings.port_number); } struct sockaddr_storage client_addr; unsigned int address_size = sizeof(client_addr); if (settings.verbose) { puts("Waiting for connection"); } while(1) { int connect_d = accept(listener_d, (struct sockaddr *)&client_addr, &address_size); if (connect_d == -1) { error("Can't open secondary socket"); } else if (settings.verbose) { puts("New connection started"); } if (!fork()) { close(listener_d); char input_buffer[512]; char output_buffer[512]; char send_buffer[512]; char action_buffer[3]; send_version(connect_d); while (1) { get_command(connect_d, action_buffer); puts(action_buffer); if (action_buffer[0] == 's') { puts("opening chat buffer"); listen_to_client(connect_d, input_buffer); parse_command(input_buffer, output_buffer); sprintf(send_buffer, "You said: \"%s\"\r\n", output_buffer); puts(send_buffer); send_to_client(connect_d, send_buffer); } else { handle_user_command(connect_d, action_buffer); } } close(connect_d); exit(0); } close(connect_d); } return 0; }
char *rep_getpass(const char *prompt) { FILE *in, *out; int echo_off; static char buf[256]; static size_t bufsize = sizeof(buf); size_t nread; /* Catch problematic signals */ catch_signal(SIGINT, gotintr_sig); /* Try to write to and read from the terminal if we can. If we can't open the terminal, use stderr and stdin. */ in = fopen ("/dev/tty", "w+"); if (in == NULL) { in = stdin; out = stderr; } else { out = in; } setvbuf(in, NULL, _IONBF, 0); /* Turn echoing off if it is on now. */ if (tcgetattr (fileno (in), &t) == 0) { if (ECHO_IS_ON(t)) { TURN_ECHO_OFF(t); echo_off = tcsetattr (fileno (in), TCSAFLUSH, &t) == 0; TURN_ECHO_ON(t); } else { echo_off = 0; } } else { echo_off = 0; } /* Write the prompt. */ fputs(prompt, out); fflush(out); /* Read the password. */ buf[0] = 0; if (!gotintr) { in_fd = fileno(in); if (fgets(buf, bufsize, in) == NULL) { buf[0] = 0; } } nread = strlen(buf); if (nread) { if (buf[nread - 1] == '\n') buf[nread - 1] = '\0'; } /* Restore echoing. */ if (echo_off) { if (gotintr && in_fd == -1) { in = fopen ("/dev/tty", "w+"); } if (in != NULL) tcsetattr (fileno (in), TCSANOW, &t); } fprintf(out, "\n"); fflush(out); if (in && in != stdin) /* We opened the terminal; now close it. */ fclose(in); /* Catch problematic signals */ catch_signal(SIGINT, SIG_DFL); if (gotintr) { printf("Interrupted by signal.\n"); fflush(stdout); exit(1); } return buf; }
int main(int argc, char **argv) { struct pollfd pset[2]; int psize; int opt; opterr = 0; while ((opt = getopt(argc, argv, "aAb:dvoe")) != EOF) { switch (opt) { case 'a': ++all_ifaces; break; case 'A': ++listen_arp; break; case 'd': ++debug; break; case 'v': ++verbose; break; case 'o': ++allow_offlink; break; case 'e': ++only_ethers; break; case 'b': tftp_dir = optarg; break; default: usage(); } } if (argc > optind) { if (argc > optind+1) usage(); ifname = argv[optind]; } psize = 1; pset[0].fd = socket(PF_PACKET, SOCK_DGRAM, 0); if (ifname) { struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, ifname, IFNAMSIZ); if (ioctl(pset[0].fd, SIOCGIFINDEX, &ifr)) { perror("ioctl(SIOCGIFINDEX)"); usage(); } ifidx = ifr.ifr_ifindex; } pset[1].fd = -1; if (listen_arp) { pset[1].fd = socket(PF_PACKET, SOCK_DGRAM, 0); if (pset[1].fd >= 0) { load_arp_bpflet(pset[1].fd); psize = 1; } } if (pset[1].fd >= 0) { struct sockaddr_ll sll; memset(&sll, 0, sizeof(sll)); sll.sll_family = AF_PACKET; sll.sll_protocol = htons(ETH_P_ARP); sll.sll_ifindex = all_ifaces ? 0 : ifidx; if (bind(pset[1].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) { close(pset[1].fd); pset[1].fd = -1; psize = 1; } } if (pset[0].fd >= 0) { struct sockaddr_ll sll; memset(&sll, 0, sizeof(sll)); sll.sll_family = AF_PACKET; sll.sll_protocol = htons(ETH_P_RARP); sll.sll_ifindex = all_ifaces ? 0 : ifidx; if (bind(pset[0].fd, (struct sockaddr*)&sll, sizeof(sll)) < 0) { close(pset[0].fd); pset[0].fd = -1; } } if (pset[0].fd < 0) { pset[0] = pset[1]; psize--; } if (psize == 0) { fprintf(stderr, "failed to bind any socket. Aborting.\n"); exit(1); } if (!debug) { int fd; pid_t pid = fork(); if (pid > 0) exit(0); else if (pid == -1) { perror("rarpd: fork"); exit(1); } chdir("/"); fd = open("/dev/null", O_RDWR); if (fd >= 0) { dup2(fd, 0); dup2(fd, 1); dup2(fd, 2); if (fd > 2) close(fd); } setsid(); } openlog("rarpd", LOG_PID | LOG_CONS, LOG_DAEMON); catch_signal(SIGALRM, sig_alarm); catch_signal(SIGHUP, sig_hup); for (;;) { int i; if (do_reload) { configure(); do_reload = 0; } #define EVENTS (POLLIN|POLLPRI|POLLERR|POLLHUP) pset[0].events = EVENTS; pset[0].revents = 0; pset[1].events = EVENTS; pset[1].revents = 0; i = poll(pset, psize, -1); if (i <= 0) { if (errno != EINTR && i<0) { syslog(LOG_ERR, "poll returned some crap: %m\n"); sleep(10); } continue; } for (i=0; i<psize; i++) { if (pset[i].revents&EVENTS) serve_it(pset[i].fd); } } }