int main(int argc, char **argv) { static struct poptOption options[] = { { "dummy", 'd', POPT_ARG_NONE, &dummy, 0, "Use the dummy terminal mode", NULL }, { NULL, '\0', 0, NULL } }; dummy = FALSE; quitting = FALSE; core_init_paths(argc, argv); check_files(); #ifdef WIN32 winsock_init(); #endif #ifdef HAVE_SOCKS SOCKSinit(argv[0]); #endif #ifdef ENABLE_NLS /* initialize the i18n stuff */ bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); #endif /* setlocale() must be called at the beginning before any calls that affect it, especially regexps seem to break if they're generated before t his call. locales aren't actually used for anything else than autodetection of UTF-8 currently.. */ setlocale(LC_CTYPE, ""); textui_init(); args_register(options); args_execute(argc, argv); if (!dummy && !term_init()) { fprintf(stderr, "Can't initialize screen handling, quitting.\n"); fprintf(stderr, "You can still use the dummy mode with -d parameter\n"); return 1; } textui_finish_init(); main_loop = g_main_new(TRUE); /* Does the same as g_main_run(main_loop), except we can call our dirty-checker after each iteration */ while (!quitting) { if (!dummy) term_refresh_freeze(); g_main_iteration(TRUE); if (!dummy) term_refresh_thaw(); if (reload_config) { /* SIGHUP received, do /RELOAD */ reload_config = FALSE; signal_emit("command reload", 1, ""); } dirty_check(); } g_main_destroy(main_loop); textui_deinit(); session_upgrade(); /* if we /UPGRADEd, start the new process */ return 0; }
/** * @brief * main - the initialization and main loop of pbs_daemon */ int main(int argc, char *argv[]) { char jobfile[MAXPATHLEN+1]; char jobfile_full[MAXPATHLEN+1]; pbs_net_t hostaddr = 0; int port = -1; int move_type = -1; pbs_list_head attrl; enum conn_type cntype = ToServerDIS; int con = -1; char *destin; int encode_type; int i; job *jobp; char job_id[PBS_MAXSVRJOBID+1]; attribute *pattr; struct attropl *pqjatr; /* list (single) of attropl for quejob */ char script_name[MAXPATHLEN+1]; int in_server = -1; char *param_name, *param_val; char buf[4096]; struct hostent *hp; struct in_addr addr; char *credbuf = NULL; size_t credlen = 0; int prot = PROT_TCP; /*the real deal or output version and exit?*/ execution_mode(argc, argv); /* If we are not run with real and effective uid of 0, forget it */ pbs_loadconf(0); if (!isAdminPrivilege(getlogin())) { fprintf(stderr, "%s: Must be run by root\n", argv[0]); exit(SEND_JOB_FATAL); } /* initialize the pointers in the resource_def array */ for (i = 0; i < (svr_resc_size - 1); ++i) svr_resc_def[i].rs_next = &svr_resc_def[i+1]; /* last entry is left with null pointer */ /* set single threaded mode */ pbs_client_thread_set_single_threaded_mode(); /* disable attribute verification */ set_no_attribute_verification(); /* initialize the thread context */ if (pbs_client_thread_init_thread_context() != 0) { fprintf(stderr, "%s: Unable to initialize thread context\n", argv[0]); exit(SEND_JOB_FATAL); } if(set_msgdaemonname("PBS_send_job")) { fprintf(stderr, "Out of memory\n"); return 1; } winsock_init(); connection_init(); while (fgets(buf, sizeof(buf), stdin) != NULL) { buf[strlen(buf)-1] = '\0'; /* gets rid of newline */ param_name = buf; param_val = strchr(buf, '='); if (param_val) { *param_val = '\0'; param_val++; } else { /* bad param_val -- skipping */ break; } if (strcmp(param_name, "jobfile") == 0) { jobfile[0] = '\0'; strncpy(jobfile, param_val, MAXPATHLEN); } else if (strcmp(param_name, "destaddr") == 0) { hostaddr = atol(param_val); } else if (strcmp(param_name, "destport") == 0) { port = atoi(param_val); } else if (strcmp(param_name, "move_type") == 0) { move_type = atoi(param_val); } else if (strcmp(param_name, "in_server") == 0) { in_server = atoi(param_val); } else if (strcmp(param_name, "server_name") == 0) { server_name[0] = '\0'; strncpy(server_name, param_val, PBS_MAXSERVERNAME); } else if (strcmp(param_name, "server_host") == 0) { server_host[0] = '\0'; strncpy(server_host, param_val, (sizeof(server_host) - 1)); } else if (strcmp(param_name, "server_addr") == 0) { pbs_server_addr = atol(param_val); } else if (strcmp(param_name, "server_port") == 0) { pbs_server_port_dis = atoi(param_val); } else if (strcmp(param_name, "log_file") == 0) { log_file = strdup(param_val); } else if (strcmp(param_name, "path_log") == 0) { path_log[0] = '\0'; strncpy(path_log, param_val, MAXPATHLEN); } else if (strcmp(param_name, "path_jobs") == 0) { path_jobs = strdup(param_val); } else if (strcmp(param_name, "path_spool") == 0) { path_spool = strdup(param_val); } else if (strcmp(param_name, "path_rescdef") == 0) { path_rescdef = strdup(param_val); } else if (strcmp(param_name, "path_users") == 0) { path_users = strdup(param_val); } else if (strcmp(param_name, "path_hooks_workdir") == 0) { path_hooks_workdir = strdup(param_val); if (path_hooks_workdir == NULL) exit(SEND_JOB_FATAL); } else if (strcmp(param_name, "svr_history_enable") == 0) { svr_history_enable = atol(param_val); } else if (strcmp(param_name, "svr_history_duration") == 0) { svr_history_duration = atol(param_val); } else if (strcmp(param_name, "single_signon_password_enable") == 0) { if (decode_b(&server.sv_attr[(int)SRV_ATR_ssignon_enable], NULL, NULL, param_val) != 0) { fprintf(stderr, "%s: failed to set ssignon_password_enable\n", argv[0]); exit(SEND_JOB_FATAL); } } else if (strcmp(param_name, "script_name") == 0) { strncpy(script_name, param_val, MAXPATHLEN + 1); } else break; } time(&time_now); (void)log_open_main(log_file, path_log, 1); /* silent open */ if (setup_resc(1) == -1) { /* log_buffer set in setup_resc */ log_err(-1, "pbsd_send_job(setup_resc)", log_buffer); return (-1); } if( strlen(jobfile) == 0 || hostaddr == 0 || port == 0 || move_type == -1 || \ in_server == -1 || strlen(server_name) == 0 || strlen(server_host) == 0 || \ pbs_server_addr == 0 || pbs_server_port_dis == 0 || \ strlen(path_log) == 0 || path_jobs == NULL || \ path_spool == NULL || path_users == NULL ) { log_err(-1, "pbs_send_job", "error on one of the parameters"); log_close(0); /* silent close */ exit(SEND_JOB_FATAL); } CLEAR_HEAD(task_list_immed); CLEAR_HEAD(task_list_timed); CLEAR_HEAD(task_list_event); CLEAR_HEAD(svr_queues); CLEAR_HEAD(svr_alljobs); CLEAR_HEAD(svr_newjobs); CLEAR_HEAD(svr_allresvs); CLEAR_HEAD(svr_newresvs); CLEAR_HEAD(svr_deferred_req); CLEAR_HEAD(svr_unlicensedjobs); strcpy(jobfile_full, path_jobs); strcat(jobfile_full, jobfile); if (chk_save_file(jobfile_full) != 0) { sprintf(log_buffer, "Error opening jobfile=%s", jobfile); log_err(-1, __func__, log_buffer); goto fatal_exit; } if ((jobp=job_recov_fs(jobfile, RECOV_SUBJOB)) == NULL) { sprintf(log_buffer, "Failed to recreate job in jobfile=%s", jobfile); log_err(-1, __func__, log_buffer); goto fatal_exit; } /* now delete the temp job file that was created by job_save_fs in server code * jobs are in database now, no need to keep in filesystem */ unlink(jobfile_full); if (in_server) append_link(&svr_alljobs, &jobp->ji_alljobs, jobp); /* select attributes/resources to send based on move type */ if (move_type == MOVE_TYPE_Exec) { resc_access_perm = ATR_DFLAG_MOM; encode_type = ATR_ENCODE_MOM; cntype = ToServerDIS; } else { resc_access_perm = ATR_DFLAG_USWR | ATR_DFLAG_OPWR | ATR_DFLAG_MGWR | ATR_DFLAG_SvRD; encode_type = ATR_ENCODE_SVR; svr_dequejob(jobp); } CLEAR_HEAD(attrl); pattr = jobp->ji_wattr; for (i=0; i < (int)JOB_ATR_LAST; i++) { if ((job_attr_def+i)->at_flags & resc_access_perm) { (void)(job_attr_def+i)->at_encode(pattr+i, &attrl, (job_attr_def+i)->at_name, NULL, encode_type, NULL); } } attrl_fixlink(&attrl); /* script name is passed from parent */ /* get host name */ pbs_loadconf(0); addr.s_addr = htonl(hostaddr); hp = gethostbyaddr((void *)&addr, sizeof(struct in_addr), AF_INET); if (hp == NULL) { sprintf(log_buffer, "%s: h_errno=%d", inet_ntoa(addr), h_errno); log_err(-1, __func__, log_buffer); } else { /* read any credential file */ (void)get_credential(hp->h_name, jobp, PBS_GC_BATREQ, &credbuf, &credlen); } /* save the job id for when after we purge the job */ (void)strcpy(job_id, jobp->ji_qs.ji_jobid); con = -1; DIS_tcparray_init(); for (i=0; i<RETRY; i++) { pbs_errno = 0; /* connect to receiving server with retries */ if (i > 0) { /* recycle after an error */ if (con >= 0) svr_disconnect(con); if (should_retry_route(pbs_errno) == -1) { goto fatal_exit; /* fatal error, don't retry */ } sleep(1<<i); } if ((con = svr_connect(hostaddr, port, 0, cntype, prot)) == PBS_NET_RC_FATAL) { (void)sprintf(log_buffer, "send_job failed to %lx port %d", hostaddr, port); log_err(pbs_errno, __func__, log_buffer); goto fatal_exit; } else if (con == PBS_NET_RC_RETRY) { pbs_errno = WSAECONNREFUSED; /* should retry */ continue; } /* * if the job is substate JOB_SUBSTATE_TRNOUTCM which means * we are recovering after being down or a late failure, we * just want to send the "read-to-commit/commit" */ if (jobp->ji_qs.ji_substate != JOB_SUBSTATE_TRNOUTCM) { if (jobp->ji_qs.ji_substate != JOB_SUBSTATE_TRNOUT) { jobp->ji_qs.ji_substate = JOB_SUBSTATE_TRNOUT; } pqjatr = &((svrattrl *)GET_NEXT(attrl))->al_atopl; destin = jobp->ji_qs.ji_destin; if (PBSD_queuejob(con, jobp->ji_qs.ji_jobid, destin, pqjatr, NULL, prot, NULL)== 0) { if (pbs_errno == PBSE_JOBEXIST && move_type == MOVE_TYPE_Exec) { /* already running, mark it so */ log_event(PBSEVENT_ERROR, PBS_EVENTCLASS_JOB, LOG_INFO, jobp->ji_qs.ji_jobid, "Mom reports job already running"); goto ok_exit; } else if ((pbs_errno == PBSE_HOOKERROR) || (pbs_errno == PBSE_HOOK_REJECT) || (pbs_errno == PBSE_HOOK_REJECT_RERUNJOB) || (pbs_errno == PBSE_HOOK_REJECT_DELETEJOB)) { char name_buf[MAXPATHLEN+1]; int rfd; int len; char *reject_msg; int err; err = pbs_errno; reject_msg = pbs_geterrmsg(con); (void)snprintf(log_buffer, sizeof(log_buffer), "send of job to %s failed error = %d reject_msg=%s", destin, err, reject_msg?reject_msg:""); log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, LOG_INFO, jobp->ji_qs.ji_jobid, log_buffer); (void)strcpy(name_buf, path_hooks_workdir); (void)strcat(name_buf, jobp->ji_qs.ji_jobid); (void)strcat(name_buf, HOOK_REJECT_SUFFIX); if ((reject_msg != NULL) && (reject_msg[0] != '\0')) { if ((rfd = open(name_buf, O_RDWR|O_CREAT|O_TRUNC, 0600)) == -1) { snprintf(log_buffer, sizeof(log_buffer), "open of reject file %s failed: errno %d", name_buf, errno); log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, LOG_INFO, jobp->ji_qs.ji_jobid, log_buffer); } else { secure_file(name_buf, "Administrators", READS_MASK|WRITES_MASK|STANDARD_RIGHTS_REQUIRED); setmode(rfd, O_BINARY); len = strlen(reject_msg)+1; /* write also trailing null char */ if (write(rfd, reject_msg, len) != len) { snprintf(log_buffer, sizeof(log_buffer), "write to file %s incomplete: errno %d", name_buf, errno); log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, LOG_INFO, jobp->ji_qs.ji_jobid, log_buffer); } close(rfd); } } if (err == PBSE_HOOKERROR) exit(SEND_JOB_HOOKERR); if (err == PBSE_HOOK_REJECT) exit(SEND_JOB_HOOK_REJECT); if (err == PBSE_HOOK_REJECT_RERUNJOB) exit(SEND_JOB_HOOK_REJECT_RERUNJOB); if (err == PBSE_HOOK_REJECT_DELETEJOB) exit(SEND_JOB_HOOK_REJECT_DELETEJOB); } else { (void)sprintf(log_buffer, "send of job to %s failed error = %d", destin, pbs_errno); log_event(PBSEVENT_JOB, PBS_EVENTCLASS_JOB, LOG_INFO, jobp->ji_qs.ji_jobid, log_buffer); continue; } } if (jobp->ji_qs.ji_svrflags & JOB_SVFLG_SCRIPT) { if (PBSD_jscript(con, script_name, prot, NULL) != 0) continue; } if (credlen > 0) { int ret; ret = PBSD_jcred(con, jobp->ji_extended.ji_ext.ji_credtype, credbuf, credlen, prot, NULL); if ((ret == 0) || (i == (RETRY - 1))) free(credbuf); /* free credbuf if credbuf is sent successfully OR */ /* at the end of all retry attempts */ if (ret != 0) continue; } if ((move_type == MOVE_TYPE_Exec) && (jobp->ji_qs.ji_svrflags & JOB_SVFLG_HASRUN) && (hostaddr != pbs_server_addr)) { /* send files created on prior run */ if ((move_job_file(con, jobp, StdOut, prot) != 0) || (move_job_file(con, jobp, StdErr, prot) != 0) || (move_job_file(con, jobp, Chkpt, prot) != 0)) continue; } jobp->ji_qs.ji_substate = JOB_SUBSTATE_TRNOUTCM; } if (PBSD_rdytocmt(con, job_id, prot, NULL) != 0) continue; if (PBSD_commit(con, job_id, prot, NULL) != 0) goto fatal_exit; goto ok_exit; /* This child process is all done */ } if (con >= 0) svr_disconnect(con); /* * If connection is actively refused by the execution node(or mother superior) OR * the execution node(or mother superior) is rejecting request with error * PBSE_BADHOST(failing to authorize server host), the node should be marked down. */ if ((move_type == MOVE_TYPE_Exec) && (pbs_errno == WSAECONNREFUSED || pbs_errno == PBSE_BADHOST)) { i = SEND_JOB_NODEDW; } else if (should_retry_route(pbs_errno) == -1) { i = SEND_JOB_FATAL; } else { i = SEND_JOB_RETRY; } (void)sprintf(log_buffer, "send_job failed with error %d", pbs_errno); log_event(PBSEVENT_DEBUG, PBS_EVENTCLASS_JOB, LOG_NOTICE, jobp->ji_qs.ji_jobid, log_buffer); log_close(0); net_close(-1); unlink(script_name); exit(i); fatal_exit: if (con >= 0) svr_disconnect(con); log_close(0); net_close(-1); unlink(script_name); exit(SEND_JOB_FATAL); ok_exit: if (con >= 0) svr_disconnect(con); log_close(0); net_close(-1); unlink(script_name); exit(SEND_JOB_OK); }
int main(int argc, char **argv) { /* * This routine sends a Run Job request to the batch server. If the * batch request is accepted, the server will have started the execution * of the job. */ char job[PBS_MAXCLTJOBID]; /* Job Id */ char server[MAXSERVERNAME]; /* Server name */ char *location = NULL; /* Where to run the job */ static char opts[] = "H:a"; /* See man getopt */ static char *usage = "Usage: qrun [-a] [-H vnode_specification ] job_identifier_list\n" " qrun [-a] [-H - ] job_identifier_list\n" " qrun --version\n"; int s; int errflg = 0; /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif /* Command line options */ while ((s = getopt(argc, argv, opts)) != EOF) switch (s) { case 'H': if (strlen(optarg) == 0) { fprintf(stderr, "qrun: illegal -H value\n"); errflg++; break; } location = optarg; break; case 'a': async = 1; break; case '?': default: errflg++; break; } if (errflg || (optind >= argc)) { fprintf(stderr, "%s", usage); exit(1); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qrun: unable to initialize security library.\n"); exit(2); } for (; optind < argc; optind++) { if (get_server(argv[optind], job, server)) { fprintf(stderr, "qrun: illegally formed job identifier: %s\n", argv[optind]); exitstatus = 1; continue; } execute(job, server, location); } /*cleanup security library initializations before exiting*/ CS_close_app(); exit(exitstatus); }
int main(int argc, char **argv, char **envp) /* qhold */ { int c; int errflg=0; int any_failed=0; char job_id[PBS_MAXCLTJOBID]; /* from the command line */ char job_id_out[PBS_MAXCLTJOBID]; char server_out[MAXSERVERNAME]; char rmt_server[MAXSERVERNAME]; struct ecl_attribute_errors *err_list; #define MAX_HOLD_TYPE_LEN 32 char hold_type[MAX_HOLD_TYPE_LEN+1]; #define GETOPT_ARGS "h:-:" /*test for real deal or just version and exit*/ execution_mode(argc, argv); #ifdef WIN32 winsock_init(); #endif hold_type[0]='\0'; while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) switch (c) { case 'h': while (isspace((int)*optarg)) optarg++; if (optarg[0] == '\0') { fprintf(stderr, "qhold: illegal -h value\n"); errflg++; } else strcpy(hold_type, optarg); break; default : errflg++; } if (errflg || optind >= argc) { print_usage(); exit(2); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qhold: unable to initialize security library.\n"); exit(2); } for (; optind < argc; optind++) { int connect; int stat=0; int located = FALSE; strcpy(job_id, argv[optind]); if (get_server(job_id, job_id_out, server_out)) { fprintf(stderr, "qhold: illegally formed job identifier: %s\n", job_id); any_failed = 1; continue; } cnt: connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qhold: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); any_failed = pbs_errno; continue; } stat = pbs_holdjob(connect, job_id_out, hold_type, NULL); if (stat && (err_list = pbs_get_attributes_in_error(connect))) handle_attribute_errors(connect, err_list); if (stat && (pbs_errno != PBSE_UNKJOBID)) { prt_job_err("qhold", connect, job_id_out); any_failed = pbs_errno; } else if (stat && (pbs_errno == PBSE_UNKJOBID) && !located) { located = TRUE; if (locate_job(job_id_out, server_out, rmt_server)) { pbs_disconnect(connect); strcpy(server_out, rmt_server); goto cnt; } prt_job_err("qhold", connect, job_id_out); any_failed = pbs_errno; } pbs_disconnect(connect); } /*cleanup security library initializations before exiting*/ CS_close_app(); exit(any_failed); }
int main(int argc, char **argv, char **envp) /* pbs_release_nodes */ { int c; int errflg=0; int any_failed=0; char job_id[PBS_MAXCLTJOBID]; /* from the command line */ char job_id_out[PBS_MAXCLTJOBID]; char server_out[MAXSERVERNAME]; char rmt_server[MAXSERVERNAME]; int len; char *node_list = NULL; int connect; int stat=0; int k; int all_opt = 0; #define GETOPT_ARGS "j:a" /*test for real deal or just version and exit*/ execution_mode(argc, argv); #ifdef WIN32 winsock_init(); #endif job_id[0] = '\0'; while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) { switch (c) { case 'j': strncpy(job_id, optarg, PBS_MAXCLTJOBID); job_id[PBS_MAXCLTJOBID-1] = '\0'; break; case 'a': all_opt = 1; break; default : errflg++; } } if (job_id[0] == '\0') { char *jid; jid = getenv("PBS_JOBID"); strncpy(job_id, jid?jid:"", PBS_MAXCLTJOBID); job_id[PBS_MAXCLTJOBID-1] = '\0'; } if (errflg || ((optind == argc) && !all_opt) || ((optind != argc) && all_opt) ) { fprintf(stderr, USAGE); fprintf(stderr, USAGE2); fprintf(stderr, USAGE3); exit(2); } if (job_id[0] == '\0') { fprintf(stderr, "pbs_release_nodes: No jobid given\n"); exit(2); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "pbs_release_nodes: unable to initialize security library.\n"); exit(2); } len = 0; for (k = optind; k < argc; k++) { len += (strlen(argv[k]) + 1); /* +1 for space */ } node_list = (char *)malloc(len + 1); if (node_list == NULL) { fprintf(stderr, "failed to malloc to store data (error %d)", errno); exit(2); } node_list[0] = '\0'; for (k = optind; k < argc; k++) { if (k != optind) strcat(node_list, "+"); strcat(node_list, argv[k]); } if (get_server(job_id, job_id_out, server_out)) { fprintf(stderr, "pbs_release_nodes: illegally formed job identifier: %s\n", job_id); free(node_list); exit(2); } pbs_errno = 0; stat = 0; while(1) { connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "pbs_release_nodes: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); break; } stat = pbs_relnodesjob(connect, job_id_out, node_list, NULL); if (stat && (pbs_errno == PBSE_UNKJOBID)) { if (locate_job(job_id_out, server_out, rmt_server)) { /* * job located at a different server * retry connect on the new server */ pbs_disconnect(connect); strcpy(server_out, rmt_server); } else { prt_job_err("pbs_release_nodes", connect, job_id_out); break; } } else { char *info_msg; if (stat && (pbs_errno != PBSE_UNKJOBID)) { prt_job_err("pbs_release_nodes", connect, ""); } else if ((info_msg = pbs_geterrmsg(connect)) != NULL) { /* print potential warning message */ printf("pbs_release_nodes: %s\n", info_msg); } break; } } any_failed = pbs_errno; pbs_disconnect(connect); /*cleanup security library initializations before exiting*/ CS_close_app(); exit(any_failed); }
int main (int argc, char *argv[]) { static const char *options = "rv?"; static const struct option long_options[] = { { "help", 0, 0, '?' }, { "verbose", 0, 0, 'v' }, { 0, 0, 0, 0 } }; int c; char *cmdline; ignore_value (chdir ("/")); if (winsock_init () == -1) error (EXIT_FAILURE, 0, "winsock initialization failed"); #ifdef HAVE_REGISTER_PRINTF_SPECIFIER /* http://udrepper.livejournal.com/20948.html */ register_printf_specifier ('Q', print_shell_quote, print_arginfo); register_printf_specifier ('R', print_sysroot_shell_quote, print_arginfo); #else #ifdef HAVE_REGISTER_PRINTF_FUNCTION register_printf_function ('Q', print_shell_quote, print_arginfo); register_printf_function ('R', print_sysroot_shell_quote, print_arginfo); #else #error "HAVE_REGISTER_PRINTF_{SPECIFIER|FUNCTION} not defined" #endif #endif struct stat statbuf; if (stat ("/", &statbuf) == 0) root_device = statbuf.st_dev; for (;;) { c = getopt_long (argc, argv, options, long_options, NULL); if (c == -1) break; switch (c) { /* The -r flag is used when running standalone. It changes * several aspects of the daemon. */ case 'r': sysroot = ""; sysroot_len = 0; autosync_umount = 0; break; case 'v': verbose = 1; break; case '?': usage (); exit (EXIT_SUCCESS); default: fprintf (stderr, "guestfsd: unexpected command line option 0x%x\n", c); exit (EXIT_FAILURE); } } if (optind < argc) { usage (); exit (EXIT_FAILURE); } cmdline = read_cmdline (); /* Set the verbose flag. */ verbose = verbose || (cmdline && strstr (cmdline, "guestfs_verbose=1") != NULL); if (verbose) printf ("verbose daemon enabled\n"); if (verbose) { if (cmdline) printf ("linux commmand line: %s\n", cmdline); else printf ("could not read linux command line\n"); } free (cmdline); #ifndef WIN32 /* Make sure SIGPIPE doesn't kill us. */ struct sigaction sa; memset (&sa, 0, sizeof sa); sa.sa_handler = SIG_IGN; sa.sa_flags = 0; if (sigaction (SIGPIPE, &sa, NULL) == -1) perror ("sigaction SIGPIPE"); /* but try to continue anyway ... */ #endif #ifdef WIN32 # define setenv(n,v,f) _putenv(n "=" v) #endif /* Set up a basic environment. After we are called by /init the * environment is essentially empty. * https://bugzilla.redhat.com/show_bug.cgi?id=502074#c5 * * NOTE: if you change $PATH, you must also change 'prog_exists' * function below. */ setenv ("PATH", "/sbin:/usr/sbin:/bin:/usr/bin", 1); setenv ("SHELL", "/bin/sh", 1); setenv ("LC_ALL", "C", 1); setenv ("TERM", "dumb", 1); #ifndef WIN32 /* We document that umask defaults to 022 (it should be this anyway). */ umask (022); #else /* This is the default for Windows anyway. It's not even clear if * Windows ever uses this -- the MSDN documentation for the function * contains obvious errors. */ _umask (0); #endif /* Make a private copy of /etc/lvm so we can change the config (see * daemon/lvm-filter.c). */ copy_lvm (); /* Connect to virtio-serial channel. */ int sock = open (VIRTIO_SERIAL_CHANNEL, O_RDWR|O_CLOEXEC); if (sock == -1) { fprintf (stderr, "\n" "Failed to connect to virtio-serial channel.\n" "\n" "This is a fatal error and the appliance will now exit.\n" "\n" "Usually this error is caused by either QEMU or the appliance\n" "kernel not supporting the vmchannel method that the\n" "libguestfs library chose to use. Please run\n" "'libguestfs-test-tool' and provide the complete, unedited\n" "output to the libguestfs developers, either in a bug report\n" "or on the libguestfs redhat com mailing list.\n" "\n"); perror (VIRTIO_SERIAL_CHANNEL); exit (EXIT_FAILURE); } /* Wait for udev devices to be created. If you start libguestfs, * especially with disks that contain complex (eg. mdadm) data * already, then it is possible for the 'mdadm' and LVM commands * that the init script runs to have not completed by the time the * daemon starts executing library commands. (This is very rare and * hard to test however, but we have seen it in 'brew'). Run * udev_settle, but do it as late as possible to minimize the chance * that we'll have to do any waiting here. */ udev_settle (); /* Send the magic length message which indicates that * userspace is up inside the guest. */ char lenbuf[4]; XDR xdr; uint32_t len = GUESTFS_LAUNCH_FLAG; xdrmem_create (&xdr, lenbuf, sizeof lenbuf, XDR_ENCODE); xdr_u_int (&xdr, &len); if (xwrite (sock, lenbuf, sizeof lenbuf) == -1) { perror ("xwrite"); exit (EXIT_FAILURE); } xdr_destroy (&xdr); /* Enter the main loop, reading and performing actions. */ main_loop (sock); exit (EXIT_SUCCESS); }
int main(int argc, char **argv, char **envp) /* qselect */ { int c; int errflg=0; char *errmsg; #define MAX_OPTARG_LEN 256 #define MAX_RESOURCE_NAME_LEN 256 char optargout[MAX_OPTARG_LEN+1]; char resource_name[MAX_RESOURCE_NAME_LEN+1]; enum batch_op op; enum batch_op *pop = &op; struct attropl *select_list = 0; static char destination[PBS_MAXQUEUENAME+1] = ""; char server_out[MAXSERVERNAME] = ""; char *queue_name_out; char *server_name_out; int connect; char **selectjob_list; char *res_pos; char *pc; time_t after; char a_value[80]; char extendopts[4] = ""; char *attr_time = NULL; struct ecl_attribute_errors *err_list; char *resc_time = NULL; #define GETOPT_ARGS "a:A:c:h:HJl:N:p:q:r:s:t:Tu:xP:" /*test for real deal or just version and exit*/ execution_mode(argc, argv); #ifdef WIN32 winsock_init(); #endif while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) switch (c) { case 'a': check_op(optarg, pop, optargout); if ((after = cvtdate(optargout)) < 0) { fprintf(stderr, "qselect: illegal -a value\n"); errflg++; break; } sprintf(a_value, "%ld", (long)after); set_attrop(&select_list, ATTR_a, NULL, a_value, op); break; case 'c': check_op(optarg, pop, optargout); pc = optargout; while (isspace((int)*pc)) pc++; if (strlen(pc) == 0) { fprintf(stderr, "qselect: illegal -c value\n"); errflg++; break; } set_attrop(&select_list, ATTR_c, NULL, optargout, op); break; case 'h': check_op(optarg, pop, optargout); pc = optargout; while (isspace((int)*pc)) pc++; set_attrop(&select_list, ATTR_h, NULL, optargout, op); break; case 'J': op = EQ; set_attrop(&select_list, ATTR_array, NULL, "True", op); break; case 'l': res_pos = optarg; while (*res_pos != '\0') { if (check_res_op(res_pos, resource_name, pop, optargout, &res_pos) != 0) { errflg++; break; } set_attrop(&select_list, ATTR_l, resource_name, optargout, op); } break; case 'p': check_op(optarg, pop, optargout); set_attrop(&select_list, ATTR_p, NULL, optargout, op); break; case 'q': strcpy(destination, optarg); check_op(optarg, pop, optargout); set_attrop(&select_list, ATTR_q, NULL, optargout, op); break; case 'r': op = EQ; pc = optarg; while (isspace((int)(*pc))) pc++; if (*pc != 'y' && *pc != 'n') { /* qselect specific check - stays */ fprintf(stderr, "qselect: illegal -r value\n"); errflg++; break; } set_attrop(&select_list, ATTR_r, NULL, pc, op); break; case 's': check_op(optarg, pop, optargout); pc = optargout; while (isspace((int)(*pc))) pc++; set_attrop(&select_list, ATTR_state, NULL, optargout, op); break; case 't': if (get_tsubopt(*optarg, &attr_time, &resc_time)) { fprintf(stderr, "qselect: illegal -t value\n"); errflg++; break; } /* 1st character possess the subopt, so send optarg++ */ optarg ++; check_op(optarg, pop, optargout); if ((after = cvtdate(optargout)) < 0) { fprintf(stderr, "qselect: illegal -t value\n"); errflg++; break; } sprintf(a_value, "%ld", (long)after); set_attrop(&select_list, attr_time, resc_time, a_value, op); break; case 'T': if (strchr(extendopts, (int)'T') == NULL) (void)strcat(extendopts, "T"); break; case 'x': if (strchr(extendopts, (int)'x') == NULL) (void)strcat(extendopts, "x"); break; case 'H': op = EQ; if (strchr(extendopts, (int)'x') == NULL) (void)strcat(extendopts, "x"); set_attrop(&select_list, ATTR_state, NULL, "FM", op); break; case 'u': op = EQ; set_attrop(&select_list, ATTR_u, NULL, optarg, op); break; case 'A': op = EQ; set_attrop(&select_list, ATTR_A, NULL, optarg, op); break; case 'P': op = EQ; set_attrop(&select_list, ATTR_project, NULL, optarg, op); break; case 'N': op = EQ; set_attrop(&select_list, ATTR_N, NULL, optarg, op); break; default : errflg++; } if (errflg || (optind < argc)) { print_usage(); exit(2); } if (notNULL(destination)) { if (parse_destination_id(destination, &queue_name_out, &server_name_out)) { fprintf(stderr, "qselect: illegally formed destination: %s\n", destination); exit(2); } else { if (notNULL(server_name_out)) { strcpy(server_out, server_name_out); } } } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qselect: unable to initialize security library.\n"); exit(2); } connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qselect: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); /*cleanup security library initializations before exiting*/ CS_close_app(); exit(pbs_errno); } if (extendopts[0] == '\0') selectjob_list = pbs_selectjob(connect, select_list, NULL); else selectjob_list = pbs_selectjob(connect, select_list, extendopts); if (selectjob_list == NULL) { if ((err_list = pbs_get_attributes_in_error(connect))) handle_attribute_errors(err_list); if (pbs_errno != PBSE_NONE) { errmsg = pbs_geterrmsg(connect); if (errmsg != NULL) { fprintf(stderr, "qselect: %s\n", errmsg); } else { fprintf(stderr, "qselect: Error (%d) selecting jobs\n", pbs_errno); } /* * If the server is not configured for history jobs i.e. * job_history_enable svr attr is unset/set to FALSE, qselect * command with -x/-H option is being used, then pbs_selectjob() * will return PBSE_JOBHISTNOTSET error code. But command will * exit with exit code '0'after printing the corresponding error * message. i.e. "job_history_enable is set to false" */ if (pbs_errno == PBSE_JOBHISTNOTSET) pbs_errno = 0; /*cleanup security library initializations before exiting*/ CS_close_app(); exit(pbs_errno); } } else { /* got some jobs ids */ int i = 0; while (selectjob_list[i] != NULL) { printf("%s\n", selectjob_list[i++]); } free(selectjob_list); } pbs_disconnect(connect); /*cleanup security library initializations before exiting*/ CS_close_app(); exit(0); }
void grpc_iomgr_platform_init(void) { winsock_init(); grpc_iocp_init(); }
int main(int argc, char **argv, char **envp) /* qrls */ { int c; int errflg=0; int any_failed=0; int u_cnt, o_cnt, s_cnt, n_cnt, p_cnt; char *pc; char job_id[PBS_MAXCLTJOBID]; /* from the command line */ char job_id_out[PBS_MAXCLTJOBID]; char server_out[MAXSERVERNAME]; char rmt_server[MAXSERVERNAME]; #define MAX_HOLD_TYPE_LEN 32 char hold_type[MAX_HOLD_TYPE_LEN+1]; #define GETOPT_ARGS "h:" /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif hold_type[0]='\0'; while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) switch (c) { case 'h': while (isspace((int)*optarg)) optarg++; if (strlen(optarg) == 0) { fprintf(stderr, "qrls: illegal -h value\n"); errflg++; break; } pc = optarg; u_cnt = o_cnt = s_cnt = n_cnt = p_cnt = 0; while (*pc) { if (*pc == 'u') u_cnt++; else if (*pc == 'o') o_cnt++; else if (*pc == 's') s_cnt++; else if (*pc == 'p') p_cnt++; else if (*pc == 'n') n_cnt++; else { fprintf(stderr, "qrls: illegal -h value\n"); errflg++; break; } pc++; } if (n_cnt && (u_cnt + o_cnt + s_cnt + p_cnt)) { fprintf(stderr, "qrls: illegal -h value\n"); errflg++; break; } strcpy(hold_type, optarg); break; default : errflg++; } if (errflg || optind >= argc) { static char usage[]="usage: qrls [-h hold_list] job_identifier...\n"; static char usag2[]=" qrls --version\n"; fprintf(stderr, "%s", usage); fprintf(stderr, "%s", usag2); exit(2); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qrls: unable to initialize security library.\n"); exit(1); } for (; optind < argc; optind++) { int connect; int stat=0; int located = FALSE; strcpy(job_id, argv[optind]); if (get_server(job_id, job_id_out, server_out)) { fprintf(stderr, "qrls: illegally formed job identifier: %s\n", job_id); any_failed = 1; continue; } cnt: connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qrls: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); any_failed = pbs_errno; continue; } stat = pbs_rlsjob(connect, job_id_out, hold_type, NULL); if (stat && (pbs_errno != PBSE_UNKJOBID)) { prt_job_err("qrls", connect, job_id_out); any_failed = pbs_errno; } else if (stat && (pbs_errno == PBSE_UNKJOBID) && !located) { located = TRUE; if (locate_job(job_id_out, server_out, rmt_server)) { pbs_disconnect(connect); strcpy(server_out, rmt_server); goto cnt; } prt_job_err("qrls", connect, job_id_out); any_failed = pbs_errno; } pbs_disconnect(connect); } /*cleanup security library initializations before exiting*/ CS_close_app(); exit(any_failed); }
int main(int argc, char **argv, char **envp) /* qmsg */ { int c; int to_file; int errflg=0; int any_failed=0; char job_id[PBS_MAXCLTJOBID]; /* from the command line */ char job_id_out[PBS_MAXCLTJOBID]; char server_out[MAXSERVERNAME]; char rmt_server[MAXSERVERNAME]; #define MAX_MSG_STRING_LEN 256 char msg_string[MAX_MSG_STRING_LEN+1]; #define GETOPT_ARGS "EO" /*test for real deal or just version and exit*/ execution_mode(argc, argv); #ifdef WIN32 winsock_init(); #endif msg_string[0]='\0'; to_file = 0; while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) switch (c) { case 'E': to_file |= MSG_ERR; break; case 'O': to_file |= MSG_OUT; break; default : errflg++; } if (to_file == 0) to_file = MSG_ERR; /* default */ if (errflg || ((optind+1) >= argc)) { static char usage[]= "usage: qmsg [-O] [-E] msg_string job_identifier...\n"; static char usag2[]= " qmsg --version\n"; fprintf(stderr, "%s", usage); fprintf(stderr, "%s", usag2); exit(2); } strcpy(msg_string, argv[optind]); /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qmsg: unable to initialize security library.\n"); exit(2); } for (optind++; optind < argc; optind++) { int connect; int stat=0; int located = FALSE; strcpy(job_id, argv[optind]); if (get_server(job_id, job_id_out, server_out)) { fprintf(stderr, "qmsg: illegally formed job identifier: %s\n", job_id); any_failed = 1; continue; } cnt: connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qmsg: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); any_failed = pbs_errno; continue; } stat = pbs_msgjob(connect, job_id_out, to_file, msg_string, NULL); if (stat && (pbs_errno != PBSE_UNKJOBID)) { prt_job_err("qmsg", connect, job_id_out); any_failed = pbs_errno; } else if (stat && (pbs_errno == PBSE_UNKJOBID) && !located) { located = TRUE; if (locate_job(job_id_out, server_out, rmt_server)) { pbs_disconnect(connect); strcpy(server_out, rmt_server); goto cnt; } prt_job_err("qmsg", connect, job_id_out); any_failed = pbs_errno; } pbs_disconnect(connect); } /*cleanup security library initializations before exiting*/ CS_close_app(); exit(any_failed); }
/** * @brief * The entry point of pbsfs * * @return int * @retval 0 : success * @retval 1 : something is wrong! */ int main(int argc, char *argv[]) { char path_buf[256] = {0}; char sched_name[PBS_MAXSCHEDNAME + 1] = "default"; group_info *ginfo; group_info *ginfo2; int c; int flags = FS_PRINT; int flag1 = 0; double val; char *endp; char *testp; /* the real deal or output version and exit? */ PRINT_VERSION_AND_EXIT(argc, argv); set_msgdaemonname("pbsfs"); #ifdef WIN32 if (winsock_init()) { return 1; } #endif if (pbs_loadconf(0) <= 0) exit(1); while ((c = getopt(argc, argv, "sgptdceI:-:")) != -1) switch (c) { case 'g': flags = FS_GET; break; case 's': flags = FS_SET | FS_WRITE_FILE; break; case 'p': flags = FS_PRINT; break; case 't': flags = FS_PRINT_TREE; break; case 'd': flags = FS_DECAY | FS_WRITE_FILE; break; case 'c': flags = FS_COMP; break; case 'e': flags = FS_TRIM_TREE | FS_WRITE_FILE; break; case 'I': snprintf(sched_name, sizeof(sched_name), "%s", optarg); break; case '-': flag1 = 1; break; } if (flag1 == 1) { fprintf(stderr, "Usage: pbsfs --version\n"); exit(1); } if ((flags & (FS_PRINT | FS_PRINT_TREE)) && (argc - optind) != 0) { fprintf(stderr, "Usage: pbsfs -[ptdgcs] [-I sched_name]\n"); exit(1); } else if ((flags & FS_GET) && (argc - optind) != 1) { fprintf(stderr, "Usage: pbsfs [-I sched_name] -g <fairshare_entity>\n"); exit(1); } else if ((flags & FS_SET) && (argc - optind) != 2) { fprintf(stderr, "Usage: pbsfs [-I sched_name] -s <fairshare_entity> <usage>\n"); exit(1); } else if ((flags & FS_COMP) && (argc - optind) != 2) { fprintf(stderr, "Usage: pbsfs [-I sched_name] -c <entity1> <entity2>\n"); exit(1); } if (strcmp(sched_name, "default") != 0) { int pbs_sd; struct batch_status *bs; struct batch_status *cur_bs; pbs_sd = pbs_connect(NULL); if (pbs_sd < 0) { fprintf(stderr, "Can't connect to the server\n"); exit(1); } bs = pbs_statsched(pbs_sd, NULL, NULL); for (cur_bs = bs; cur_bs != NULL; cur_bs = cur_bs->next) { if (strcmp(cur_bs->name, sched_name) == 0) { struct attrl *cur_attrl; for (cur_attrl = cur_bs->attribs; cur_attrl != NULL; cur_attrl = cur_attrl->next) { if (strcmp(cur_attrl->name, ATTR_sched_priv) == 0) { strncpy(path_buf, cur_attrl->value, sizeof(path_buf)); path_buf[sizeof(path_buf) - 1] = '\0'; break; } } if (cur_attrl == NULL) { fprintf(stderr, "Scheduler %s does not have its sched_priv set\n", sched_name); exit(1); } break; } } if (cur_bs == NULL) { fprintf(stderr, "Scheduler %s does not exist\n", sched_name); exit(1); } pbs_disconnect(pbs_sd); } else snprintf(path_buf, sizeof(path_buf), "%s/sched_priv/", pbs_conf.pbs_home_path); if (chdir(path_buf) == -1) { perror("Unable to access fairshare data"); exit(1); } init_config(); parse_config(CONFIG_FILE); if ((conf.fairshare = preload_tree()) == NULL) { fprintf(stderr, "Error in preloading fairshare information\n"); return 1; } if (parse_group(RESGROUP_FILE, conf.fairshare->root) == 0) return 1; if (flags & FS_TRIM_TREE) read_usage(USAGE_FILE, FS_TRIM, conf.fairshare); else read_usage(USAGE_FILE, 0, conf.fairshare); calc_fair_share_perc(conf.fairshare->root->child, UNSPECIFIED); calc_usage_factor(conf.fairshare); if (flags & FS_PRINT_TREE) print_fairshare(conf.fairshare->root, 0); else if (flags & FS_PRINT ) { printf("Fairshare usage units are in: %s\n", conf.fairshare_res); print_fairshare(conf.fairshare->root, -1); } else if (flags & FS_DECAY) decay_fairshare_tree(conf.fairshare->root); else if (flags & (FS_GET | FS_SET | FS_COMP)) { ginfo = find_group_info(argv[optind], conf.fairshare->root); if (ginfo == NULL) { fprintf(stderr, "Fairshare Entity %s does not exist.\n", argv[optind]); return 1; } if (flags & FS_COMP) { ginfo2 = find_group_info(argv[optind + 1], conf.fairshare->root); if (ginfo2 == NULL) { fprintf(stderr, "Fairshare Entity %s does not exist.\n", argv[optind + 1]); return 1; } switch (compare_path(ginfo->gpath, ginfo2->gpath)) { case -1: printf("%s\n", ginfo->name); break; case 0: printf("%s == %s\n", ginfo->name, ginfo2->name); break; case 1: printf("%s\n", ginfo2->name); } } else if (flags & FS_GET) print_fairshare_entity(ginfo); else { testp = argv[optind + 1]; val = strtod(testp, &endp); if (*endp == '\0') ginfo->usage = val; } } if (flags & FS_WRITE_FILE) { FILE *fp; /* make backup of database file */ remove(USAGE_FILE ".bak"); if (rename(USAGE_FILE, USAGE_FILE ".bak") < 0) perror("Could not backup usage database."); write_usage(USAGE_FILE, conf.fairshare); if ((fp = fopen(USAGE_TOUCH, "w")) != NULL) fclose(fp); } return 0; }
int main(int argc, char **argv) #endif /* WIN32 */ { #ifdef WIN32 struct arg_param *p = (struct arg_param *)pv; int argc; char **argv; SERVICE_STATUS ss; #endif /* WIN32 */ char *name = NULL; struct tpp_config conf; int rpp_fd; char *pc; int numthreads; char lockfile[MAXPATHLEN + 1]; char path_log[MAXPATHLEN + 1]; char svr_home[MAXPATHLEN + 1]; char *log_file = 0; char *host; int port; char *routers = NULL; int c, i, rc; extern char *optarg; int are_primary; int num_var_env; #ifndef WIN32 struct sigaction act; struct sigaction oact; #endif #ifndef WIN32 /*the real deal or just pbs_version and exit*/ execution_mode(argc, argv); #endif /* As a security measure and to make sure all file descriptors */ /* are available to us, close all above stderr */ #ifdef WIN32 _fcloseall(); #else i = sysconf(_SC_OPEN_MAX); while (--i > 2) (void)close(i); /* close any file desc left open by parent */ #endif /* If we are not run with real and effective uid of 0, forget it */ #ifdef WIN32 argc = p->argc; argv = p->argv; ZeroMemory(&ss, sizeof(ss)); ss.dwCheckPoint = 0; ss.dwServiceType = SERVICE_WIN32_OWN_PROCESS; ss.dwCurrentState = g_dwCurrentState; ss.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN; ss.dwWaitHint = 6000; if (g_ssHandle != 0) SetServiceStatus(g_ssHandle, &ss); if (!isAdminPrivilege(getlogin())) { fprintf(stderr, "%s: Must be run by root\n", argv[0]); return (2); } #else if ((getuid() != 0) || (geteuid() != 0)) { fprintf(stderr, "%s: Must be run by root\n", argv[0]); return (2); } #endif /* WIN32 */ /* set standard umask */ #ifndef WIN32 umask(022); #endif /* load the pbs conf file */ if (pbs_loadconf(0) == 0) { fprintf(stderr, "%s: Configuration error\n", argv[0]); return (1); } umask(022); #ifdef WIN32 save_env(); #endif /* The following is code to reduce security risks */ /* start out with standard umask, system resource limit infinite */ if ((num_var_env = setup_env(pbs_conf.pbs_environment)) == -1) { #ifdef WIN32 g_dwCurrentState = SERVICE_STOPPED; ss.dwCurrentState = g_dwCurrentState; ss.dwWin32ExitCode = ERROR_INVALID_ENVIRONMENT; if (g_ssHandle != 0) SetServiceStatus(g_ssHandle, &ss); return (1); #else exit(1); #endif /* WIN32 */ } #ifndef WIN32 i = getgid(); (void)setgroups(1, (gid_t *)&i); /* secure suppl. groups */ #endif log_event_mask = &pbs_conf.pbs_comm_log_events; tpp_set_logmask(*log_event_mask); #ifdef WIN32 winsock_init(); #endif routers = pbs_conf.pbs_comm_routers; numthreads = pbs_conf.pbs_comm_threads; server_host[0] = '\0'; if (pbs_conf.pbs_comm_name) { name = pbs_conf.pbs_comm_name; host = tpp_parse_hostname(name, &port); if (host) snprintf(server_host, sizeof(server_host), "%s", host); free(host); host = NULL; } else if (pbs_conf.pbs_leaf_name) { char *endp; snprintf(server_host, sizeof(server_host), "%s", pbs_conf.pbs_leaf_name); endp = strchr(server_host, ','); /* find the first name */ if (endp) *endp = '\0'; endp = strchr(server_host, ':'); /* cut out the port */ if (endp) *endp = '\0'; name = server_host; } else { if (gethostname(server_host, (sizeof(server_host) - 1)) == -1) { #ifndef WIN32 sprintf(log_buffer, "Could not determine my hostname, errno=%d", errno); #else sprintf(log_buffer, "Could not determine my hostname, errno=%d", WSAGetLastError()); #endif fprintf(stderr, "%s\n", log_buffer); return (1); } if ((get_fullhostname(server_host, server_host, (sizeof(server_host) - 1)) == -1)) { sprintf(log_buffer, "Could not determine my hostname"); fprintf(stderr, "%s\n", log_buffer); return (1); } name = server_host; } if (server_host[0] == '\0') { sprintf(log_buffer, "Could not determine server host"); fprintf(stderr, "%s\n", log_buffer); return (1); } while ((c = getopt(argc, argv, "r:t:e:N")) != -1) { switch (c) { case 'e': *log_event_mask = strtol(optarg, NULL, 0); break; case 'r': routers = optarg; break; case 't': numthreads = atol(optarg); if (numthreads == -1) { usage(argv[0]); return (1); } break; case 'N': stalone = 1; break; default: usage(argv[0]); return (1); } } (void)strcpy(daemonname, "Comm@"); (void)strcat(daemonname, name); if ((pc = strchr(daemonname, (int)'.')) != NULL) *pc = '\0'; if(set_msgdaemonname(daemonname)) { fprintf(stderr, "Out of memory\n"); return 1; } (void) snprintf(path_log, sizeof(path_log), "%s/%s", pbs_conf.pbs_home_path, PBS_COMM_LOGDIR); #ifdef WIN32 /* * let SCM wait 10 seconds for log_open() to complete * as it does network interface query which can take time */ ss.dwCheckPoint++; ss.dwWaitHint = 60000; if (g_ssHandle != 0) SetServiceStatus(g_ssHandle, &ss); #endif (void) log_open(log_file, path_log); /* set pbs_comm's process limits */ set_limits(); /* set_limits can call log_record, so call only after opening log file */ /* set tcp function pointers */ set_tpp_funcs(log_tppmsg); (void) snprintf(svr_home, sizeof(svr_home), "%s/%s", pbs_conf.pbs_home_path, PBS_SVR_PRIVATE); if (chdir(svr_home) != 0) { (void) sprintf(log_buffer, msg_init_chdir, svr_home); log_err(-1, __func__, log_buffer); return (1); } (void) sprintf(lockfile, "%s/%s/comm.lock", pbs_conf.pbs_home_path, PBS_SVR_PRIVATE); if ((are_primary = are_we_primary()) == FAILOVER_SECONDARY) { strcat(lockfile, ".secondary"); } else if (are_primary == FAILOVER_CONFIG_ERROR) { sprintf(log_buffer, "Failover configuration error"); log_err(-1, __func__, log_buffer); #ifdef WIN32 g_dwCurrentState = SERVICE_STOPPED; ss.dwCurrentState = g_dwCurrentState; ss.dwWin32ExitCode = ERROR_SERVICE_NOT_ACTIVE; if (g_ssHandle != 0) SetServiceStatus(g_ssHandle, &ss); #endif return (3); } if ((lockfds = open(lockfile, O_CREAT | O_WRONLY, 0600)) < 0) { (void) sprintf(log_buffer, "pbs_comm: unable to open lock file"); log_err(errno, __func__, log_buffer); return (1); } if ((host = tpp_parse_hostname(name, &port)) == NULL) { sprintf(log_buffer, "Out of memory parsing leaf name"); log_err(errno, __func__, log_buffer); return (1); } rc = 0; if (pbs_conf.auth_method == AUTH_RESV_PORT) { rc = set_tpp_config(&pbs_conf, &conf, host, port, routers, pbs_conf.pbs_use_compression, TPP_AUTH_RESV_PORT, NULL, NULL); } else { /* for all non-resv-port based authentication use a callback from TPP */ rc = set_tpp_config(&pbs_conf, &conf, host, port, routers, pbs_conf.pbs_use_compression, TPP_AUTH_EXTERNAL, get_ext_auth_data, validate_ext_auth_data); } if (rc == -1) { (void) sprintf(log_buffer, "Error setting TPP config"); log_err(-1, __func__, log_buffer); return (1); } free(host); i = 0; if (conf.routers) { while (conf.routers[i]) { sprintf(log_buffer, "Router[%d]:%s", i, conf.routers[i]); fprintf(stdout, "%s\n", log_buffer); log_event(PBSEVENT_SYSTEM | PBSEVENT_FORCE, PBS_EVENTCLASS_SERVER, LOG_INFO, msg_daemonname, log_buffer); i++; } } #ifndef DEBUG #ifndef WIN32 if (stalone != 1) go_to_background(); #endif #endif #ifdef WIN32 ss.dwCheckPoint = 0; g_dwCurrentState = SERVICE_RUNNING; ss.dwCurrentState = g_dwCurrentState; if (g_ssHandle != 0) SetServiceStatus(g_ssHandle, &ss); #endif if (already_forked == 0) lock_out(lockfds, F_WRLCK); /* go_to_backgroud call creates a forked process, * thus print/log pid only after go_to_background() * has been called */ sprintf(log_buffer, "%s ready (pid=%d), Proxy Name:%s, Threads:%d", argv[0], getpid(), conf.node_name, numthreads); fprintf(stdout, "%s\n", log_buffer); log_event(PBSEVENT_SYSTEM | PBSEVENT_FORCE, PBS_EVENTCLASS_SERVER, LOG_INFO, msg_daemonname, log_buffer); #ifndef DEBUG pbs_close_stdfiles(); #endif #ifdef WIN32 signal(SIGINT, stop_me); signal(SIGTERM, stop_me); #else sigemptyset(&act.sa_mask); act.sa_flags = 0; act.sa_handler = hup_me; if (sigaction(SIGHUP, &act, &oact) != 0) { log_err(errno, __func__, "sigaction for HUP"); return (2); } act.sa_handler = stop_me; if (sigaction(SIGINT, &act, &oact) != 0) { log_err(errno, __func__, "sigaction for INT"); return (2); } if (sigaction(SIGTERM, &act, &oact) != 0) { log_err(errno, __func__, "sigactin for TERM"); return (2); } if (sigaction(SIGQUIT, &act, &oact) != 0) { log_err(errno, __func__, "sigactin for QUIT"); return (2); } #ifdef SIGSHUTDN if (sigaction(SIGSHUTDN, &act, &oact) != 0) { log_err(errno, __func__, "sigactin for SHUTDN"); return (2); } #endif /* SIGSHUTDN */ act.sa_handler = SIG_IGN; if (sigaction(SIGPIPE, &act, &oact) != 0) { log_err(errno, __func__, "sigaction for PIPE"); return (2); } if (sigaction(SIGUSR1, &act, &oact) != 0) { log_err(errno, __func__, "sigaction for USR1"); return (2); } if (sigaction(SIGUSR2, &act, &oact) != 0) { log_err(errno, __func__, "sigaction for USR2"); return (2); } #endif /* WIN32 */ conf.node_type = TPP_ROUTER_NODE; conf.numthreads = numthreads; if ((rpp_fd = tpp_init_router(&conf)) == -1) { log_err(-1, __func__, "tpp init failed\n"); return 1; } /* Protect from being killed by kernel */ daemon_protect(0, PBS_DAEMON_PROTECT_ON); /* go in a while loop */ while (get_out == 0) { if (hupped == 1) { struct pbs_config pbs_conf_bak; int new_logevent; hupped = 0; /* reset back */ memcpy(&pbs_conf_bak, &pbs_conf, sizeof(struct pbs_config)); if (pbs_loadconf(1) == 0) { log_tppmsg(LOG_CRIT, NULL, "Configuration error, ignoring"); memcpy(&pbs_conf, &pbs_conf_bak, sizeof(struct pbs_config)); } else { /* restore old pbs.conf */ new_logevent = pbs_conf.pbs_comm_log_events; memcpy(&pbs_conf, &pbs_conf_bak, sizeof(struct pbs_config)); pbs_conf.pbs_comm_log_events = new_logevent; log_tppmsg(LOG_INFO, NULL, "Processed SIGHUP"); log_event_mask = &pbs_conf.pbs_comm_log_events; tpp_set_logmask(*log_event_mask); } } sleep(3); } tpp_router_shutdown(); log_event(PBSEVENT_SYSTEM | PBSEVENT_FORCE, PBS_EVENTCLASS_SERVER, LOG_NOTICE, msg_daemonname, "Exiting"); log_close(1); lock_out(lockfds, F_UNLCK); /* unlock */ (void)close(lockfds); (void)unlink(lockfile); return 0; }
int main (int argc, char **argv) { int i, c; int pid_flags = 0; char *local_port = NULL; char *local_addr = NULL; char *password = NULL; char *timeout = NULL; char *method = NULL; char *pid_path = NULL; char *conf_path = NULL; char *iface = NULL; int remote_num = 0; char *remote_host[MAX_REMOTE_NUM]; char *remote_port = NULL; opterr = 0; while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:i:c:b:uv")) != -1) { switch (c) { case 's': remote_host[remote_num++] = optarg; break; case 'p': remote_port = optarg; break; case 'l': local_port = optarg; break; case 'k': password = optarg; break; case 'f': pid_flags = 1; pid_path = optarg; break; case 't': timeout = optarg; break; case 'm': method = optarg; break; case 'c': conf_path = optarg; break; case 'i': iface = optarg; break; case 'b': local_addr = optarg; break; case 'u': udprelay = 1; break; case 'v': verbose = 1; break; } } if (opterr) { usage(); exit(EXIT_FAILURE); } if (conf_path != NULL) { jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; for (i = 0; i < remote_num; i++) { remote_host[i] = conf->remote_host[i]; } } if (remote_port == NULL) remote_port = conf->remote_port; if (local_port == NULL) local_port = conf->local_port; if (password == NULL) password = conf->password; if (method == NULL) method = conf->method; if (timeout == NULL) timeout = conf->timeout; } if (remote_num == 0 || remote_port == NULL || local_port == NULL || password == NULL) { usage(); exit(EXIT_FAILURE); } if (timeout == NULL) timeout = "10"; if (local_addr == NULL) local_addr = "0.0.0.0"; if (pid_flags) { demonize(pid_path); } #ifdef __MINGW32__ winsock_init(); #else // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif // Setup keys LOGD("initialize ciphers... %s", method); int m = enc_init(password, method); // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port); if (listenfd < 0) { FATAL("bind() error.."); } if (listen(listenfd, SOMAXCONN) == -1) { FATAL("listen() error."); } setnonblocking(listenfd); LOGD("server listening at port %s.", local_port); // Setup proxy context struct listen_ctx listen_ctx; listen_ctx.remote_num = remote_num; listen_ctx.remote_host = malloc(sizeof(char *) * remote_num); while (remote_num > 0) { int index = --remote_num; listen_ctx.remote_host[index] = remote_host[index]; } listen_ctx.remote_port = remote_port; listen_ctx.timeout = atoi(timeout); listen_ctx.fd = listenfd; listen_ctx.iface = iface; listen_ctx.method = m; struct ev_loop *loop = ev_default_loop(0); if (!loop) { FATAL("ev_loop error."); } ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ); ev_io_start (loop, &listen_ctx.io); // Setup UDP if (udprelay) { LOGD("udprelay enabled."); udprelay_init(local_addr, local_port, remote_host[0], remote_port, m, iface); } ev_run (loop, 0); #ifdef __MINGW32__ winsock_cleanup(); #endif return 0; }
int main(int argc, char **argv, char **envp) { char job_id1[PBS_MAXCLTJOBID+1]; /* from the command line */ char job_id2[PBS_MAXCLTJOBID+1]; /* from the command line */ char job_id1_out[PBS_MAXCLTJOBID+1]; char job_id2_out[PBS_MAXCLTJOBID+1]; char *pn; int port1 = 0; int port2 = 0; char server_out1[MAXSERVERNAME+1]; char server_out2[MAXSERVERNAME+1]; char svrtmp[MAXSERVERNAME+1]; int connect; int stat=0; int rc = 0; extern char *PBS_get_server(); /*test for real deal or just version and exit*/ PRINT_VERSION_AND_EXIT(argc, argv); #ifdef WIN32 if (winsock_init()) { return 1; } #endif if (argc != 3) { static char usage[]="usage: qorder job_identifier job_identifier\n"; static char usag2[]=" qorder --version\n"; fprintf(stderr, "%s", usage); fprintf(stderr, "%s", usag2); exit(2); } strcpy(job_id1, argv[1]); strcpy(job_id2, argv[2]); svrtmp[0] = '\0'; if (get_server(job_id1, job_id1_out, svrtmp)) { fprintf(stderr, "qorder: illegally formed job identifier: %s\n", job_id1); exit(1); } if (*svrtmp == '\0') { if ((pn = pbs_default()) != NULL) { (void)strcpy(svrtmp, pn); } else { fprintf(stderr, "qorder: could not get default server: %s\n", job_id1); exit(1); } } if ((pn = strchr(svrtmp, (int)':')) != 0) { *pn = '\0'; port1 = atoi(pn+1); } if (get_fullhostname(svrtmp, server_out1, MAXSERVERNAME) != 0) { fprintf(stderr, "qorder: invalid server name: %s\n", job_id1); exit(1); } svrtmp[0] = '\0'; if (get_server(job_id2, job_id2_out, svrtmp)) { fprintf(stderr, "qorder: illegally formed job identifier: %s\n", job_id2); exit(1); } if (*svrtmp == '\0') { if ((pn = pbs_default()) != NULL) { (void)strcpy(svrtmp, pn); } else { fprintf(stderr, "qorder: could not get default server: %s\n", job_id1); exit(1); } } if ((pn = strchr(svrtmp, (int)':')) != 0) { *pn = '\0'; port2 = atoi(pn+1); } if (get_fullhostname(svrtmp, server_out2, MAXSERVERNAME) != 0) { fprintf(stderr, "qorder: invalid server name: %s\n", job_id2); exit(1); } if ((strcmp(server_out1, server_out2) != 0) || (port1 != port2)) { fprintf(stderr, "qorder: both jobs ids must specify the same server\n"); exit(1); } if (pn) *pn = ':'; /* restore : if it was present */ /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qorder: unable to initialize security library.\n"); exit(1); } connect = cnt2server(svrtmp); if (connect <= 0) { fprintf(stderr, "qorder: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); exit(1);; } stat = pbs_orderjob(connect, job_id1_out, job_id2_out, NULL); if (stat) { char job_id_both[PBS_MAXCLTJOBID + PBS_MAXCLTJOBID + 3]; strcpy(job_id_both, job_id1_out); strcat(job_id_both, " or "); strcat(job_id_both, job_id2_out); prt_job_err("qorder", connect, job_id_both); rc = pbs_errno; } pbs_disconnect(connect); /*cleanup security library initializations before exiting*/ CS_close_app(); exit(rc); }
int main (int argc, char **argv) { int i, c; int pid_flags = 0; char *user = NULL; char *local_port = NULL; char *local_addr = NULL; char *password = NULL; char *timeout = NULL; char *method = NULL; char *pid_path = NULL; char *conf_path = NULL; char *iface = NULL; int remote_num = 0; ss_addr_t remote_addr[MAX_REMOTE_NUM]; char *remote_port = NULL; int option_index = 0; static struct option long_options[] = { {"fast-open", no_argument, 0, 0 }, {0, 0, 0, 0 } }; opterr = 0; while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:uv", long_options, &option_index)) != -1) { switch (c) { case 0: if (option_index == 0) { #ifdef TCP_FASTOPEN fast_open = 1; LOGD("using tcp fast open"); #else LOGE("tcp fast open is not supported by this environment"); #endif } break; case 's': remote_addr[remote_num].host = optarg; remote_addr[remote_num++].port = NULL; break; case 'p': remote_port = optarg; break; case 'l': local_port = optarg; break; case 'k': password = optarg; break; case 'f': pid_flags = 1; pid_path = optarg; break; case 't': timeout = optarg; break; case 'm': method = optarg; break; case 'c': conf_path = optarg; break; case 'i': iface = optarg; break; case 'b': local_addr = optarg; break; case 'a': user = optarg; break; case 'u': udprelay = 1; break; case 'v': verbose = 1; break; } } if (opterr) { usage(); exit(EXIT_FAILURE); } if (conf_path != NULL) { jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; for (i = 0; i < remote_num; i++) { remote_addr[i] = conf->remote_addr[i]; } } if (remote_port == NULL) remote_port = conf->remote_port; if (local_addr == NULL) local_addr = conf->local_addr; if (local_port == NULL) local_port = conf->local_port; if (password == NULL) password = conf->password; if (method == NULL) method = conf->method; if (timeout == NULL) timeout = conf->timeout; if (fast_open == 0) fast_open = conf->fast_open; } if (remote_num == 0 || remote_port == NULL || local_port == NULL || password == NULL) { usage(); exit(EXIT_FAILURE); } if (timeout == NULL) timeout = "10"; if (local_addr == NULL) local_addr = "0.0.0.0"; if (pid_flags) { USE_SYSLOG(argv[0]); demonize(pid_path); } #ifdef __MINGW32__ winsock_init(); #else // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif // Setup keys LOGD("initialize ciphers... %s", method); int m = enc_init(password, method); // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port); if (listenfd < 0) { FATAL("bind() error.."); } if (listen(listenfd, SOMAXCONN) == -1) { FATAL("listen() error."); } setnonblocking(listenfd); LOGD("server listening at port %s.", local_port); // Setup proxy context struct listen_ctx listen_ctx; listen_ctx.remote_num = remote_num; listen_ctx.remote_addr = malloc(sizeof(ss_addr_t) * remote_num); while (remote_num > 0) { int index = --remote_num; if (remote_addr[index].port == NULL) remote_addr[index].port = remote_port; listen_ctx.remote_addr[index] = remote_addr[index]; } listen_ctx.timeout = atoi(timeout); listen_ctx.fd = listenfd; listen_ctx.iface = iface; listen_ctx.method = m; struct ev_loop *loop = ev_default_loop(0); if (!loop) { FATAL("ev_loop error."); } ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ); ev_io_start (loop, &listen_ctx.io); // Setup UDP if (udprelay) { LOGD("udprelay enabled."); udprelay_init(local_addr, local_port, remote_addr[0].host, remote_addr[0].port, m, listen_ctx.timeout, iface); } // setuid if (user != NULL) run_as(user); ev_run (loop, 0); #ifdef __MINGW32__ winsock_cleanup(); #endif return 0; }
int start_ss_local_server(profile_t profile) { srand(time(NULL)); char *remote_host = profile.remote_host; char *local_addr = profile.local_addr; char *method = profile.method; char *password = profile.password; char *log = profile.log; int remote_port = profile.remote_port; int local_port = profile.local_port; int timeout = profile.timeout; udprelay = profile.udp_relay; fast_open = profile.fast_open; verbose = profile.verbose; char local_port_str[16]; char remote_port_str[16]; sprintf(local_port_str, "%d", local_port); sprintf(remote_port_str, "%d", remote_port); USE_LOGFILE(log); if (profile.acl != NULL) { acl = !init_acl(profile.acl); } if (local_addr == NULL) { local_addr = "127.0.0.1"; } #ifdef __MINGW32__ winsock_init(); #else // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif struct ev_signal sigint_watcher; struct ev_signal sigterm_watcher; ev_signal_init(&sigint_watcher, signal_cb, SIGINT); ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); ev_signal_start(EV_DEFAULT, &sigint_watcher); ev_signal_start(EV_DEFAULT, &sigterm_watcher); // Setup keys LOGI("initialize ciphers... %s", method); int m = enc_init(password, method); // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port_str); if (listenfd < 0) { ERROR("bind()"); return -1; } if (listen(listenfd, SOMAXCONN) == -1) { ERROR("listen()"); return -1; } setnonblocking(listenfd); LOGI("listening at %s:%s", local_addr, local_port_str); // Setup proxy context struct listen_ctx listen_ctx; listen_ctx.remote_num = 1; listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *)); struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage)); memset(storage, 0, sizeof(struct sockaddr_storage)); if (get_sockaddr(remote_host, remote_port_str, storage) == -1) { return -1; } listen_ctx.remote_addr[0] = (struct sockaddr *)storage; listen_ctx.timeout = timeout; listen_ctx.fd = listenfd; listen_ctx.method = m; listen_ctx.iface = NULL; struct ev_loop *loop = EV_DEFAULT; ev_io_init(&listen_ctx.io, accept_cb, listenfd, EV_READ); ev_io_start(loop, &listen_ctx.io); // Setup UDP if (udprelay) { LOGI("udprelay enabled"); init_udprelay(local_addr, local_port_str, listen_ctx.remote_addr[0], get_sockaddr_len(listen_ctx.remote_addr[0]), m, listen_ctx.timeout, NULL); } // Init connections cork_dllist_init(&connections); // Enter the loop ev_run(loop, 0); if (verbose) { LOGI("closed gracefully"); } // Clean up free_connections(loop); if (udprelay) { free_udprelay(); } ev_io_stop(loop, &listen_ctx.io); free(listen_ctx.remote_addr); close(listen_ctx.fd); #ifdef __MINGW32__ winsock_cleanup(); #endif ev_signal_stop(EV_DEFAULT, &sigint_watcher); ev_signal_stop(EV_DEFAULT, &sigterm_watcher); // cannot reach here return 0; }
int main(int argc, char **argv) { int i, c; int pid_flags = 0; char *user = NULL; char *local_port = NULL; char *local_addr = NULL; char *password = NULL; char *timeout = NULL; char *method = NULL; char *pid_path = NULL; char *conf_path = NULL; char *iface = NULL; srand(time(NULL)); int remote_num = 0; ss_addr_t remote_addr[MAX_REMOTE_NUM]; char *remote_port = NULL; int option_index = 0; static struct option long_options[] = { { "fast-open", no_argument, 0, 0 }, { "acl", required_argument, 0, 0 }, { 0, 0, 0, 0 } }; opterr = 0; USE_TTY(); #ifdef ANDROID while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:uvVA", long_options, &option_index)) != -1) { #else while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:a:uvA", long_options, &option_index)) != -1) { #endif switch (c) { case 0: if (option_index == 0) { fast_open = 1; } else if (option_index == 1) { LOGI("initialize acl..."); acl = !init_acl(optarg); } break; case 's': if (remote_num < MAX_REMOTE_NUM) { remote_addr[remote_num].host = optarg; remote_addr[remote_num++].port = NULL; } break; case 'p': remote_port = optarg; break; case 'l': local_port = optarg; break; case 'k': password = optarg; break; case 'f': pid_flags = 1; pid_path = optarg; break; case 't': timeout = optarg; break; case 'm': method = optarg; break; case 'c': conf_path = optarg; break; case 'i': iface = optarg; break; case 'b': local_addr = optarg; break; case 'a': user = optarg; break; case 'u': mode = TCP_AND_UDP; break; case 'v': verbose = 1; break; case 'A': auth = 1; break; #ifdef ANDROID case 'V': vpn = 1; break; #endif } } if (opterr) { usage(); exit(EXIT_FAILURE); } if (argc == 1) { if (conf_path == NULL) { conf_path = DEFAULT_CONF_PATH; } } if (conf_path != NULL) { jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; for (i = 0; i < remote_num; i++) { remote_addr[i] = conf->remote_addr[i]; } } if (remote_port == NULL) { remote_port = conf->remote_port; } if (local_addr == NULL) { local_addr = conf->local_addr; } if (local_port == NULL) { local_port = conf->local_port; } if (password == NULL) { password = conf->password; } if (method == NULL) { method = conf->method; } if (timeout == NULL) { timeout = conf->timeout; } if (fast_open == 0) { fast_open = conf->fast_open; } #ifdef HAVE_SETRLIMIT if (nofile == 0) { nofile = conf->nofile; } /* * no need to check the return value here since we will show * the user an error message if setrlimit(2) fails */ if (nofile) { if (verbose) { LOGI("setting NOFILE to %d", nofile); } set_nofile(nofile); } #endif } if (remote_num == 0 || remote_port == NULL || local_port == NULL || password == NULL) { usage(); exit(EXIT_FAILURE); } if (timeout == NULL) { timeout = "60"; } if (local_addr == NULL) { local_addr = "127.0.0.1"; } if (pid_flags) { USE_SYSLOG(argv[0]); daemonize(pid_path); } if (fast_open == 1) { #ifdef TCP_FASTOPEN LOGI("using tcp fast open"); #else LOGE("tcp fast open is not supported by this environment"); #endif } #ifdef __MINGW32__ winsock_init(); #else // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif struct ev_signal sigint_watcher; struct ev_signal sigterm_watcher; ev_signal_init(&sigint_watcher, signal_cb, SIGINT); ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); ev_signal_start(EV_DEFAULT, &sigint_watcher); ev_signal_start(EV_DEFAULT, &sigterm_watcher); // Setup keys LOGI("initialize ciphers... %s", method); int m = enc_init(password, method); // Setup proxy context struct listen_ctx listen_ctx; listen_ctx.remote_num = remote_num; listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *) * remote_num); for (i = 0; i < remote_num; i++) { char *host = remote_addr[i].host; char *port = remote_addr[i].port == NULL ? remote_port : remote_addr[i].port; struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage)); memset(storage, 0, sizeof(struct sockaddr_storage)); if (get_sockaddr(host, port, storage, 1) == -1) { FATAL("failed to resolve the provided hostname"); } listen_ctx.remote_addr[i] = (struct sockaddr *)storage; } listen_ctx.timeout = atoi(timeout); listen_ctx.iface = iface; listen_ctx.method = m; struct ev_loop *loop = EV_DEFAULT; // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port); if (listenfd < 0) { FATAL("bind() error"); } if (listen(listenfd, SOMAXCONN) == -1) { FATAL("listen() error"); } setnonblocking(listenfd); listen_ctx.fd = listenfd; ev_io_init(&listen_ctx.io, accept_cb, listenfd, EV_READ); ev_io_start(loop, &listen_ctx.io); if (auth) { LOGI("onetime authentication enabled"); } // Setup UDP if (mode != TCP_ONLY) { LOGI("udprelay enabled"); init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0], get_sockaddr_len(listen_ctx.remote_addr[0]), m, listen_ctx.timeout, iface); } LOGI("listening at %s:%s", local_addr, local_port); // setuid if (user != NULL) { run_as(user); } // Init connections cork_dllist_init(&connections); // Enter the loop ev_run(loop, 0); if (verbose) { LOGI("closed gracefully"); } // Clean up ev_io_stop(loop, &listen_ctx.io); free_connections(loop); if (mode != TCP_ONLY) { free_udprelay(); } for (i = 0; i < remote_num; i++) { free(listen_ctx.remote_addr[i]); } free(listen_ctx.remote_addr); #ifdef __MINGW32__ winsock_cleanup(); #endif ev_signal_stop(EV_DEFAULT, &sigint_watcher); ev_signal_stop(EV_DEFAULT, &sigterm_watcher); return 0; } #else int start_ss_local_server(profile_t profile) { srand(time(NULL)); char *remote_host = profile.remote_host; char *local_addr = profile.local_addr; char *method = profile.method; char *password = profile.password; char *log = profile.log; int remote_port = profile.remote_port; int local_port = profile.local_port; int timeout = profile.timeout; mode = profile.mode; fast_open = profile.fast_open; verbose = profile.verbose; char local_port_str[16]; char remote_port_str[16]; sprintf(local_port_str, "%d", local_port); sprintf(remote_port_str, "%d", remote_port); USE_LOGFILE(log); if (profile.acl != NULL) { acl = !init_acl(profile.acl); } if (local_addr == NULL) { local_addr = "127.0.0.1"; } #ifdef __MINGW32__ winsock_init(); #else // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif struct ev_signal sigint_watcher; struct ev_signal sigterm_watcher; ev_signal_init(&sigint_watcher, signal_cb, SIGINT); ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); ev_signal_start(EV_DEFAULT, &sigint_watcher); ev_signal_start(EV_DEFAULT, &sigterm_watcher); // Setup keys LOGI("initialize ciphers... %s", method); int m = enc_init(password, method); struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage)); memset(storage, 0, sizeof(struct sockaddr_storage)); if (get_sockaddr(remote_host, remote_port_str, storage, 1) == -1) { return -1; } // Setup proxy context struct ev_loop *loop = EV_DEFAULT; struct listen_ctx listen_ctx; listen_ctx.remote_num = 1; listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *)); listen_ctx.remote_addr[0] = (struct sockaddr *)storage; listen_ctx.timeout = timeout; listen_ctx.method = m; listen_ctx.iface = NULL; // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port_str); if (listenfd < 0) { ERROR("bind()"); return -1; } if (listen(listenfd, SOMAXCONN) == -1) { ERROR("listen()"); return -1; } setnonblocking(listenfd); listen_ctx.fd = listenfd; ev_io_init(&listen_ctx.io, accept_cb, listenfd, EV_READ); ev_io_start(loop, &listen_ctx.io); // Setup UDP if (mode != TCP_ONLY) { LOGI("udprelay enabled"); struct sockaddr *addr = (struct sockaddr *)storage; init_udprelay(local_addr, local_port_str, addr, get_sockaddr_len(addr), m, timeout, NULL); } LOGI("listening at %s:%s", local_addr, local_port_str); // Init connections cork_dllist_init(&connections); // Enter the loop ev_run(loop, 0); if (verbose) { LOGI("closed gracefully"); } // Clean up if (mode != TCP_ONLY) { free_udprelay(); } ev_io_stop(loop, &listen_ctx.io); free_connections(loop); close(listen_ctx.fd); free(listen_ctx.remote_addr); #ifdef __MINGW32__ winsock_cleanup(); #endif ev_signal_stop(EV_DEFAULT, &sigint_watcher); ev_signal_stop(EV_DEFAULT, &sigterm_watcher); // cannot reach here return 0; }
BOOL WINAPI DllMain/*tirpc_main*/(HINSTANCE hinstDLL, // DLL module handle DWORD fdwReason, // reason called LPVOID lpvReserved) // reserved { LPVOID lpvData; BOOL fIgnore; // if (init++) // return TRUE; // Deal with Thread Local Storage initialization!! switch (fdwReason) { // The DLL is loading due to process // initialization or a call to LoadLibrary. case DLL_PROCESS_ATTACH: // Initialize socket library if (winsock_init() == FALSE) return FALSE; // Initialize CriticalSections tirpc_criticalsection_init(); // Allocate a TLS index. if ((dwTlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES) return FALSE; // No break: Initialize the index for first thread. // The attached process creates a new thread. case DLL_THREAD_ATTACH: // Initialize the TLS index for this thread lpvData = (LPVOID) LocalAlloc(LPTR, 256); if (lpvData != NULL) fIgnore = TlsSetValue(dwTlsIndex, lpvData); break; // The thread of the attached process terminates. case DLL_THREAD_DETACH: // Release the allocated memory for this thread. lpvData = TlsGetValue(dwTlsIndex); if (lpvData != NULL) LocalFree((HLOCAL) lpvData); break; // DLL unload due to process termination or FreeLibrary. case DLL_PROCESS_DETACH: // Release the allocated memory for this thread. lpvData = TlsGetValue(dwTlsIndex); if (lpvData != NULL) LocalFree((HLOCAL) lpvData); // Release the TLS index. TlsFree(dwTlsIndex); // Clean up winsock stuff winsock_fini(); break; default: break; } return TRUE; }
/** * @brief * main - the entry point in pbsTclInit.c * * @param[in] argc - argument count. * @param[in] argv - argument variables. * * @return int * @retval 0 : success */ int main(int argc, char *argv[]) { char tcl_libpath[MAXPATHLEN+13]; /* 13 for "TCL_LIBRARY=" + \0 */ int rc; /*the real deal or just pbs_version and exit?*/ execution_mode(argc, argv); if(set_msgdaemonname("pbs_tclsh")) { fprintf(stderr, "Out of memory\n"); return 1; } set_logfile(stderr); #ifdef WIN32 winsock_init(); Tcl_FindExecutable(argv[0]); #endif /* load the pbs conf file */ if (pbs_loadconf(0) == 0) { fprintf(stderr, "%s: Configuration error\n", argv[0]); return (1); } if (!getenv("TCL_LIBRARY")) { if (pbs_conf.pbs_exec_path) { sprintf((char *)tcl_libpath, #ifdef WIN32 "TCL_LIBRARY=%s/lib/tcl%s", #else "TCL_LIBRARY=%s/tcltk/lib/tcl%s", #endif pbs_conf.pbs_exec_path, TCL_VERSION); putenv(tcl_libpath); } } if (pbs_conf.pbs_use_tcp == 1) { char *nodename; struct tpp_config tpp_conf; char my_hostname[PBS_MAXHOSTNAME+1]; fd_set selset; struct timeval tv; if (pbs_conf.pbs_leaf_name) nodename = pbs_conf.pbs_leaf_name; else { if (gethostname(my_hostname, (sizeof(my_hostname) - 1)) < 0) { fprintf(stderr, "Failed to get hostname\n"); return -1; } nodename = my_hostname; } /* We don't want to show logs related to connecting pbs_comm on console * this set this flag to ignore it */ log_mask = SHOW_NONE; /* set tpp function pointers */ set_tpp_funcs(log_tppmsg); /* call tpp_init */ rc = 0; #ifndef WIN32 if (pbs_conf.auth_method == AUTH_MUNGE) rc = set_tpp_config(&pbs_conf, &tpp_conf, nodename, -1, pbs_conf.pbs_leaf_routers, pbs_conf.pbs_use_compression, TPP_AUTH_EXTERNAL, get_ext_auth_data, validate_ext_auth_data); else #endif rc = set_tpp_config(&pbs_conf, &tpp_conf, nodename, -1, pbs_conf.pbs_leaf_routers, pbs_conf.pbs_use_compression, TPP_AUTH_RESV_PORT, NULL, NULL); if (rc == -1) { fprintf(stderr, "Error setting TPP config\n"); return -1; } if ((rpp_fd = tpp_init(&tpp_conf)) == -1) { fprintf(stderr, "rpp_init failed\n"); return -1; } /* * Wait for net to get restored, ie, app to connect to routers */ FD_ZERO(&selset); FD_SET(rpp_fd, &selset); tv.tv_sec = 5; tv.tv_usec = 0; select(FD_SETSIZE, &selset, (fd_set *) 0, (fd_set *) 0, &tv); rpp_poll(); /* to clear off the read notification */ /* Once the connection is established we can unset log_mask */ log_mask &= ~SHOW_NONE; } else { /* set rpp function pointers */ set_rpp_funcs(log_rppfail); } Tcl_Main(argc, argv, pbsTcl_Init); return 0; }
/** * @brief * The main function in C - entry point * * @param[in] argc - argument count * @param[in] argv - pointer to argument array * @param[in] envp - pointer to environment values * * @return int * @retval 0 - success * @retval !0 - error */ int main(int argc, char *argv[], char *envp[]) { int errflg; /* command line option error */ int connect; /* return from pbs_connect */ char *errmsg; /* return from pbs_geterrmsg */ char destbuf[256]; /* buffer for option server */ struct attrl *attrib; /* the attrib list */ char *new_resvname; /* the name returned from pbs_submit_resv */ struct ecl_attribute_errors *err_list; char *interactive = NULL; /*test for real deal or just version and exit*/ execution_mode(argc, argv); #ifdef WIN32 winsock_init(); #endif destbuf[0] = '\0'; errflg = process_opts(argc, argv, &attrib, destbuf); /* get cmdline options */ if (errflg || ((optind+1) < argc) || argc == 1) { print_usage(); exit(2); } /* Get any required environment variables needing to be sent. */ if (! set_resv_env(envp)) { fprintf(stderr, "pbs_rsub: can't send environment with the reservation\n"); exit(3); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "pbs_rsub: unable to initialize security library.\n"); exit(1); } /* Connect to the server */ connect = cnt2server(destbuf); if (connect <= 0) { fprintf(stderr, "pbs_rsub: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); CS_close_app(); exit(pbs_errno); } if (qmoveflg == TRUE) { qmoveflg = FALSE; interactive = get_attr(attrib, ATTR_inter, NULL); if (interactive == NULL) { set_attr(&attrib, ATTR_inter, DEFAULT_INTERACTIVE); } else { if (atoi(interactive) > -1) { fprintf(stderr, "pbs_rsub: -I <timeout> value must be negative when used with -Wqmove option.\n"); CS_close_app(); exit(2); } } errflg = cnvrt_proc_attrib(connect, &attrib, destbuf); if (errflg) { fprintf(stderr, "pbs_rsub: can't make a reservation with the qmove option\n"); CS_close_app(); exit(2); } } pbs_errno = 0; new_resvname = pbs_submit_resv(connect, (struct attropl *)attrib, NULL); if (new_resvname == NULL) { if ((err_list = pbs_get_attributes_in_error(connect))) handle_attribute_errors(err_list); errmsg = pbs_geterrmsg(connect); if (errmsg != NULL) { fprintf(stderr, "pbs_rsub: %s\n", errmsg); } else fprintf(stderr, "pbs_rsub: Error (%d) submitting reservation\n", pbs_errno); CS_close_app(); exit(pbs_errno); } else { printf("%s\n", new_resvname); free(new_resvname); } /* Disconnet from the server. */ pbs_disconnect(connect); CS_close_app(); exit(0); }
int main(int argc, char **argv) { int i, c; int pid_flags = 0; char *user = NULL; char *local_port = NULL; char *local_addr = NULL; char *password = NULL; char *timeout = NULL; char *method = NULL; char *pid_path = NULL; char *conf_path = NULL; char *iface = NULL; int remote_num = 0; ss_addr_t remote_addr[MAX_REMOTE_NUM]; char *remote_port = NULL; ss_addr_t tunnel_addr = { .host = NULL, .port = NULL }; char *tunnel_addr_str = NULL; opterr = 0; USE_TTY(); #ifdef ANDROID while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:uUvV")) != -1) { #else while ((c = getopt(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:uUv")) != -1) { #endif switch (c) { case 's': if (remote_num < MAX_REMOTE_NUM) { remote_addr[remote_num].host = optarg; remote_addr[remote_num++].port = NULL; } break; case 'p': remote_port = optarg; break; case 'l': local_port = optarg; break; case 'k': password = optarg; break; case 'f': pid_flags = 1; pid_path = optarg; break; case 't': timeout = optarg; break; case 'm': method = optarg; break; case 'c': conf_path = optarg; break; case 'i': iface = optarg; break; case 'b': local_addr = optarg; break; case 'u': mode = TCP_AND_UDP; break; case 'U': mode = UDP_ONLY; break; case 'L': tunnel_addr_str = optarg; break; case 'a': user = optarg; break; case 'v': verbose = 1; break; #ifdef ANDROID case 'V': vpn = 1; break; #endif } } if (opterr) { usage(); exit(EXIT_FAILURE); } if (argc == 1) { if (conf_path == NULL) { conf_path = DEFAULT_CONF_PATH; } } if (conf_path != NULL) { jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; for (i = 0; i < remote_num; i++) { remote_addr[i] = conf->remote_addr[i]; } } if (remote_port == NULL) { remote_port = conf->remote_port; } if (local_addr == NULL) { local_addr = conf->local_addr; } if (local_port == NULL) { local_port = conf->local_port; } if (password == NULL) { password = conf->password; } if (method == NULL) { method = conf->method; } if (timeout == NULL) { timeout = conf->timeout; } } if (remote_num == 0 || remote_port == NULL || tunnel_addr_str == NULL || local_port == NULL || password == NULL) { usage(); exit(EXIT_FAILURE); } if (timeout == NULL) { timeout = "60"; } if (local_addr == NULL) { local_addr = "127.0.0.1"; } if (pid_flags) { USE_SYSLOG(argv[0]); daemonize(pid_path); } // parse tunnel addr parse_addr(tunnel_addr_str, &tunnel_addr); if (tunnel_addr.port == NULL) { FATAL("tunnel port is not defined"); } #ifdef __MINGW32__ winsock_init(); #else // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif // Setup keys LOGI("initialize ciphers... %s", method); int m = enc_init(password, method); // Setup proxy context struct listen_ctx listen_ctx; listen_ctx.tunnel_addr = tunnel_addr; listen_ctx.remote_num = remote_num; listen_ctx.remote_addr = malloc(sizeof(struct sockaddr *) * remote_num); for (i = 0; i < remote_num; i++) { char *host = remote_addr[i].host; char *port = remote_addr[i].port == NULL ? remote_port : remote_addr[i].port; struct sockaddr_storage *storage = malloc(sizeof(struct sockaddr_storage)); memset(storage, 0, sizeof(struct sockaddr_storage)); if (get_sockaddr(host, port, storage, 1) == -1) { FATAL("failed to resolve the provided hostname"); } listen_ctx.remote_addr[i] = (struct sockaddr *)storage; } listen_ctx.timeout = atoi(timeout); listen_ctx.iface = iface; listen_ctx.method = m; struct ev_loop *loop = EV_DEFAULT; if (mode != UDP_ONLY) { // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port); if (listenfd < 0) { FATAL("bind() error:"); } if (listen(listenfd, SOMAXCONN) == -1) { FATAL("listen() error:"); } setnonblocking(listenfd); listen_ctx.fd = listenfd; ev_io_init(&listen_ctx.io, accept_cb, listenfd, EV_READ); ev_io_start(loop, &listen_ctx.io); } // Setup UDP if (mode != TCP_ONLY) { LOGI("UDP relay enabled"); init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0], get_sockaddr_len(listen_ctx.remote_addr[0]), tunnel_addr, m, listen_ctx.timeout, iface); } if (mode == UDP_ONLY) { LOGI("TCP relay disabled"); } LOGI("listening at %s:%s", local_addr, local_port); // setuid if (user != NULL) { run_as(user); } ev_run(loop, 0); #ifdef __MINGW32__ winsock_cleanup(); #endif return 0; }
int main(int argc, char **argv, char **envp) /* qalter */ { int c; int errflg=0; int any_failed=0; char *pc; int i; struct attrl *attrib = NULL; char *keyword; char *valuewd; char *erplace; time_t after; char a_value[80]; char job_id[PBS_MAXCLTJOBID]; char job_id_out[PBS_MAXCLTJOBID]; char server_out[MAXSERVERNAME]; char rmt_server[MAXSERVERNAME]; struct ecl_attribute_errors *err_list; #ifdef WIN32 struct attrl *ap = NULL; short int nSizeofHostName = 0; char* orig_apvalue = NULL; char* temp_apvalue = NULL; #endif #define GETOPT_ARGS "a:A:c:e:h:j:k:l:m:M:N:o:p:r:R:S:u:W:P:" /*test for real deal or just version and exit*/ execution_mode(argc, argv); #ifdef WIN32 winsock_init(); #endif while ((c = getopt(argc, argv, GETOPT_ARGS)) != EOF) switch (c) { case 'a': if ((after = cvtdate(optarg)) < 0) { fprintf(stderr, "qalter: illegal -a value\n"); errflg++; break; } sprintf(a_value, "%ld", (long)after); set_attr(&attrib, ATTR_a, a_value); break; case 'A': set_attr(&attrib, ATTR_A, optarg); break; case 'P': set_attr(&attrib, ATTR_project, optarg); break; case 'c': while (isspace((int)*optarg)) optarg++; pc = optarg; if ((pc[0] == 'u') && (pc[1] == '\0')) { fprintf(stderr, "qalter: illegal -c value\n"); errflg++; break; } set_attr(&attrib, ATTR_c, optarg); break; case 'e': set_attr(&attrib, ATTR_e, optarg); break; case 'h': while (isspace((int)*optarg)) optarg++; set_attr(&attrib, ATTR_h, optarg); break; case 'j': set_attr(&attrib, ATTR_j, optarg); break; case 'k': set_attr(&attrib, ATTR_k, optarg); break; case 'l': if ((i = set_resources(&attrib, optarg, TRUE, &erplace)) != 0) { if (i > 1) { pbs_prt_parse_err("qalter: illegal -l value\n", optarg, erplace-optarg, i); } else fprintf(stderr, "qalter: illegal -l value\n"); errflg++; } break; case 'm': while (isspace((int)*optarg)) optarg++; set_attr(&attrib, ATTR_m, optarg); break; case 'M': set_attr(&attrib, ATTR_M, optarg); break; case 'N': set_attr(&attrib, ATTR_N, optarg); break; case 'o': set_attr(&attrib, ATTR_o, optarg); break; case 'p': while (isspace((int)*optarg)) optarg++; set_attr(&attrib, ATTR_p, optarg); break; case 'r': if (strlen(optarg) != 1) { fprintf(stderr, "qalter: illegal -r value\n"); errflg++; break; } if (*optarg != 'y' && *optarg != 'n') { fprintf(stderr, "qalter: illegal -r value\n"); errflg++; break; } set_attr(&attrib, ATTR_r, optarg); break; case 'R': set_attr(&attrib, ATTR_R, optarg); break; case 'S': set_attr(&attrib, ATTR_S, optarg); break; case 'u': set_attr(&attrib, ATTR_u, optarg); break; case 'W': while (isspace((int)*optarg)) optarg++; if (strlen(optarg) == 0) { fprintf(stderr, "qalter: illegal -W value\n"); errflg++; break; } #ifdef WIN32 back2forward_slash2(optarg); #endif i = parse_equal_string(optarg, &keyword, &valuewd); while (i == 1) { set_attr(&attrib, keyword, valuewd); i = parse_equal_string(NULL, &keyword, &valuewd); } if (i == -1) { fprintf(stderr, "qalter: illegal -W value\n"); errflg++; } break; case '?': default : errflg++; break; } if (errflg || optind == argc) { print_usage(); exit(2); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qalter: unable to initialize security library.\n"); exit(1); } for (; optind < argc; optind++) { int connect; int stat=0; int located = FALSE; strcpy(job_id, argv[optind]); if (get_server(job_id, job_id_out, server_out)) { fprintf(stderr, "qalter: illegally formed job identifier: %s\n", job_id); any_failed = 1; continue; } cnt: connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qalter: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); any_failed = pbs_errno; continue; } stat = pbs_alterjob(connect, job_id_out, attrib, NULL); if (stat && (pbs_errno != PBSE_UNKJOBID)) { if ((err_list = pbs_get_attributes_in_error(connect))) handle_attribute_errors(connect, err_list, job_id_out); prt_job_err("qalter", connect, job_id_out); any_failed = pbs_errno; } else if (stat && (pbs_errno == PBSE_UNKJOBID) && !located) { located = TRUE; if (locate_job(job_id_out, server_out, rmt_server)) { pbs_disconnect(connect); strcpy(server_out, rmt_server); goto cnt; } prt_job_err("qalter", connect, job_id_out); any_failed = pbs_errno; } pbs_disconnect(connect); } CS_close_app(); exit(any_failed); }
int main(int argc, char *argv[]) { struct options options; clock_t start; bool create_db; GError *error = NULL; bool success; daemonize_close_stdin(); #ifdef HAVE_LOCALE_H /* initialize locale */ setlocale(LC_CTYPE,""); #endif g_set_application_name("Music Player Daemon"); /* enable GLib's thread safety code */ g_thread_init(NULL); winsock_init(); idle_init(); dirvec_init(); songvec_init(); tag_pool_init(); config_global_init(); success = parse_cmdline(argc, argv, &options, &error); if (!success) { g_warning("%s", error->message); g_error_free(error); return EXIT_FAILURE; } glue_daemonize_init(&options); stats_global_init(); tag_lib_init(); log_init(options.verbose, options.log_stderr); success = listen_global_init(&error); if (!success) { g_warning("%s", error->message); g_error_free(error); return EXIT_FAILURE; } daemonize_set_user(); main_task = g_thread_self(); main_loop = g_main_loop_new(NULL, FALSE); main_cond = g_cond_new(); event_pipe_init(); event_pipe_register(PIPE_EVENT_IDLE, idle_event_emitted); path_global_init(); glue_mapper_init(); initPermissions(); playlist_global_init(); spl_global_init(); #ifdef ENABLE_ARCHIVE archive_plugin_init_all(); #endif decoder_plugin_init_all(); update_global_init(); create_db = !glue_db_init_and_load(); glue_sticker_init(); command_init(); initialize_decoder_and_player(); volume_init(); initAudioConfig(); audio_output_all_init(); client_manager_init(); replay_gain_global_init(); if (!input_stream_global_init(&error)) { g_warning("%s", error->message); g_error_free(error); return EXIT_FAILURE; } playlist_list_global_init(); daemonize(options.daemon); setup_log_output(options.log_stderr); initSigHandlers(); initZeroconf(); player_create(); if (create_db) { /* the database failed to load: recreate the database */ unsigned job = update_enqueue(NULL, true); if (job == 0) g_error("directory update failed"); } glue_state_file_init(); success = config_get_bool(CONF_AUTO_UPDATE, false); #ifdef ENABLE_INOTIFY if (success && mapper_has_music_directory()) mpd_inotify_init(config_get_unsigned(CONF_AUTO_UPDATE_DEPTH, G_MAXUINT)); #else if (success) g_warning("inotify: auto_update was disabled. enable during compilation phase"); #endif config_global_check(); /* enable all audio outputs (if not already done by playlist_state_restore() */ pc_update_audio(); /* run the main loop */ g_main_loop_run(main_loop); /* cleanup */ g_main_loop_unref(main_loop); #ifdef ENABLE_INOTIFY mpd_inotify_finish(); #endif state_file_finish(); pc_kill(); finishZeroconf(); client_manager_deinit(); listen_global_finish(); playlist_global_finish(); start = clock(); db_finish(); g_debug("db_finish took %f seconds", ((float)(clock()-start))/CLOCKS_PER_SEC); #ifdef ENABLE_SQLITE sticker_global_finish(); #endif g_cond_free(main_cond); event_pipe_deinit(); playlist_list_global_finish(); input_stream_global_finish(); audio_output_all_finish(); volume_finish(); mapper_finish(); path_global_finish(); finishPermissions(); pc_deinit(); command_finish(); update_global_finish(); decoder_plugin_deinit_all(); #ifdef ENABLE_ARCHIVE archive_plugin_deinit_all(); #endif config_global_finish(); tag_pool_deinit(); songvec_deinit(); dirvec_deinit(); idle_deinit(); stats_global_finish(); daemonize_finish(); #ifdef WIN32 WSACleanup(); #endif close_log_files(); return EXIT_SUCCESS; }
int main(int argc, char **argv) { srand(time(NULL)); int i, c; int pid_flags = 0; int mptcp = 0; int mtu = 0; char *user = NULL; char *local_port = NULL; char *local_addr = NULL; char *password = NULL; char *timeout = NULL; char *method = NULL; char *pid_path = NULL; char *conf_path = NULL; char *iface = NULL; int remote_num = 0; ss_addr_t remote_addr[MAX_REMOTE_NUM]; char *remote_port = NULL; ss_addr_t tunnel_addr = { .host = NULL, .port = NULL }; char *tunnel_addr_str = NULL; int option_index = 0; static struct option long_options[] = { { "mtu", required_argument, 0, 0 }, { "mptcp", no_argument, 0, 0 }, { "help", no_argument, 0, 0 }, { 0, 0, 0, 0 } }; opterr = 0; USE_TTY(); #ifdef ANDROID while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:n:P:huUvVA6", long_options, &option_index)) != -1) { #else while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:i:c:b:L:a:n:huUvA6", long_options, &option_index)) != -1) { #endif switch (c) { case 0: if (option_index == 0) { mtu = atoi(optarg); LOGI("set MTU to %d", mtu); } else if (option_index == 1) { mptcp = 1; LOGI("enable multipath TCP"); } else if (option_index == 2) { usage(); exit(EXIT_SUCCESS); } break; case 's': if (remote_num < MAX_REMOTE_NUM) { remote_addr[remote_num].host = optarg; remote_addr[remote_num++].port = NULL; } break; case 'p': remote_port = optarg; break; case 'l': local_port = optarg; break; case 'k': password = optarg; break; case 'f': pid_flags = 1; pid_path = optarg; break; case 't': timeout = optarg; break; case 'm': method = optarg; break; case 'c': conf_path = optarg; break; case 'i': iface = optarg; break; case 'b': local_addr = optarg; break; case 'u': mode = TCP_AND_UDP; break; case 'U': mode = UDP_ONLY; break; case 'L': tunnel_addr_str = optarg; break; case 'a': user = optarg; break; #ifdef HAVE_SETRLIMIT case 'n': nofile = atoi(optarg); break; #endif case 'v': verbose = 1; break; case 'h': usage(); exit(EXIT_SUCCESS); case 'A': auth = 1; break; case '6': ipv6first = 1; break; #ifdef ANDROID case 'V': vpn = 1; break; case 'P': prefix = optarg; break; #endif case '?': // The option character is not recognized. LOGE("Unrecognized option: %s", optarg); opterr = 1; break; } } if (opterr) { usage(); exit(EXIT_FAILURE); } if (argc == 1) { if (conf_path == NULL) { conf_path = DEFAULT_CONF_PATH; } } if (conf_path != NULL) { jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; for (i = 0; i < remote_num; i++) remote_addr[i] = conf->remote_addr[i]; } if (remote_port == NULL) { remote_port = conf->remote_port; } if (local_addr == NULL) { local_addr = conf->local_addr; } if (local_port == NULL) { local_port = conf->local_port; } if (password == NULL) { password = conf->password; } if (method == NULL) { method = conf->method; } if (timeout == NULL) { timeout = conf->timeout; } if (user == NULL) { user = conf->user; } if (auth == 0) { auth = conf->auth; } if (tunnel_addr_str == NULL) { tunnel_addr_str = conf->tunnel_address; } if (mode == TCP_ONLY) { mode = conf->mode; } if (mtu == 0) { mtu = conf->mtu; } if (mptcp == 0) { mptcp = conf->mptcp; } #ifdef HAVE_SETRLIMIT if (nofile == 0) { nofile = conf->nofile; } #endif } if (remote_num == 0 || remote_port == NULL || tunnel_addr_str == NULL || local_port == NULL || password == NULL) { usage(); exit(EXIT_FAILURE); } if (method == NULL) { method = "rc4-md5"; } if (timeout == NULL) { timeout = "60"; } #ifdef HAVE_SETRLIMIT /* * no need to check the return value here since we will show * the user an error message if setrlimit(2) fails */ if (nofile > 1024) { if (verbose) { LOGI("setting NOFILE to %d", nofile); } set_nofile(nofile); } #endif if (local_addr == NULL) { local_addr = "127.0.0.1"; } if (pid_flags) { USE_SYSLOG(argv[0]); daemonize(pid_path); } if (ipv6first) { LOGI("resolving hostname to IPv6 address first"); } if (auth) { LOGI("onetime authentication enabled"); } // parse tunnel addr parse_addr(tunnel_addr_str, &tunnel_addr); if (tunnel_addr.port == NULL) { FATAL("tunnel port is not defined"); } #ifdef __MINGW32__ winsock_init(); #else // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); signal(SIGINT, signal_cb); signal(SIGTERM, signal_cb); #endif // Setup keys LOGI("initializing ciphers... %s", method); int m = enc_init(password, method); // Setup proxy context struct listen_ctx listen_ctx; listen_ctx.tunnel_addr = tunnel_addr; listen_ctx.remote_num = remote_num; listen_ctx.remote_addr = ss_malloc(sizeof(struct sockaddr *) * remote_num); for (i = 0; i < remote_num; i++) { char *host = remote_addr[i].host; char *port = remote_addr[i].port == NULL ? remote_port : remote_addr[i].port; struct sockaddr_storage *storage = ss_malloc(sizeof(struct sockaddr_storage)); memset(storage, 0, sizeof(struct sockaddr_storage)); if (get_sockaddr(host, port, storage, 1, ipv6first) == -1) { FATAL("failed to resolve the provided hostname"); } listen_ctx.remote_addr[i] = (struct sockaddr *)storage; } listen_ctx.timeout = atoi(timeout); listen_ctx.iface = iface; listen_ctx.method = m; listen_ctx.mptcp = mptcp; struct ev_loop *loop = EV_DEFAULT; if (mode != UDP_ONLY) { // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port); if (listenfd == -1) { FATAL("bind() error:"); } if (listen(listenfd, SOMAXCONN) == -1) { FATAL("listen() error:"); } setnonblocking(listenfd); listen_ctx.fd = listenfd; ev_io_init(&listen_ctx.io, accept_cb, listenfd, EV_READ); ev_io_start(loop, &listen_ctx.io); } // Setup UDP if (mode != TCP_ONLY) { LOGI("UDP relay enabled"); init_udprelay(local_addr, local_port, listen_ctx.remote_addr[0], get_sockaddr_len(listen_ctx.remote_addr[0]), tunnel_addr, mtu, m, auth, listen_ctx.timeout, iface); } if (mode == UDP_ONLY) { LOGI("TCP relay disabled"); } LOGI("listening at %s:%s", local_addr, local_port); // setuid if (user != NULL && ! run_as(user)) { FATAL("failed to switch user"); } #ifndef __MINGW32__ if (geteuid() == 0){ LOGI("running from root user"); } #endif ev_run(loop, 0); #ifdef __MINGW32__ winsock_cleanup(); #endif return 0; }
/** * @brief * main - the entry point in pbs_account_win.c * * @param[in] argc - argument count * @param[in] argv - argument variables. * @param[in] env - environment values. * * @return int * @retval 0 : success * @retval !=0 : error code */ main(int argc, char *argv[]) { SID *sa_sid = NULL; /* service account SID */ char sa_name[PBS_MAXHOSTNAME+UNLEN+2] = {'\0'}; /* service account name */ /* domain\user\0 */ int ret_val = 0; int c_opt = 0; int s_opt = 0; int a_opt = 0; int p_opt = 0; int R_opt = 0; int U_opt = 0; int instid_opt = 0; char service_bin_path[MAXPATHLEN+1] = {'\0'}; char service_name[MAXPATHLEN+1] = {'\0'}; int i = 0; char outputfile[MAXPATHLEN+1] = {'\0'}; char instanceName[MAXPATHLEN+1] = {'\0'}; int output_fd = -1; struct passwd *pw = NULL; char *p = NULL; winsock_init(); /*test for real deal or just version and exit*/ execution_mode(argc, argv); strcpy(exec_unamef, getlogin_full()); strcpy(exec_dname, "."); if ((p=strchr(exec_unamef, '\\'))) { *p = '\0'; strcpy(exec_dname, exec_unamef); *p = '\\'; } strcpy(sa_password, ""); strcpy(outputfile, ""); /* with no option, check only if service account exists */ if (argc == 1) { int in_domain_environment = 0; char dname[PBS_MAXHOSTNAME+1] = {'\0'}; char dctrl[PBS_MAXHOSTNAME+1] = {'\0'}; wchar_t unamew[UNLEN+1] = {'\0'}; wchar_t dctrlw[PBS_MAXHOSTNAME+1] = {'\0'}; USER_INFO_0 *ui1_ptr = NULL; NET_API_STATUS netst = 0; /* figure out the domain controller hostname (dctrl) */ /* in domain environment, */ /* domain name (dname) != domain controller hostname */ /* in standalone environment, */ /* domain name (dname) == domain controller hostname */ in_domain_environment = GetComputerDomainName(dname); strcpy(dctrl, dname); if (in_domain_environment) { char dname_a[PBS_MAXHOSTNAME+1]; get_dcinfo(dname, dname_a, dctrl); } /* convert strings to "wide" format */ mbstowcs(unamew, service_accountname, UNLEN+1); mbstowcs(dctrlw, dctrl, PBS_MAXHOSTNAME+1); netst = wrap_NetUserGetInfo(dctrlw, unamew, 1, (LPBYTE *)&ui1_ptr); if (strlen(winlog_buffer) > 0) { fprintf(stderr, "%s\n", winlog_buffer); } if (netst == NERR_UserNotFound) { fprintf(stderr, "%s not found!\n", service_accountname); if (in_domain_environment && stricmp(exec_dname, dname) != 0) { fprintf(stderr, "But no privilege to create service account %s\\%s!\n", dname, service_accountname); ret_val=2; } else { ret_val=1; } } else if ((netst == ERROR_ACCESS_DENIED) || (netst == ERROR_LOGON_FAILURE)) { fprintf(stderr, "no privilege to obtain info for service account %s\\%s!\n", dname, service_accountname); ret_val= 2; } else { fprintf(stderr, "service account is %s\\%s!\n", dname, service_accountname); ret_val = 0; } if (ui1_ptr != NULL) NetApiBufferFree(ui1_ptr); goto end_pbs_account; } i = 1; while (i < argc) { if (strcmp(argv[i], "-c") == 0) { c_opt = 1; i++; } else if (strcmp(argv[i], "--ci") == 0) { c_opt = 1; for_info_only = 1; i++; } else if (strcmp(argv[i], "-s") == 0) { s_opt = 1; i++; } else if (strcmp(argv[i], "-a") == 0) { if ((argv[i+1] == NULL) || (argv[i+1][0] == '-')) { fprintf(stderr, "No service account name argument supplied!\n"); usage(argv[0]); exit(1); } a_opt = 1; strcpy(service_accountname, argv[i+1]); i+=2; } else if (strcmp(argv[i], "-p") == 0) { if ((argv[i+1] == NULL) || (argv[i+1][0] == '-')) { fprintf(stderr, "No password argument supplied!\n"); usage(argv[0]); exit(1); } p_opt = 1; strcpy(sa_password, argv[i+1]); cache_usertoken_and_homedir(service_accountname, NULL, 0, read_sa_password, (char*)service_accountname, decrypt_sa_password, 1); i+=2; } else if (strcmp(argv[i], "--reg") == 0) { if ((argv[i+1] == NULL) || (argv[i+1][0] == '-')) { fprintf(stderr, "No service binary path given\n"); usage(argv[0]); exit(1); } R_opt = 1; strcpy(service_bin_path, argv[i+1]); i+=2; } else if (strcmp(argv[i], "--unreg") == 0) { if ((argv[i+1] == NULL) || (argv[i+1][0] == '-')) { fprintf(stderr, "No service binary path given\n"); usage(argv[0]); exit(1); } U_opt = 1; strcpy(service_bin_path, argv[i+1]); i+=2; } else if (strcmp(argv[i], "-o") == 0) { if ((argv[i+1] == NULL) || (argv[i+1][0] == '-')) { fprintf(stderr, "No output path argument supplied!\n"); usage(argv[0]); exit(1); } p_opt = 1; strcpy(outputfile, argv[i+1]); i+=2; } else if (strncmp(argv[i], "--instid", strlen("--instid")) == 0) { if ((argv[i+1] == NULL) || (argv[i+1][0] == '-')) { fprintf(stderr, "No instance id supplied!\n"); usage(argv[0]); exit(1); } instid_opt = 1; strncpy(instanceName, argv[i+1], MAXPATHLEN); i+=2; } else { fprintf(stderr, "Unknown option %s\n", argv[i]); usage(argv[0]); exit(1); } } if (strlen(outputfile) > 0) { if ((output_fd=open(outputfile, O_RDWR|O_CREAT, 0600)) != -1) { _dup2(output_fd, 1); /* put stdout in file */ _dup2(output_fd, 2); /* put stderr in file */ } } /* prompt for password if not supplied with -p */ if ((c_opt || R_opt) && (strcmp(sa_password, "") == 0)) { prompt_to_get_password(sa_password); cache_usertoken_and_homedir(service_accountname, NULL, 0, read_sa_password, (char *)service_accountname, decrypt_sa_password, 1); } /* Need to get service_name */ if (R_opt || U_opt) { char *p = NULL; int k = 0; strcpy(service_name, service_bin_path); if ((p=strrchr(service_bin_path, '\\'))) { strcpy(service_name, p+1); } if ((p=strrchr(service_name, '.'))) {/*remove .exe portion*/ *p = '\0'; } /* translate from lower-case to upper-case */ for (k=0; k < strlen(service_name); k++) { service_name[k] = toupper(service_name[k]); } if (instid_opt) { strcat_s(service_name, MAXPATHLEN, "_"); strcat_s(service_name, MAXPATHLEN, instanceName); } } if (c_opt) { if (add_service_account(sa_password) == 0) { ret_val = 3; goto end_pbs_account; } } if (s_opt || R_opt) { /* need service account name */ sa_sid = getusersid2(service_accountname, sa_name); if (sa_sid == NULL) { fprintf(stderr, "%s not found!\n", service_accountname); ret_val= 1; goto end_pbs_account; } if (!isAdminPrivilege(service_accountname)) { fprintf(stderr, "%s is not ADMIN! - %s\n", service_accountname, winlog_buffer); ret_val = 2; goto end_pbs_account; } } if (s_opt) { int r1, r2, r3, r4; printf("Setting the following privileges to %s:\n", sa_name); r1 = add_privilege(sa_sid, SE_CREATE_TOKEN_NAME); r2 = add_privilege(sa_sid, SE_ASSIGNPRIMARYTOKEN_NAME); r3 = add_privilege(sa_sid, SE_SERVICE_LOGON_NAME); r4 = add_privilege(sa_sid, SE_TCB_NAME); if ((r1 != 0) || (r2 != 0) || (r3 != 0) || (r4 != 0)) { ret_val = 4; goto end_pbs_account; } } if (R_opt) { ret_val = register_scm(__TEXT(service_name), service_bin_path, sa_name, sa_password); } if (U_opt) { ret_val = unregister_scm(__TEXT(service_name)); } end_pbs_account: if (sa_sid != NULL) LocalFree(sa_sid); if (strlen(sa_password) > 0) memset((char *)sa_password, 0, strlen(sa_password)); if (output_fd != -1) (void)close(output_fd); exit(ret_val); }
int main(int argc, char *argv[]) { int i; char mom_name[PBS_MAXHOSTNAME+1]; int mom_port = 0; int c, rc; int mom_sd; char *req; #ifdef WIN32 if (winsock_init()) { return 1; } #endif if (gethostname(mom_name, (sizeof(mom_name) - 1)) < 0 ) mom_name[0] = '\0'; while ((c = getopt(argc, argv, "m:p:")) != EOF) { switch (c) { case 'm': strcpy(mom_name, optarg); break; case 'p': mom_port = atoi(optarg); break; default: fprintf(stderr, "Bad option: %c\n", c); } } if (mom_name[0] == '\0' || optind == argc) { fprintf(stderr, "Error in usage: pbs_rmget [-m mom name] [-p mom port] <req1>...[reqN]\n"); return 1; } if(set_msgdaemonname("pbs_rmget")) { fprintf(stderr, "Out of memory\n"); return 1; } /* load the pbs conf file */ if (pbs_loadconf(0) == 0) { fprintf(stderr, "%s: Configuration error\n", argv[0]); return (1); } if (pbs_conf.pbs_use_tcp == 1) { struct tpp_config tpp_conf; fd_set selset; struct timeval tv; if (!pbs_conf.pbs_leaf_name) { char my_hostname[PBS_MAXHOSTNAME+1]; if (gethostname(my_hostname, (sizeof(my_hostname) - 1)) < 0) { fprintf(stderr, "Failed to get hostname\n"); return -1; } pbs_conf.pbs_leaf_name = get_all_ips(my_hostname, log_buffer, sizeof(log_buffer) - 1); if (!pbs_conf.pbs_leaf_name) { fprintf(stderr, "%s\n", log_buffer); fprintf(stderr, "%s\n", "Unable to determine TPP node name"); return -1; } } /* We don't want to show logs related to connecting pbs_comm on console * this set this flag to ignore it */ log_mask = SHOW_NONE; /* set tpp function pointers */ set_tpp_funcs(log_tppmsg); /* call tpp_init */ rc = 0; #ifndef WIN32 if (pbs_conf.auth_method == AUTH_MUNGE) rc = set_tpp_config(&pbs_conf, &tpp_conf, pbs_conf.pbs_leaf_name, -1, pbs_conf.pbs_leaf_routers, pbs_conf.pbs_use_compression, TPP_AUTH_EXTERNAL, get_ext_auth_data, validate_ext_auth_data); else #endif rc = set_tpp_config(&pbs_conf, &tpp_conf, pbs_conf.pbs_leaf_name, -1, pbs_conf.pbs_leaf_routers, pbs_conf.pbs_use_compression, TPP_AUTH_RESV_PORT, NULL, NULL); if (rc == -1) { fprintf(stderr, "Error setting TPP config\n"); return -1; } if ((rpp_fd = tpp_init(&tpp_conf)) == -1) { fprintf(stderr, "rpp_init failed\n"); return -1; } /* * Wait for net to get restored, ie, app to connect to routers */ FD_ZERO(&selset); FD_SET(rpp_fd, &selset); tv.tv_sec = 5; tv.tv_usec = 0; select(FD_SETSIZE, &selset, NULL, NULL, &tv); rpp_poll(); /* to clear off the read notification */ /* Once the connection is established we can unset log_mask */ log_mask &= ~SHOW_NONE; } else { /* set rpp function pointers */ set_rpp_funcs(log_rppfail); } /* get the FQDN of the mom */ c = get_fullhostname(mom_name, mom_name, (sizeof(mom_name) - 1)); if (c == -1) { fprintf(stderr, "Unable to get full hostname for mom %s\n", mom_name); return -1; } if ((mom_sd = openrm(mom_name, mom_port)) < 0) { fprintf(stderr, "Unable to open connection to mom: %s:%d\n", mom_name, mom_port); return 1; } for (i = optind; i < argc; i++) addreq(mom_sd, argv[i]); for (i = optind; i < argc; i++) { req = getreq(mom_sd); if (req == NULL) { fprintf(stderr, "Error getting response %d from mom.\n", i - optind); return 1; } printf("[%d] %s\n", i - optind, req); free(req); } closerm(mom_sd); return 0; }
/** * @brief * main - the initialization and main loop of pbs_daemon */ int main(int argc, char *argv[]) { char buf[4096]; char *param_name, *param_val; int rc; execution_mode(argc, argv); if(set_msgdaemonname("PBS_send_hooks")) { fprintf(stderr, "Out of memory\n"); return 1; } pbs_loadconf(0); /* If we are not run with real and effective uid of 0, forget it */ if (!isAdminPrivilege(getlogin())) { fprintf(stderr, "%s: Must be run by root\n", argv[0]); exit(-1); } pbs_client_thread_set_single_threaded_mode(); /* disable attribute verification */ set_no_attribute_verification(); /* initialize the thread context */ if (pbs_client_thread_init_thread_context() != 0) { fprintf(stderr, "%s: Unable to initialize thread context\n", argv[0]); exit(-1); } winsock_init(); connection_init(); while (fgets(buf, sizeof(buf), stdin) != NULL) { buf[strlen(buf)-1] = '\0'; /* gets rid of newline */ param_name = buf; param_val = strchr(buf, '='); if (param_val) { *param_val = '\0'; param_val++; } else { /* bad param_val -- skipping */ break; } if (strcmp(param_name, "path_log") == 0) { path_log[0] = '\0'; strncpy(path_log, param_val, MAXPATHLEN); } else if (strcmp(param_name, "path_hooks") == 0) { path_hooks = strdup(param_val); if (path_hooks == NULL) exit(-1); } else if (strcmp(param_name, "log_file") == 0) { log_file = strdup(param_val); if (log_file == NULL) exit(-1); } else if (strcmp(param_name, "path_hooks_tracking") == 0) { path_hooks_tracking = strdup(param_val); if (path_hooks_tracking == NULL) exit(-1); } else if (strcmp(param_name, "hook_action_tid") == 0) { #ifdef WIN32 hook_action_tid_set(_atoi64(param_val)); #else hook_action_tid_set(atoll(param_val)); #endif } else if (strcmp(param_name, "pbs_server_port_dis") == 0) { pbs_server_port_dis = atoi(param_val); } else if (strcmp(param_name, "pbs_server_addr") == 0) { pbs_server_addr = atol(param_val); } else break; } (void)log_open_main(log_file, path_log, 1); /* silent open */ hook_track_recov(); rc = sync_mom_hookfiles(NULL); log_close(0); /* silent close */ net_close(-1); if (log_file != NULL) free(log_file); if (path_hooks != NULL) free(path_hooks); if (path_hooks_tracking != NULL) free(path_hooks_tracking); exit(rc); }
int main(int argc, char **argv) { static int version = 0; static GOptionEntry options[] = { { "dummy", 'd', 0, G_OPTION_ARG_NONE, &dummy, "Use the dummy terminal mode", NULL }, { "version", 'v', 0, G_OPTION_ARG_NONE, &version, "Display irssi version", NULL }, { NULL } }; #ifdef USE_GC g_mem_set_vtable(&gc_mem_table); #endif core_register_options(); fe_common_core_register_options(); args_register(options); args_execute(argc, argv); if (version) { printf(PACKAGE_TARNAME" " PACKAGE_VERSION" (%d %04d)\n", IRSSI_VERSION_DATE, IRSSI_VERSION_TIME); return 0; } srand(time(NULL)); dummy = FALSE; quitting = FALSE; core_preinit(argv[0]); check_files(); #ifdef WIN32 winsock_init(); #endif #ifdef HAVE_SOCKS SOCKSinit(argv[0]); #endif /* setlocale() must be called at the beginning before any calls that affect it, especially regexps seem to break if they're generated before this call. locales aren't actually used for anything else than autodetection of UTF-8 currently.. furthermore to get the users's charset with g_get_charset() properly you have to call setlocale(LC_ALL, "") */ setlocale(LC_ALL, ""); textui_init(); if (!dummy && !term_init()) { fprintf(stderr, "Can't initialize screen handling, quitting.\n"); fprintf(stderr, "You can still use the dummy mode with -d parameter\n"); return 1; } textui_finish_init(); main_loop = g_main_new(TRUE); /* Does the same as g_main_run(main_loop), except we can call our dirty-checker after each iteration */ while (!quitting) { #ifdef USE_GC GC_collect_a_little(); #endif if (!dummy) term_refresh_freeze(); g_main_iteration(TRUE); if (!dummy) term_refresh_thaw(); if (reload_config) { /* SIGHUP received, do /RELOAD */ reload_config = FALSE; signal_emit("command reload", 1, ""); } dirty_check(); } g_main_destroy(main_loop); textui_deinit(); session_upgrade(); /* if we /UPGRADEd, start the new process */ return 0; }
int main(int argc, char **argv) { int i, c; int pid_flags = 0; char *user = NULL; char *password = NULL; char *timeout = NULL; char *method = NULL; char *pid_path = NULL; char *conf_path = NULL; char *iface = NULL; int server_num = 0; const char *server_host[MAX_REMOTE_NUM]; char * nameservers[MAX_DNS_NUM + 1]; int nameserver_num = 0; int option_index = 0; static struct option long_options[] = { { "fast-open", no_argument, 0, 0 }, { "acl", required_argument, 0, 0 }, { "manager-address", required_argument, 0, 0 }, { 0, 0, 0, 0 } }; opterr = 0; USE_TTY(); while ((c = getopt_long(argc, argv, "f:s:p:l:k:t:m:c:i:d:a:uUvA", long_options, &option_index)) != -1) { switch (c) { case 0: if (option_index == 0) { fast_open = 1; } else if (option_index == 1) { LOGI("initialize acl..."); acl = !init_acl(optarg); } else if (option_index == 2) { manager_address = optarg; } break; case 's': if (server_num < MAX_REMOTE_NUM) { server_host[server_num++] = optarg; } break; case 'p': server_port = optarg; break; case 'k': password = optarg; break; case 'f': pid_flags = 1; pid_path = optarg; break; case 't': timeout = optarg; break; case 'm': method = optarg; break; case 'c': conf_path = optarg; break; case 'i': iface = optarg; break; case 'd': if (nameserver_num < MAX_DNS_NUM) { nameservers[nameserver_num++] = optarg; } break; case 'a': user = optarg; break; case 'u': mode = TCP_AND_UDP; break; case 'U': mode = UDP_ONLY; break; case 'v': verbose = 1; break; case 'A': auth = 1; break; } } if (opterr) { usage(); exit(EXIT_FAILURE); } if (argc == 1) { if (conf_path == NULL) { conf_path = DEFAULT_CONF_PATH; } } if (conf_path != NULL) { jconf_t *conf = read_jconf(conf_path); if (server_num == 0) { server_num = conf->remote_num; for (i = 0; i < server_num; i++) { server_host[i] = conf->remote_addr[i].host; } } if (server_port == NULL) { server_port = conf->remote_port; } if (password == NULL) { password = conf->password; } if (method == NULL) { method = conf->method; } if (timeout == NULL) { timeout = conf->timeout; } #ifdef TCP_FASTOPEN if (fast_open == 0) { fast_open = conf->fast_open; } #endif #ifdef HAVE_SETRLIMIT if (nofile == 0) { nofile = conf->nofile; } /* * no need to check the return value here since we will show * the user an error message if setrlimit(2) fails */ if (nofile) { if (verbose) { LOGI("setting NOFILE to %d", nofile); } set_nofile(nofile); } #endif if (conf->nameserver != NULL) { nameservers[nameserver_num++] = conf->nameserver; } } if (server_num == 0) { server_host[server_num++] = NULL; } if (server_num == 0 || server_port == NULL || password == NULL) { usage(); exit(EXIT_FAILURE); } if (method == NULL) { method = "table"; } if (timeout == NULL) { timeout = "60"; } if (pid_flags) { USE_SYSLOG(argv[0]); daemonize(pid_path); } if (fast_open == 1) { #ifdef TCP_FASTOPEN LOGI("using tcp fast open"); #else LOGE("tcp fast open is not supported by this environment"); #endif } if (auth) { LOGI("onetime authentication enabled"); } #ifdef __MINGW32__ winsock_init(); #else // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGCHLD, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif struct ev_signal sigint_watcher; struct ev_signal sigterm_watcher; ev_signal_init(&sigint_watcher, signal_cb, SIGINT); ev_signal_init(&sigterm_watcher, signal_cb, SIGTERM); ev_signal_start(EV_DEFAULT, &sigint_watcher); ev_signal_start(EV_DEFAULT, &sigterm_watcher); // setup keys LOGI("initialize ciphers... %s", method); int m = enc_init(password, method); // inilitialize ev loop struct ev_loop *loop = EV_DEFAULT; // setup udns if (nameserver_num == 0) { #ifdef __MINGW32__ nameservers[nameserver_num++] = "8.8.8.8"; resolv_init(loop, nameservers, nameserver_num); #else resolv_init(loop, NULL, 0); #endif } else { resolv_init(loop, nameservers, nameserver_num); } for (int i = 0; i < nameserver_num; i++) { LOGI("using nameserver: %s", nameservers[i]); } // inilitialize listen context struct listen_ctx listen_ctx_list[server_num]; // bind to each interface while (server_num > 0) { int index = --server_num; const char * host = server_host[index]; if (mode != UDP_ONLY) { // Bind to port int listenfd; listenfd = create_and_bind(host, server_port); if (listenfd < 0) { FATAL("bind() error"); } if (listen(listenfd, SSMAXCONN) == -1) { FATAL("listen() error"); } setnonblocking(listenfd); struct listen_ctx *listen_ctx = &listen_ctx_list[index]; // Setup proxy context listen_ctx->timeout = atoi(timeout); listen_ctx->fd = listenfd; listen_ctx->method = m; listen_ctx->iface = iface; listen_ctx->loop = loop; ev_io_init(&listen_ctx->io, accept_cb, listenfd, EV_READ); ev_io_start(loop, &listen_ctx->io); } // Setup UDP if (mode != TCP_ONLY) { init_udprelay(server_host[index], server_port, m, atoi(timeout), iface); } LOGI("listening at %s:%s", host ? host : "*", server_port); } if (manager_address != NULL) { ev_timer_init(&stat_update_watcher, stat_update_cb, UPDATE_INTERVAL, UPDATE_INTERVAL); ev_timer_start(EV_DEFAULT, &stat_update_watcher); } if (mode != TCP_ONLY) { LOGI("UDP relay enabled"); } if (mode == UDP_ONLY) { LOGI("TCP relay disabled"); } // setuid if (user != NULL) { run_as(user); } // Init connections cork_dllist_init(&connections); // start ev loop ev_run(loop, 0); if (verbose) { LOGI("closed gracefully"); } if (manager_address != NULL) { ev_timer_stop(EV_DEFAULT, &stat_update_watcher); } // Clean up for (int i = 0; i <= server_num; i++) { struct listen_ctx *listen_ctx = &listen_ctx_list[i]; if (mode != UDP_ONLY) { ev_io_stop(loop, &listen_ctx->io); close(listen_ctx->fd); } } if (mode != UDP_ONLY) { free_connections(loop); } if (mode != TCP_ONLY) { free_udprelay(); } resolv_shutdown(loop); #ifdef __MINGW32__ winsock_cleanup(); #endif ev_signal_stop(EV_DEFAULT, &sigint_watcher); ev_signal_stop(EV_DEFAULT, &sigterm_watcher); return 0; }
int main(int argc, char **argv, char **envp) /* qmove */ { int any_failed=0; char job_id[PBS_MAXCLTJOBID]; /* from the command line */ char destination[PBS_MAXSERVERNAME]; /* from the command line */ char *q_n_out, *s_n_out; char job_id_out[PBS_MAXCLTJOBID]; char server_out[MAXSERVERNAME]; char rmt_server[MAXSERVERNAME]; /*test for real deal or just version and exit*/ execution_mode(argc, argv); #ifdef WIN32 winsock_init(); #endif if (argc < 3) { static char usage[]="usage: qmove destination job_identifier...\n"; static char usag2[]=" qmove --version\n"; fprintf(stderr, usage); fprintf(stderr, usag2); exit(2); } strcpy(destination, argv[1]); if (parse_destination_id(destination, &q_n_out, &s_n_out)) { fprintf(stderr, "qmove: illegally formed destination: %s\n", destination); exit(2); } /*perform needed security library initializations (including none)*/ if (CS_client_init() != CS_SUCCESS) { fprintf(stderr, "qmove: unable to initialize security library.\n"); exit(2); } for (optind = 2; optind < argc; optind++) { int connect; int stat=0; int located = FALSE; strcpy(job_id, argv[optind]); if (get_server(job_id, job_id_out, server_out)) { fprintf(stderr, "qmove: illegally formed job identifier: %s\n", job_id); any_failed = 1; continue; } cnt: connect = cnt2server(server_out); if (connect <= 0) { fprintf(stderr, "qmove: cannot connect to server %s (errno=%d)\n", pbs_server, pbs_errno); any_failed = pbs_errno; continue; } stat = pbs_movejob(connect, job_id_out, destination, NULL); if (stat && (pbs_errno != PBSE_UNKJOBID)) { if (stat != PBSE_NEEDQUET) { prt_job_err("qmove", connect, job_id_out); any_failed = pbs_errno; } else { fprintf(stderr, "qmove: Queue type not set for queue \'%s\'\n", destination); } } else if (stat && (pbs_errno == PBSE_UNKJOBID) && !located) { located = TRUE; if (locate_job(job_id_out, server_out, rmt_server)) { pbs_disconnect(connect); strcpy(server_out, rmt_server); goto cnt; } prt_job_err("qmove", connect, job_id_out); any_failed = pbs_errno; } pbs_disconnect(connect); } /*cleanup security library initializations before exiting*/ CS_close_app(); exit(any_failed); }