struct mpi_queue *mpi_queue_create(int port) { struct mpi_queue *q = malloc(sizeof(*q)); char *envstring; memset(q, 0, sizeof(*q)); if(port == 0) { envstring = getenv("MPI_QUEUE_PORT"); if(envstring) { port = atoi(envstring); } else { // indicate using a random available port port = -1; } } if(port == -1) { int lowport = 9000; int highport = 32767; envstring = getenv("MPI_QUEUE_LOW_PORT"); if(envstring) lowport = atoi(envstring); envstring = getenv("MPI_QUEUE_HIGH_PORT"); if(envstring) highport = atoi(envstring); for(port = lowport; port < highport; port++) { q->master_link = link_serve(port); if(q->master_link) break; } } else { q->master_link = link_serve(port); } if(!q->master_link) goto failure; q->ready_list = list_create(); q->active_list = itable_create(0); q->complete_list = list_create(); debug(D_WQ, "MPI Queue is listening on port %d.", port); return q; failure: debug(D_NOTICE, "Could not create mpi_queue on port %i.", port); free(q); return 0; }
int main(int argc, char *argv[]) { struct link *link, *list_port = 0; char ch; time_t current; int is_daemon = 0; char *pidfile = NULL; outgoing_host_list = list_create(); debug_config(argv[0]); while((ch = getopt(argc, argv, "bB:d:hH:l:L:m:M:n:o:O:p:ST:u:U:v")) != (char) -1) { switch (ch) { case 'b': is_daemon = 1; break; case 'B': free(pidfile); pidfile = strdup(optarg); break; case 'd': debug_flags_set(optarg); break; case 'h': default: show_help(argv[0]); return 1; case 'l': lifetime = string_time_parse(optarg); break; case 'L': logfilename = strdup(optarg); break; case 'H': history_dir = strdup(optarg); break; case 'm': child_procs_max = atoi(optarg); break; case 'M': max_server_size = string_metric_parse(optarg); break; case 'n': preferred_hostname = optarg; break; case 'o': free(debug_filename); debug_filename = strdup(optarg); break; case 'O': debug_config_file_size(string_metric_parse(optarg)); break; case 'p': port = atoi(optarg); break; case 'S': fork_mode = 0; break; case 'T': child_procs_timeout = string_time_parse(optarg); break; case 'u': list_push_head(outgoing_host_list, xxstrdup(optarg)); break; case 'U': outgoing_timeout = string_time_parse(optarg); break; case 'v': cctools_version_print(stdout, argv[0]); return 0; } } if (is_daemon) daemonize(0, pidfile); debug_config_file(debug_filename); cctools_version_debug(D_DEBUG, argv[0]); if(logfilename) { logfile = fopen(logfilename,"a"); if(!logfile) fatal("couldn't open %s: %s\n",optarg,strerror(errno)); } current = time(0); debug(D_ALL, "*** %s starting at %s", argv[0], ctime(¤t)); if(!list_size(outgoing_host_list)) { list_push_head(outgoing_host_list, CATALOG_HOST_DEFAULT); } install_handler(SIGPIPE, ignore_signal); install_handler(SIGHUP, ignore_signal); install_handler(SIGCHLD, ignore_signal); install_handler(SIGINT, shutdown_clean); install_handler(SIGTERM, shutdown_clean); install_handler(SIGQUIT, shutdown_clean); install_handler(SIGALRM, shutdown_clean); if(!preferred_hostname) { domain_name_cache_guess(hostname); preferred_hostname = hostname; } username_get(owner); starttime = time(0); table = nvpair_database_create(history_dir); if(!table) fatal("couldn't create directory %s: %s\n",history_dir,strerror(errno)); update_dgram = datagram_create(port); if(!update_dgram) fatal("couldn't listen on udp port %d", port); outgoing_dgram = datagram_create(0); if(!outgoing_dgram) fatal("couldn't create outgoing udp port"); list_port = link_serve(port); if(!list_port) fatal("couldn't listen on tcp port %d", port); while(1) { fd_set rfds; int ufd = datagram_fd(update_dgram); int lfd = link_fd(list_port); int result, maxfd; struct timeval timeout; remove_expired_records(); if(time(0) > outgoing_alarm) { update_all_catalogs(outgoing_dgram); outgoing_alarm = time(0) + outgoing_timeout; } while(1) { int status; pid_t pid = waitpid(-1, &status, WNOHANG); if(pid>0) { child_procs_count--; continue; } else { break; } } FD_ZERO(&rfds); FD_SET(ufd, &rfds); if(child_procs_count < child_procs_max) { FD_SET(lfd, &rfds); } maxfd = MAX(ufd, lfd) + 1; timeout.tv_sec = 5; timeout.tv_usec = 0; result = select(maxfd, &rfds, 0, 0, &timeout); if(result <= 0) continue; if(FD_ISSET(ufd, &rfds)) { handle_updates(update_dgram); } if(FD_ISSET(lfd, &rfds)) { link = link_accept(list_port, time(0) + 5); if(link) { if(fork_mode) { pid_t pid = fork(); if(pid == 0) { alarm(child_procs_timeout); handle_query(link); _exit(0); } else if (pid>0) { child_procs_count++; } } else { handle_query(link); } link_close(link); } } } return 1; }
int main(int argc, char *argv[]) { struct link *link, *master; char *subject = 0, *type = 0; time_t stoptime; char line[1024]; signed char c; int portnum = 30000; char *hostname = 0; int timeout = 30; debug_config(argv[0]); static struct option long_options[] = { {"auth", required_argument, 0, 'a'}, {"port", required_argument, 0, 'p'}, {"host", required_argument, 0, 'r'}, {"help", required_argument, 0, 'h'}, {"debug-file", required_argument, 0, 'o'}, {"debug-rotate-max", required_argument, 0, 'O'}, {"debug", required_argument, 0, 'd'}, {0,0,0,0} }; while((c = getopt_long(argc, argv, "a:p:r:d:o:O:", long_options, NULL)) > -1) { switch (c) { case 'p': portnum = atoi(optarg); break; case 'h': show_help(argv[0]); exit(0); break; case 'r': hostname = optarg; break; case 'd': debug_flags_set(optarg); break; case 'o': debug_config_file(optarg); break; case 'O': debug_config_file_size(string_metric_parse(optarg)); break; case 'a': if(!auth_register_byname(optarg)) fatal("couldn't register %s authentication", optarg); break; default: show_use(argv[0]); exit(1); break; } } if(hostname) { char addr[LINK_ADDRESS_MAX]; stoptime = time(0) + timeout; if(!domain_name_cache_lookup(hostname, addr)) fatal("unknown host name: %s", hostname); link = link_connect(addr, portnum, stoptime); if(!link) fatal("couldn't connect to %s:%d: %s", hostname, portnum, strerror(errno)); if(auth_assert(link, &type, &subject, stoptime)) { printf("server thinks I am %s %s\n", type, subject); if(link_readline(link, line, sizeof(line), stoptime)) { printf("got message: %s\n", line); } else { printf("lost connection!\n"); } } else { printf("unable to authenticate.\n"); } link_close(link); } else { stoptime = time(0) + timeout; master = link_serve(portnum); if(!master) fatal("couldn't serve port %d: %s\n", portnum, strerror(errno)); while(time(0) < stoptime) { link = link_accept(master, stoptime); if(!link) continue; if(auth_accept(link, &type, &subject, stoptime)) { time_t t = time(0); link_putfstring(link, "Hello %s:%s, it is now %s", stoptime, type, subject, ctime(&t)); /* ctime ends with newline */ } else { printf("couldn't auth accept\n"); } link_close(link); } } return 0; }