int main(int argc, char **argv) { int is_leader; if (argc < 3) { fprintf(stderr,"Not enough arguments\n"); exit(0); } my_jobid = atoi(argv[1]); my_stepid = atoi(argv[2]); snprintf(linkname, FILENAME_MAX, "%s.%d", FILENAME_PREFIX, my_jobid); snprintf(usockname, FILENAME_MAX, "%s.%d", linkname, my_stepid); snprintf(lockname, FILENAME_MAX, "%s.lock",usockname); if (0 > (lockfd = pmix_create_locked(lockname))) { fprintf(stderr,"Can't create lock file %s\n", lockname); exit(0); } sfd = prepare_srv_socket(usockname); int i = 0; while(1) { int fd = run_discovery(my_jobid, my_stepid, &is_leader); if (is_leader) { fprintf("Iteration %d. I am the leader\n", i); service_requests(fd); } else { monitor_leader(fd); printf("server closed connection. Repeat resolution\n"); } i++; } }
/** * Run discovery for on address (eg broadcast 255.255.255.255) */ static int cw_run_discovery(struct conn *conn, const char *acaddr) { /* get addr of destination */ struct addrinfo hints; struct addrinfo *res, *res0; memset(&hints, 0, sizeof(hints)); hints.ai_socktype = SOCK_DGRAM; hints.ai_family = PF_UNSPEC; int rc = getaddrinfo(acaddr, conf_control_port, &hints, &res0); if (rc) { cw_log(LOG_ERR, "Can't connect to AC %s: %s", acaddr, gai_strerror(rc)); return 0; } for (res = res0; res; res = res->ai_next) { int sockfd; int opt; sockfd = socket(res->ai_family, SOCK_DGRAM, 0); if (sockfd == -1) { cw_log(LOG_ERR, "Can't create socket for %s: %s", acaddr, strerror(errno)); continue; } opt = 1; if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &opt, sizeof(opt)) < 0) { cw_log(LOG_ERR, "Can't set broadcast sockopt"); } sock_set_recvtimeout(sockfd, 1); sock_set_dontfrag(sockfd, 0); sock_copyaddr(&conn->addr, res->ai_addr); if (conf_ip){ struct sockaddr bind_address; sock_strtoaddr(conf_ip,&bind_address); int brc = bind(sockfd,&bind_address,sock_addrlen(&bind_address)); if (brc<0) { cw_log(LOG_ERR,"Can't bind to %s",sock_addr2str(&bind_address)); return 0; } } conn->sock = sockfd; conn->readfrom = conn_recvfrom_packet; run_discovery(conn); conn->readfrom=NULL; close(sockfd); } freeaddrinfo(res0); return 0; }
int main(int argc, char **argv) { isns_server_t *server; isns_source_t *source; isns_db_t *db; int c; #ifdef MTRACE mtrace(); #endif while ((c = getopt_long(argc, argv, "46c:d:Efhr:", options, NULL)) != -1) { switch (c) { case '4': opt_af = AF_INET; break; case '6': opt_af = AF_INET6; break; case 'c': opt_configfile = optarg; break; case 'd': isns_enable_debugging(optarg); break; case 'E': opt_esi = 0; break; case 'f': opt_foreground = 1; break; case 'h': usage(0, NULL); case 'r': if (!strcasecmp(optarg, "initiator")) opt_role = ROLE_INITIATOR; else if (!strcasecmp(optarg, "control") || !strcasecmp(optarg, "monitor")) opt_role = ROLE_MONITOR; else { isns_error("Unknown role \"%s\"\n", optarg); usage(1, NULL); } break; case 'V': printf("Open-iSNS version %s\n" "Copyright (C) 2007, Olaf Kirch <*****@*****.**>\n", OPENISNS_VERSION_STRING); return 0; default: usage(1, "Unknown option"); } } if (optind != argc) usage(1, NULL); /* If the config code derives the source name * automatically, we want it to be distinct from * any other source name (chosen by eg the iSCSI * initiator). Adding a suffix of ":isns" is a * somewhat lame attempt. */ isns_config.ic_source_suffix = "isns"; isns_read_config(opt_configfile); if (!isns_config.ic_source_name) usage(1, "Please specify an iSNS source name"); source = isns_source_create_iscsi(isns_config.ic_source_name); isns_write_pidfile(isns_config.ic_pidfile); if (!opt_foreground) { if (daemon(0, 0) < 0) isns_fatal("Unable to background server process\n"); isns_log_background(); isns_update_pidfile(isns_config.ic_pidfile); } install_sighandler(SIGTERM, sig_cleanup); install_sighandler(SIGINT, sig_cleanup); install_sighandler(SIGUSR2, sig_reread); /* Create a DB object that shadows our portal list. This is for ESI - * when an ESI comes in, the library will look up the portal in this * database, and update its mtime. By checking the mtime at regular * intervals, we can verify whether the server's ESIs actually * reach us. */ db = isns_db_open_shadow(&local_portals); server = isns_create_server(source, db, &isns_callback_service_ops); isns_server_set_scn_callback(server, scn_callback); run_discovery(server); return 0; }