void handle_signal(int sig){ switch (sig){ case SIGUSR1: case SIGUSR2: syslog(LOG_DEBUG, "recieved SIGUSR %d", sig); if (kill_server() < 0) { syslog(LOG_CRIT,"SIGHANDLER: unable to kill server"); break; } if (kill_index() < 0) { syslog(LOG_CRIT,"SIGHANDLER: unable to kill index"); break; } if (init_index() < 0) { syslog(LOG_CRIT,"SIGHANDLER: unable to init index - index STILL down"); break; } if (init_server(main_ctx) < 0) { syslog(LOG_CRIT,"SIGHANDLER, unable to init server - index up but unknown by main server"); break; } break; case SIGHUP: case SIGTERM: syslog(LOG_DEBUG, "recieved SIGTERM %d", sig); if (kill_server() < 0) syslog(LOG_CRIT,"SIGHANDLER: unable to kill server"); if (kill_index() < 0) syslog(LOG_CRIT,"SIGHANDLER: unable to kill index"); kill_process(); /* should this be called ??? */ /* if called, must close sockets in threads */ /* before calling */ /* zmq_term(main_ctx); */ exit(0); } }
int main(int argc, char *argv[]) { char *conffile = NULL; int request_kill = 0, show_message = 0; if (!read_parameters(argc, argv, &conffile, &request_kill, &show_message)) { print_usage(argv[0]); return 1; } /* read the config file; conffile = NULL means use the default. */ if (!read_config(conffile)) return 1; /* error reading the config file */ setup_defaults(); if (request_kill) { kill_server(); return 0; } if (show_message) { signal_message(); return 0; } setup_signals(); /* Init files and directories. * Start logging last, so that the log is only created if startup succeeds. */ if (!init_workdir() || !init_database() || !check_database() || !begin_logging()) return 1; if (!settings.nodaemon) { if (daemon(0, 0) == -1) { settings.nodaemon = TRUE; log_msg("could not detach from terminal: %s", strerror(errno)); return 1; } if (!create_pidfile()) return 1; } /* give this process and its children their own process group id for kill() */ setpgid(0, 0); report_startup(); runserver(); /* shutdown */ remove_pidfile(); end_logging(); close_database(); remove_unix_socket(); free_config(); return 0; }
static int script_jump(int nargs, int num) { if (nargs) server_set_next(num); cycle_delay = server_config.cycle_delay; kill_server("changing servers"); return(0); }
void handle_signal(int sig){ switch (sig){ case SIGHUP: case SIGTERM: syslog(LOG_INFO, "recieved signal, %d", sig); kill_server(); kill_process(); exit(0); } }
END_TEST /* Added/Modified * Two server instance get/set pass tests: * set/get basic table/key/value names from one table. * set/get extended table/key/value names from one table. * set/get extended table/key/value names from two tables. */ START_TEST (test_restartserver_onetable) { struct storage_record r; // Start and connect to the server. int serverpid = 0; void *conn = start_connect(ONETABLE_CONF, "test_restartserver_onetable.serverout", &serverpid); fail_unless(conn != NULL, "Couldn't start or connect to server."); // Set a key/value. strncpy(r.value, VALUE, sizeof r.value); int status = storage_set(TABLE, KEY, &r, conn); fail_unless(status == 0, "Error setting a key/value pair."); // Disconnect from the server. status = storage_disconnect(conn); //fail_unless(status == 0, "Error disconnecting from the server."); // Kill the server. status = kill_server(serverpid); fail_unless(status == 0, "Couldn't kill server."); // Start the server. conn = start_connect(ONETABLE_CONF, "test_restartserver_onetable-2.serverout", &serverpid); fail_unless(conn != NULL, "Couldn't start or connect to server."); // Get a value. strncpy(r.value, "", sizeof r.value); status = storage_get(TABLE, KEY, &r, conn); fail_unless(status == -1, "storage_get with missing key should fail."); fail_unless(errno == ERR_KEY_NOT_FOUND, "storage_get with missing key not setting errno properly."); // Disconnect from the server. status = storage_disconnect(conn); //fail_unless(status == 0, "Error disconnecting from the server."); }
void *client_thr(void *arg) { int dinx; int ferr; static int kinx = -1; int lerr; int req_ctrl_size; int req_data_size; RT results; MS_SRE_LDONE sre_ldone; MyThread *thr; int tinx; thr = (MyThread *) arg; tinx = thr->inx; if (kinx < 0) kinx = gdloop - 1; for (dinx = 0; dinx < gdloop; dinx++) { req_ctrl_size = 0; req_data_size = gdsize; if (gverbose) printf("client LINK tinx=%d, dinx=%d\n", tinx, dinx); ferr = XMSG_LINK_(TPT_REF(gphandle), // phandle &gmsgids[tinx][dinx], // msgid gsend_cli_c_buffer[tinx][dinx], // reqctrl (ushort) req_ctrl_size, // reqctrlsize grecv_cli_c_buffer[tinx][dinx], // replyctrl (ushort) req_ctrl_size, // replyctrlmax gsend_cli_d_buffer[tinx][dinx], // reqdata (ushort) req_data_size, // reqdatasize grecv_cli_d_buffer[tinx][dinx], // replydata (ushort) req_data_size, // replydatamax 0, // linkertag 0, // pri 0, // xmitclass XMSG_LINK_LDONEQ); // linkopts util_check("XMSG_LINK_", ferr); if ((tinx == 0) && (dinx == kinx)) { usleep(10000); if (gnodecycle && gvirtual) node_cycle(); else kill_server(); kinx--; if (kinx < 0) kinx = gdloop - 1; } } for (dinx = 0; dinx < gdloop; ) { lerr = XWAIT(LDONE, -1); TEST_CHK_WAITIGNORE(lerr); do { lerr = XMSG_LISTEN_((short *) &sre_ldone, // sre XLISTEN_ALLOW_LDONEM, // listenopts 0); // listenertag if (lerr == XSRETYPE_LDONE) { if (gverbose) printf("client LISTEN tinx=%d, dinx=%d\n", tinx, dinx); dinx++; ferr = XMSG_BREAK_(sre_ldone.sre_msgId, results.u.s, TPT_REF(gphandle)); ferr = ferr; // ignore } } while (lerr); } return NULL; }
/** Returns connection sequence (server indices), which must be freed */ static int *perform_request(servers_fixture *f, grpc_channel *client, request_data *rdata, const test_spec *spec) { grpc_call *c; int s_idx; int *s_valid; grpc_op ops[6]; grpc_op *op; int was_cancelled; size_t i, iter_num; grpc_event ev; int read_tag; int *connection_sequence; int completed_client; s_valid = gpr_malloc(sizeof(int) * f->num_servers); connection_sequence = gpr_malloc(sizeof(int) * spec->num_iters); for (iter_num = 0; iter_num < spec->num_iters; iter_num++) { cq_verifier *cqv = cq_verifier_create(f->cq); rdata->details = NULL; rdata->details_capacity = 0; was_cancelled = 2; for (i = 0; i < f->num_servers; i++) { if (spec->kill_at[iter_num][i] != 0) { kill_server(f, i); } else if (spec->revive_at[iter_num][i] != 0) { /* killing takes precedence */ revive_server(f, rdata, i); } } connection_sequence[iter_num] = -1; grpc_metadata_array_init(&rdata->initial_metadata_recv); grpc_metadata_array_init(&rdata->trailing_metadata_recv); for (i = 0; i < f->num_servers; i++) { grpc_call_details_init(&rdata->call_details[i]); } memset(s_valid, 0, f->num_servers * sizeof(int)); c = grpc_channel_create_call(client, NULL, GRPC_PROPAGATE_DEFAULTS, f->cq, "/foo", "foo.test.google.fr", gpr_inf_future(GPR_CLOCK_REALTIME), NULL); GPR_ASSERT(c); completed_client = 0; op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 0; op->flags = 0; op->reserved = NULL; op++; op->op = GRPC_OP_SEND_CLOSE_FROM_CLIENT; op->flags = 0; op->reserved = NULL; op++; op->op = GRPC_OP_RECV_INITIAL_METADATA; op->data.recv_initial_metadata = &rdata->initial_metadata_recv; op->flags = 0; op->reserved = NULL; op++; op->op = GRPC_OP_RECV_STATUS_ON_CLIENT; op->data.recv_status_on_client.trailing_metadata = &rdata->trailing_metadata_recv; op->data.recv_status_on_client.status = &rdata->status; op->data.recv_status_on_client.status_details = &rdata->details; op->data.recv_status_on_client.status_details_capacity = &rdata->details_capacity; op->flags = 0; op->reserved = NULL; op++; GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(c, ops, (size_t)(op - ops), tag(1), NULL)); s_idx = -1; while ((ev = grpc_completion_queue_next( f->cq, GRPC_TIMEOUT_SECONDS_TO_DEADLINE(1), NULL)) .type != GRPC_QUEUE_TIMEOUT) { GPR_ASSERT(ev.type == GRPC_OP_COMPLETE); read_tag = ((int)(gpr_intptr)ev.tag); gpr_log(GPR_DEBUG, "EVENT: success:%d, type:%d, tag:%d iter:%d", ev.success, ev.type, read_tag, iter_num); if (ev.success && read_tag >= 1000) { GPR_ASSERT(s_idx == -1); /* only one server must reply */ /* only server notifications for non-shutdown events */ s_idx = read_tag - 1000; s_valid[s_idx] = 1; connection_sequence[iter_num] = s_idx; break; } else if (read_tag == 1) { gpr_log(GPR_DEBUG, "client timed out"); GPR_ASSERT(ev.success); completed_client = 1; } } if (s_idx >= 0) { op = ops; op->op = GRPC_OP_SEND_INITIAL_METADATA; op->data.send_initial_metadata.count = 0; op->flags = 0; op->reserved = NULL; op++; op->op = GRPC_OP_SEND_STATUS_FROM_SERVER; op->data.send_status_from_server.trailing_metadata_count = 0; op->data.send_status_from_server.status = GRPC_STATUS_UNIMPLEMENTED; op->data.send_status_from_server.status_details = "xyz"; op->flags = 0; op->reserved = NULL; op++; op->op = GRPC_OP_RECV_CLOSE_ON_SERVER; op->data.recv_close_on_server.cancelled = &was_cancelled; op->flags = 0; op->reserved = NULL; op++; GPR_ASSERT(GRPC_CALL_OK == grpc_call_start_batch(f->server_calls[s_idx], ops, (size_t)(op - ops), tag(102), NULL)); cq_expect_completion(cqv, tag(102), 1); if (!completed_client) { cq_expect_completion(cqv, tag(1), 1); } cq_verify(cqv); gpr_log(GPR_DEBUG, "status=%d; %s", rdata->status, rdata->details); GPR_ASSERT(rdata->status == GRPC_STATUS_UNIMPLEMENTED); GPR_ASSERT(0 == strcmp(rdata->details, "xyz")); GPR_ASSERT(0 == strcmp(rdata->call_details[s_idx].method, "/foo")); GPR_ASSERT(0 == strcmp(rdata->call_details[s_idx].host, "foo.test.google.fr")); GPR_ASSERT(was_cancelled == 1); grpc_call_destroy(f->server_calls[s_idx]); /* ask for the next request on this server */ GPR_ASSERT(GRPC_CALL_OK == grpc_server_request_call( f->servers[s_idx], &f->server_calls[s_idx], &rdata->call_details[s_idx], &f->request_metadata_recv[s_idx], f->cq, f->cq, tag(1000 + (int)s_idx))); } else { /* no response from server */ grpc_call_cancel(c, NULL); if (!completed_client) { cq_expect_completion(cqv, tag(1), 1); cq_verify(cqv); } } GPR_ASSERT(grpc_completion_queue_next( f->cq, GRPC_TIMEOUT_MILLIS_TO_DEADLINE(200), NULL).type == GRPC_QUEUE_TIMEOUT); grpc_metadata_array_destroy(&rdata->initial_metadata_recv); grpc_metadata_array_destroy(&rdata->trailing_metadata_recv); cq_verifier_destroy(cqv); grpc_call_destroy(c); for (i = 0; i < f->num_servers; i++) { grpc_call_details_destroy(&rdata->call_details[i]); } gpr_free(rdata->details); } gpr_free(s_valid); return connection_sequence; }
int main(int argc,char const *argv[]) { int len, n, pid, s, s2, inet_fd, connections=0; socklen_t t; struct sockaddr_in inet, inet2; struct hostent* hostinfo; char arguments[100],hearts_start[200]; char *all_guids[24]; memset(arguments,'\0',sizeof(arguments)); memset(hearts_start,'\0',sizeof(hearts_start)); /* Initialize the logging interface */ openlog(DAEMON_NAME, LOG_PID, LOG_LOCAL5 ); syslog(LOG_INFO, "starting" ); /* Daemonize */ daemonize( "/var/lock/" DAEMON_NAME); syslog(LOG_INFO, "We did get to daemonize!\n"); /* Now we are a daemon -- do the work for which we were paid */ // Example from Beej's Guide to Network Programming: ////https://beej.us/guide/bgnet/output/html/multipage/sockaddr_inman.html if ((inet_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { syslog(LOG_ERR, "%s", strerror(errno)); exit(1); } else syslog(LOG_INFO, IP_ADDRESS); inet.sin_family = AF_INET; inet.sin_port = htons(atoi(PORT)); inet_pton(AF_INET,IP_ADDRESS,&inet.sin_addr); /*(strcpy(inet.sin_path, argv[1])); syslog(LOG_INFO, "path copied!\n"); if(unlink(inet.sin_path)){ syslog(LOG_ERR, strerror(errno)); exit(1); } else syslog(LOG_INFO, "path unlinked!\n");*/ //len = strlen(local.sun_path) + sizeof(local.sun_family); if (bind(inet_fd, (struct sockaddr *)&inet, sizeof inet) == -1) { syslog(LOG_ERR, strerror(errno)); exit(1); } else syslog(LOG_INFO, "socket bound!\n"); if (listen(inet_fd, 8) == -1) { syslog(LOG_ERR, strerror(errno)); exit(1); } syslog(LOG_INFO, "listening for up to 8 connections!\n"); for(connections=0;;connections++) { //signalhantering, kunde kanske vara utanför loopen men varför ändra ett vinnande koncept? int connection_no = (connections%4); FILE *log_fp; char gamelog[40],port[7]; struct sigaction sa; sa.sa_handler = sigchld_handlr; // reap all dead processes sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { //WNOHANG! syslog(LOG_ERR, strerror(errno)); exit(1); } // logga spel-pid och guid i spel-logg if(!connection_no) { (gamelog, "%s%s", GAMELOG, strcpy(port, get_random_port_number())); if ((log_fp = fopen(gamelog, "w")) == NULL) syslog(LOG_ERR,"%s", strerror(errno)); else { fprintf(log_fp, "%s", port); fclose(log_fp); } log_fp = fopen(gamelog, "a+"); } //syslog(LOG_INFO, "Waiting for a connection...\n"); t = sizeof(inet2); if ((s2 = accept(inet_fd, (struct sockaddr *)&inet2, &t)) == -1) { syslog(LOG_ERR, strerror(errno)); exit(1); } /* fork efter acceptet! */ if((pid=fork())==-1){ // fork och felhantering syslog(LOG_ERR, strerror(errno)); exit(1); } else if(!pid){ //serverbarnet ärver accepten, socketen och fildeskriptorn. //printf("Connected.\n"); int i = 0,j=0, done = 0; ssize_t r; do { r = recv(s2, arguments,100, 0); if (0 >= r) { if (r < 0) perror("recv"); done = 1; //försäkrar oss om att accept-loopen avslutas nedan ... } //om recv returnerar 0 eller -1 #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wfor-loop-analysis" while(i) { // You get three tries to login Account acc,account; strcpy(acc.username, NULL); account=prompt_for_login(&s2); if(account.username == acc.username){ j += 1; if (j==3){ strcpy(arguments,"login failed"); if (send(s2,arguments,30,0) < 0) syslog(LOG_ERR,"%s",strerror(errno)); close(s2); exit(EXIT_FAILURE); } else continue; } else { //if((connection_no) == 0) memset(guid,'\0',4); //strcpy(guid[connection_no],assign_guid()); //strcpy(all_guids[connections],guid[connection_no]); if((connection_no) == 3) { if(start_game_server(get_random_port_number()) < 0){ syslog(LOG_ERR,"%s",strerror(errno)); send(s2,"game failed, try again",sizeof("game failed, try again"),0); syslog(LOG_INFO,"game start failed"); } } }break; } #pragma clang diagnostic pop if (!done){ //Inget fel eller avslut, enligt tilldelning syslog(LOG_INFO, "!done\n"); if(!(syn_ack(arguments,&i,s2,port,connection_no))){ strcpy(arguments,"ENDOFTRANS"); if (send(s2,arguments,30,0) < 0) { //meddela att meddelandet är klart syslog(LOG_ERR,"%s",strerror(errno)); done = 1; // försäkrar oss om att accept-loopen avslutas } else done = 0; memset(arguments,'\0',sizeof(arguments)); } } i += 1; } while (!done); //så länge klienten skickar data håller vi öppet 24/7 printf("I'm server %d and my client just signed off!\n",getpid()); syslog(LOG_NOTICE, "terminated" ); closelog(); exit(0); } else close(s2); //if((wate=waitpid(0,NULL,WNOHANG))==-1)perror("waitpid\n"); } // Finish up if((kill_server())){ syslog(LOG_ERR, "Server process not terminated. Pid file %, code = %d, error: %s", FILENAME, errno, strerror(errno)); exit(EXIT_FAILURE); } else syslog(LOG_NOTICE, "terminated" ); closelog(); return 0; }
int main(int argc,char const *argv[]) { int done, i, j, len, n, pid, r, s, s2, t, inet_fd; struct sockaddr_in inet, inet2; struct hostent* hostinfo; char arguments[100],str[10000]; memset(arguments,'\0',sizeof(arguments)); memset(str,'\0',sizeof(str)); /* Initialize the logging interface */ openlog(DAEMON_NAME, LOG_PID, LOG_LOCAL5 ); syslog(LOG_INFO, "starting" ); /* Daemonize */ daemonize( "/var/lock/" DAEMON_NAME); syslog(LOG_INFO, "We did get to daemonize!\n"); /* Now we are a daemon -- do the work for which we were paid */ // Example from Beej's Guide to Network Programming: ////https://beej.us/guide/bgnet/output/html/multipage/sockaddr_inman.html if ((inet_fd = socket(PF_INET, SOCK_STREAM, 0)) == -1) { syslog(LOG_ERR, "%s", strerror(errno)); exit(1); } else syslog(LOG_INFO, IP_ADDRESS); inet.sin_family = AF_INET; inet.sin_port = htons(atoi(PORT)); inet_pton(AF_INET,IP_ADDRESS,&inet.sin_addr); /*(strcpy(inet.sin_path, argv[1])); syslog(LOG_INFO, "path copied!\n"); if(unlink(inet.sin_path)){ syslog(LOG_ERR, strerror(errno)); exit(1); } else syslog(LOG_INFO, "path unlinked!\n");*/ //len = strlen(local.sun_path) + sizeof(local.sun_family); if (bind(inet_fd, (struct sockaddr *)&inet, sizeof inet) == -1) { syslog(LOG_ERR, strerror(errno)); exit(1); } else syslog(LOG_INFO, "socket bound!\n"); if (listen(inet_fd, 5) == -1) { syslog(LOG_ERR, strerror(errno)); exit(1); } syslog(LOG_INFO, "listening for up to 5 connections!\n"); for(;;) { struct sigaction sa; sa.sa_handler = sigchld_handlr; // reap all dead processes sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { //WNOHANG! syslog(LOG_ERR, strerror(errno)); exit(1); } int port=41337; sprintf(str, "%d", port); //syslog(LOG_INFO, "Waiting for a connection...\n"); t = sizeof(inet2); if ((s2 = accept(inet_fd, (struct sockaddr *)&inet2, &t)) == -1) { syslog(LOG_ERR, strerror(errno)); exit(1); } /* fork efter acceptet! */ if((pid=fork())==-1){ // fork och felhantering syslog(LOG_ERR, strerror(errno)); exit(1); } else if(!pid){ //serverbarnet ärver accepten, socketen och fildeskriptorn. //printf("Connected.\n"); i = 0; done = 0; do { r = recv(s2, arguments,100, 0); if (r <= 0) { if (r < 0) perror("recv"); done = 1; //försäkrar oss om att accept-loopen avslutas nedan ... } //om recv returnerar 0 eller -1. if (!done){ //Inget fel eller avslut, enligt tilldelning syslog(LOG_INFO, "!done\n"); if(!(spawn (arguments,s2,str,&i))){ strcpy(arguments,"ENDOFTRANS"); if (send(s2,arguments,100,0) < 0) { //skicka tillbaka strängen perror("send"); done = 1; //försäkrar oss om att accept-loopen avslutas } else done = 0; memset(arguments,'\0',sizeof(arguments)); } } i += 1; } while (!done); //så länge klienten skickar data håller vi öppet 24/7 printf("I'm server %d and my client just signed off!\n",getpid()); syslog(LOG_NOTICE, "terminated" ); closelog(); exit(0); } else close(s2); //if((wate=waitpid(0,NULL,WNOHANG))==-1)perror("waitpid\n"); } /* Finish up */ if((kill_server())){ syslog(LOG_ERR, "Server process not terminated. Pid file %, code = %d, error: %s", FILENAME, errno, strerror(errno)); exit(EXIT_FAILURE); } else syslog(LOG_NOTICE, "terminated" ); closelog(); return 0; }
/** * @brief sighandler Signal process function. * * @param signal System signals. */ static void sighandler(int signal) { LOG_PRINT(LOG_INFO, "Received signal %d: %s. Shutting down.", signal, strsignal(signal)); kill_server(); }