Example #1
0
/*
 *	 Make sure only one copy is running at a time...
 */
static gboolean
TempProcessTrigger(gpointer ginfo)
{
	struct tempproc_track*	info = ginfo;
	int			pid;

 	/* Make sure only one copy is running at a time. */
	/* This avoids concurrency problems. */
	if (info->isrunning) {
		info->runagain = TRUE;
		return TRUE;
	}
	info->isrunning = TRUE;

	if (info->prefork) {
		if (debug_level > 1) {
			cl_log(LOG_DEBUG
			,	"%s: Calling prefork for temp process %s"
			,	__FUNCTION__, info->procname);
		}
		info->prefork(info->userdata);
	}
	if (ANYDEBUG) {
		cl_log(LOG_DEBUG, "Forking temp process %s", info->procname);
	}
	switch ((pid=fork())) {
		int		rc;
		case -1:	cl_perror("%s: Can't fork temporary child"
				" process [%s]!",	__FUNCTION__
				,	info->procname);
				info->isrunning = FALSE;
				break;

		case 0:		/* Child */
				if ((rc=info->fun(info->userdata)) == HA_OK) {
					exit(0);
				}
				cl_log(LOG_WARNING
				,	"%s: %s returns %d", __FUNCTION__
				,	info->procname, rc);
				exit(1);
				break;
		default:
				/* Fall out */;

	}
	if (pid > 0) {
		NewTrackedProc(pid, 0, (ANYDEBUG? PT_LOGVERBOSE : PT_LOGNORMAL)
		,	ginfo, &TempProcessTrackOps);
		if (info->postfork) {
			if (debug_level > 1) {
				cl_log(LOG_DEBUG
				,	"%s: Calling postfork for temp process %s"
				,	__FUNCTION__, info->procname);
			}
			info->postfork(info->userdata);
		}
	}
	return TRUE;
}
Example #2
0
static int
run_client_as_child(char * execv_argv[])
{
	long	pid;
	int	i;

	if (execv_argv[0] == NULL) {
		cl_log(LOG_ERR, "Null pointer to program name which need to" 
			"be executed.");
		return LSB_EXIT_EINVAL;
	}

	pid = fork();

	if (pid < 0) {
		cl_log(LOG_ERR, "cannot start monitor program %s.", 
			execv_argv[0]);
		return -1;
	} else if (pid > 0) { /* in the parent process */
		NewTrackedProc( pid, 1, PT_LOGVERBOSE
			, execv_argv, &MonitoredProcessTrackOps);
		monitored_PID = pid;
		return pid;
	}
	
 	/* Now in child process */
	execvp(execv_argv[0], execv_argv);
	/* if go here, there must be something wrong */
	cl_log(LOG_ERR, "%s",strerror(errno));
	cl_log(LOG_ERR, "execving monitored program %s failed.", execv_argv[0]);

	i = 0;
	do {
		free(execv_argv[i]);
	} while (execv_argv[++i] != NULL); 

	/* Since parameter error, donnot need to be respawned */
	exit(MAGIC_EXIT_CODE);
}