Example #1
0
int
main(int argc, char **argv)
{
	/*
	 *  This routine sends a Manage request to the batch server specified by
	 * the destination.  The ENABLED queue attribute is set to {True}.  If the
	 * batch request is accepted, the server will accept Queue Job requests for
	 * the specified queue.
	 */

	int dest;		/* Index into the destination array (argv) */
	char *queue;	/* Queue name part of destination */
	char *server;	/* Server name part of destination */

	/*test for real deal or just version and exit*/

	execution_mode(argc, argv);

#ifdef WIN32
	winsock_init();
#endif

	if (argc == 1) {
		fprintf(stderr, "Usage: qenable [queue][@server] ...\n");
		fprintf(stderr, "       qenable --version\n");
		exit(1);
	}

	/*perform needed security library initializations (including none)*/

	if (CS_client_init() != CS_SUCCESS) {
		fprintf(stderr, "qenable: unable to initialize security library.\n");
		exit(1);
	}

	for (dest=1; dest<argc; dest++)
		if (parse_destination_id(argv[dest], &queue, &server) == 0)
			execute(queue, server);
	else {
		fprintf(stderr, "qenable: illegally formed destination: %s\n",
			argv[dest]);
		exitstatus = 1;
	}

	/*cleanup security library initializations before exiting*/
	CS_close_app();

	exit(exitstatus);
}
Example #2
0
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);
}
Example #3
0
/**
 * @brief
 *      This is main function of pbs_upgrade_job process.
 */
int
main(int argc, char *argv[])
{
	DIR *dir;
	struct stat statbuf;
	struct dirent *dirent;
	char taskdir[MAXPATHLEN + 1] = {'\0'};
	char namebuf[MAXPATHLEN + 1] = {'\0'};
	char *jobfile = NULL;
	char *p;
	char *task_start;
	int fd = -1;
	int flags = 0;
	int err = 0;
	int check_flag = 0;
	int i;
	int ret;

	errno = 0;

	/* Print pbs_version and exit if --version specified */
	execution_mode(argc, argv);

	/* Parse the command line parameters */
	while (!err && ((i = getopt(argc, argv, "cf:")) != EOF)) {
		switch (i) {
			case 'c':
				check_flag = 1;
				break;
			case 'f':
				if (jobfile) {
					err = 1;
					break;
				}
				jobfile = optarg;
				break;
			default:
				err = 1;
				break;
		}
	}
	if(!jobfile)
		err = 1;
	if (err) {
		print_usage();
		return 1;
	}

	/* Ensure the tasks directory exists */
	strncpy(namebuf, jobfile, sizeof(namebuf));
	p = strrchr(namebuf, '.');
	if (!p) {
		fprintf(stderr, "Missing job file suffix");
		return 1;
	}
	if (strncmp(p, JOB_FILE_SUFFIX, strlen(JOB_FILE_SUFFIX)) != 0) {
		fprintf(stderr, "Invalid job file suffix");
		return 1;
	}
	strcpy(p, JOB_TASKDIR_SUFFIX);
	p += strlen(JOB_TASKDIR_SUFFIX);
	ret = stat(namebuf, &statbuf);
	if (ret < 0) {
		fprintf(stderr, "Failed to stat task directory %s [%s]\n",
			namebuf, errno ? strerror(errno) : "No error");
		return 1;
	}
	if (!S_ISDIR(statbuf.st_mode)) {
		fprintf(stderr, "Expected directory at %s", namebuf);
		return 1;
	}
	strncpy(taskdir, namebuf, sizeof(taskdir));
	strcat(p, "/");
	task_start = ++p;

	if (check_flag)
		flags = O_BINARY | O_RDONLY;
	else
		flags = O_BINARY | O_RDWR;

	/* Open the job file for reading */
	fd = open(jobfile, flags);
	if (fd < 0) {
		fprintf(stderr, "Failed to open %s [%s]\n", jobfile,
			errno ? strerror(errno) : "No error");
		return 1;
	}

	/* Determine the format of the file */
	ret = check_job_file(fd);
	if (ret < 0) {
		fprintf(stderr, "Unknown format: %s\n", jobfile);
		return 1;
	}
	if (check_flag) {
		printf("%d\n", ret);
		close(fd);
		return 0;
	}
	switch (ret) {
		case 12:
			break;
		case 13:
			return 0;
		default:
			fprintf(stderr, "Unsupported version %d\n", ret);
			return 1;
	}

	/* Upgrade the job file */
	ret = upgrade_job_file(fd);
	if (ret != 0) {
		fprintf(stderr, "Upgrade failed\n");
		return 1;
	}

	/* Close the job file */
	ret = close(fd);
	if (ret < 0) {
		fprintf(stderr, "Failed to close job file [%s]\n",
			errno ? strerror(errno) : "No error");
		return 1;
	}

	/* Upgrade the task files */
	dir = opendir(taskdir);
	if (!dir) {
		fprintf(stderr, "Failed open task directory [%s]\n",
			errno ? strerror(errno) : "No error");
		return 1;
	}
	while (errno = 0, (dirent = readdir(dir)) != NULL) {
		if (errno != 0) {
			fprintf(stderr, "Failed to read directory [%s]\n",
				errno ? strerror(errno) : "No error");
			return 1;
		}
		if (dirent->d_name[0] == '.')
			continue;
		strcpy(task_start, dirent->d_name);
		ret = upgrade_task_file(namebuf);
	}
	closedir(dir);

	return 0;
}
Example #4
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);


}
Example #5
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);
}
Example #6
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);
}
Example #7
0
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);
}
Example #8
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);
}
Example #9
0
/**
 * @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;
}
Example #10
0
SINT main(void)
{
#ifdef NO_RUN_ENTER_STOP_EXIT
	 
	init_OS_flag(); /* this should be called before device init */
	nxt_device_init();
	ecrobot_initDeviceStatus(); // added 10/28/2010 to fix a bug by tchikama
	ecrobot_init_nxtstate();

	if  (execution_mode() == EXECUTED_FROM_FLASH)
	{
		/*
		 * Call buttons_get() because ecrobot_get_button_state() has button bouncer.
		 * The button bouncer requires multiple periodical calls to make it work, but
		 * in this case, only single call (no while loop), so buttons_get is called. 
		 */
		if ((buttons_get() & 0x0F) == (ENTER_PRESSED | STOP_PRESSED))
		{
			/* set flash request and shut down the NXT
	 	 	 * at the next start, NXT BIOS will be executed.
	 	 	 */
			display_clear(0);
   			display_goto_xy(0, 0);
   			display_string("PWR ON: NXT BIOS");
			display_update();
			systick_wait_ms(1000);
   		
			set_flash_request();
			display_clear(1);
			systick_wait_ms(10);
			nxt_lcd_power_down(); /* reset LCD hardware */
			systick_wait_ms(10);
			while(1)
			{
				nxt_avr_power_down();
			}
		}
	}

	/* device init should be called prior to running the application */
	ecrobot_device_initialize();
	ecrobot_setDeviceInitialized();
	
	nxt_motor_set_count(NXT_PORT_A, 0);
	nxt_motor_set_count(NXT_PORT_B, 0);
	nxt_motor_set_count(NXT_PORT_C, 0);
	cpp_constructor();
	display_clear(1);
	systick_wait_ms(10);

#ifdef NXT_JSP
	interrupts_get_and_disable();
#else
	disable_int(); /* set_OS_flag and Start OS have to be atomic */
#endif
	set_OS_flag(); /* this shoud be called before starting OS */
#ifdef NXT_JSP
	lejos_osek_run(); /* start TOPPERS JSP */
#else
	StartOS(1);    /* start TOPPERS OSEK */
#endif

	/* never reached here */


#else
	/* 
	 * Default start up sequence
	 */
	U32 st;
	U32 last_act_time = 0;
	U32 flash_req_cnt = 0;

	init_OS_flag(); /* this should be called before device init */
	nxt_device_init();
	ecrobot_initDeviceStatus(); // added 10/28/2010 to fix a bug by tchikama
	ecrobot_init_nxtstate();

	show_splash_screen();
	show_main_screen();
	display_status_bar(1); /* clear status bar */
	add_status_info(execution_mode());
	display_status_bar(0); /* update status bar */
	while(1)
	{
		/* device init should be called prior to running the application */
		ecrobot_device_initialize();
		ecrobot_setDeviceInitialized();
		/* check the buttons every 10msec */
		st = systick_get_ms();
		if (st >= last_act_time + 10) 
		{
			last_act_time = st;
			ecrobot_poll_nxtstate();
			display_status_bar(0);

			/* 
			 * executed in FLASH: setup for the application flash
			 * executed in SRAM:  no effect
			 */
			if ((ecrobot_get_button_state() == (ENTER_PRESSED | STOP_PRESSED)) &&
			    (execution_mode() == EXECUTED_FROM_FLASH))
			{
				flash_req_cnt++;
				/* keep pusing ENTER + STOP buttons more than 1000msec */
				if (flash_req_cnt >= 100)
				{
					/* set flash request and shut down the NXT
				 	 * at the next start, NXT BIOS will be executed.
				 	 */
					ecrobot_device_terminate();
					set_flash_request();
					display_clear(1);
					systick_wait_ms(10);
					nxt_lcd_power_down(); /* reset LCD hardware */
					systick_wait_ms(10);
					while(1)
					{
						nxt_avr_power_down();
					}
				}
			}
			else
			{
				flash_req_cnt = 0;
				if ((ecrobot_get_button_state() == EXIT_PRESSED) || (systick_get_ms() > SLEEP_TIME))
				{
					/* shut down the NXT */
					ecrobot_device_terminate();
					display_clear(1);
					systick_wait_ms(10);
					nxt_lcd_power_down(); /* reset LCD hardware */
					systick_wait_ms(10);
					while(1)
					{
						nxt_avr_power_down();
					}
				}
				else if (ecrobot_get_button_state() == RUN_PRESSED)
				{
					nxt_motor_set_count(NXT_PORT_A, 0);
					nxt_motor_set_count(NXT_PORT_B, 0);
					nxt_motor_set_count(NXT_PORT_C, 0);
					cpp_constructor();
					display_clear(1);
					systick_wait_ms(10);
#ifdef NXT_JSP
					interrupts_get_and_disable();
#else
					disable_int(); /* set_OS_flag and Start OS have to be atomic */
#endif
					set_OS_flag(); /* this shoud be called before starting OS */
#ifdef NXT_JSP
					lejos_osek_run(); /* start TOPPERS JSP */
#else
					StartOS(1);    /* start TOPPERS OSEK */
#endif
					/* never reached here */
				}
			}
		}
    }
#endif    

    return 0;
}
Example #11
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);
}
Example #12
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);
}
Example #13
0
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);
}
Example #14
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;
}
Example #15
0
/**
 * @brief
 *		main - the initialization and main loop of pbs_comm
 *
 * @param[in]	argc	- argument count.
 * @param[in]	argv	- argument values.
 *
 * @return	int
 * @retval	0	- success
 */
main(int argc, char *argv[])
{
	SC_HANDLE schManager;
	SC_HANDLE schSelf;
	int reg = 0;
	int unreg = 0;
	TCHAR	  szFileName[MAX_PATH];

	/*the real deal or just pbs_version and exit*/

	execution_mode(argc, argv);

	if (argc > 1) {
		if (strcmp(argv[1], "-R") == 0)
			reg = 1;
		else if (strcmp(argv[1], "-U") == 0)
			unreg = 1;
		else if (strcmp(argv[1], "-N") == 0)
			stalone = 1;
	}

	if (reg || unreg) {
		schManager = OpenSCManager(0, 0, SC_MANAGER_ALL_ACCESS);
		if (schManager == 0) {
			ErrorMessage("OpenSCManager");
		}

		if (reg) {
			GetModuleFileName(0, szFileName, sizeof(szFileName)/sizeof(*szFileName));
			printf("Installing service %s\n", g_PbsCommName);
			schSelf =
				CreateService(schManager, g_PbsCommName, __TEXT("PBS COMM"),
				SERVICE_ALL_ACCESS,
				SERVICE_WIN32_OWN_PROCESS,
				SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
				replace_space(szFileName, ""), 0, 0, 0, 0, 0);

			if (schSelf) {
				printf("Service %s installed succesfully!\n", g_PbsCommName);
			} else {
				ErrorMessage("CreateService");
			}

			if (schSelf != 0)
				CloseServiceHandle(schSelf);
		} else if (unreg) {
			schSelf = OpenService(schManager, g_PbsCommName, DELETE);

			if (schSelf) {
				if (DeleteService(schSelf)) {
					printf("Service %s uninstalled successfully!\n", g_PbsCommName);
				} else {
					ErrorMessage("DeleteService");
				}
			} else {
				ErrorMessage("OpenService failed");
			}
			if (schSelf != 0)
				CloseServiceHandle(schSelf);
		}

		if (schManager != 0)
			CloseServiceHandle(schManager);
	} else if (stalone) {
		struct arg_param *pap;
		int	i, j;

		pap = create_arg_param();
		if (pap == NULL)
			ErrorMessage("create_arg_param");

		pap->argc = argc-1;	/* don't pass the second argument */
		for (i=j=0; i < argc; i++) {
			if (i == 1)
				continue;
			pap->argv[j] = strdup(argv[i]);
			j++;
		}
		main_thread((void *)pap);

		free_arg_param(pap);
	} else {	/* running as a service */
		SERVICE_TABLE_ENTRY rgste[] = { {(TCHAR*)g_PbsCommName, PbsCommMain },
			{ 0, 0 } };

		if (getenv("PBS_CONF_FILE") == NULL) {
			char conf_path[80];
			char *p;
			char psave;
			struct stat sbuf;

			if (p = strstr(argv[0], "exec")) {
				psave = *p;
				*p = '\0';
				_snprintf(conf_path, 79, "%spbs.conf", argv[0]);
				*p = psave;
				if (stat(conf_path, &sbuf) == 0) {
					setenv("PBS_CONF_FILE", conf_path, 1);
				}
			}
		}
		if (!StartServiceCtrlDispatcher(rgste)) {
			ErrorMessage("StartServiceCntrlDispatcher");
		}
	}
	return (0);
}
Example #16
0
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*/

	execution_mode(argc, argv);

#ifdef WIN32
	winsock_init();
#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, 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);
}
Example #17
0
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*/

	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 (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);
}
Example #18
0
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);
}