Esempio n. 1
0
/*
 * main-Funktion. Startet die 4 Prozesse, registriert einen signal-handler für
 * SIGTERM (dieser sendet SIGINT an die 4 Prozesse) und wartet schließlich auf
 * das Ende der Prozesse.
 *
 */
int main() {

    /*
     * Öffnen der Pipes.
     */
    if ((pipe(queue[D_CONV_TO_LOG])!=0) || (pipe(queue[D_CONV_TO_STAT])!=0) ||
            (pipe(queue[D_STAT_TO_MON])!=0)) {
        perror("main.c pipe()");
        exit(EXIT_FAILURE);
    }


    pconv = fork_child(conv, conv_cleanup);
    plog = fork_child(log, log_cleanup);
    pstatistic = fork_child(statistic, statistic_cleanup);
    pmonitor = fork_child(monitor, monitor_cleanup);

    signal(SIGTERM, sigterm);

    int status;
    waitpid(pconv, &status, 0);
    waitpid(plog, &status, 0);
    waitpid(pstatistic, &status, 0);
    waitpid(pmonitor, &status, 0);

    return 0;
}
Esempio n. 2
0
int
main(int argc, char **argv)
{ 
  char *command_name;
  rlwrap_command_line = unsplit_with(argc, argv, " ");     
  init_completer();

  /* Harvest options and leave optind pointing to first non-option argument: */
  command_name = read_options_and_command_name(argc, argv);

  /* by now, optind points to slave <command>, and &argv[optind] is <command>'s argv. Remember slave command line: */
  command_line = unsplit_with(argc - optind, argv, " ");

  /* if stdin is not a tty, just execute <command>: */ 
  if (!isatty(STDIN_FILENO) && execvp(argv[optind], &argv[optind]) < 0)
    myerror(FATAL|USE_ERRNO, "Cannot execute %s", argv[optind]);	
  init_rlwrap(rlwrap_command_line);
  install_signal_handlers();	
  block_all_signals();
  fork_child(command_name, argv); /* this will unblock most signals most of the time */
  if (filter_command)
    spawn_filter(filter_command);
  
  main_loop();
  return 42;			/* The Answer, but, sadly, we'll never get there.... */
}
Esempio n. 3
0
File: shell.c Progetto: lingmar/os16
int execute_command_line(char *line, char *argv[]) {

    int  num_of_commands = 0;
    enum cmd_pos pos     = unknown;
    int  left_pipe[2]    = {-1, -1};
    int  right_pipe[2]   = {-1, -1};

    do {

        // Repeat this loop for each of the commands separated by '|' in
        // the command pipeline.

        // Use the next_command() parser to get the position and argument
        // vector of the next command in the command pipeline.
        pos = next_command(line, argv);

        create_pipe(pos, right_pipe);


        fork_child(pos, left_pipe, right_pipe, argv);

        // Only the parent will return here!!!

        num_of_commands++;

        // The previous rigth pipe now becomes the current left pipe.
        shift_pipes(left_pipe, right_pipe);


    } while (pos != single && pos != last);

    return num_of_commands;
}
Esempio n. 4
0
int
main (void)
{
    pid_t pid;

    save_parent = getpid ();

    /* Don't run forever.  */
    alarm (180);

    /* The parent and child should basically run forever without
       tripping on any debug event.  We want to check that GDB updates
       the parent and child running states correctly right after the
       fork.  */
    pid = fork ();
    if (pid > 0)
        return fork_parent ();
    else if (pid == 0)
        return fork_child ();
    else
    {
        perror ("fork");
        return 1;
    }
}
Esempio n. 5
0
static int mavis_init_in(mavis_ctx * mcx)
{
    int i;

    DebugIn(DEBUG_MAVIS);

    mcx->lastdump = mcx->startup_time = time(NULL);

    if (!mcx->path)
	logmsg("Warning: %s: module lacks path definition", MAVIS_name);
    else if (!mcx->argv[0]) {
	mcx->argv[0] = Xstrdup(basename(mcx->path));
	mcx->argv[1] = NULL;
    }

    if (mcx->child_min > mcx->child_max)
	mcx->child_min = mcx->child_max;

    if (!mcx->io_context_parent)
	mcx->io_context_local = mcx->io = io_init();
    mcx->cx = Xcalloc(mcx->child_max, sizeof(struct context *));
    mcx->cx_stat = Xcalloc(mcx->child_max, sizeof(struct context_stat));
    for (i = 0; i < mcx->child_min; i++)
	fork_child(mcx, i);

    mcx->backlog_serial = RB_tree_new(compare_serial, NULL);
    mcx->backlog_app_ctx = RB_tree_new(compare_app_ctx, NULL);
    mcx->backlog_fifo = RB_tree_new(compare_fifo, free_payload);
    mcx->outgoing = RB_tree_new(compare_app_ctx, free_payload);
    mcx->junkcontexts = RB_tree_new(compare_ctx, free_context);

    DebugOut(DEBUG_MAVIS);
    return MAVIS_INIT_OK;
}
Esempio n. 6
0
int
main (void)
{
  pid_t childs[10];
  int i;
  int status;
  int num_exited = 0;

  for (i = 0; i < 10; i++)
  {
    pid_t pid;
    
    pid = fork ();
    
    if (pid > 0)
      {
	/* Parent */
	childs[i] = pid;
      }
    else if (pid == 0)
      {
	/* Child */
	fork_child ();
	return 0;
      }
    else
      {
	perror("fork");
	return 1;
      }
  }
  
  while (num_exited != 10)
    {
      pid_t pid = wait(&status);

      if (pid == -1)
	{
	  perror("wait");
	  printf("wait error\n");
	  return 1;
	}

      if (WIFEXITED(status))
        {
	  printf("Child %d has exited\n", pid);
	  num_exited++;
	}
      else
	{
	  printf("Hmm, unexpected wait status 0x%x from child %d\n", status, pid);
	}
    }

  printf("Test ended successfully\n");

  return 0;
}
Esempio n. 7
0
int
main (void)
{
  pid_t childs[NFORKS];
  int i;
  int status;
  int num_exited = 0;

  /* Don't run forever if the wait loop below gets stuck.  */
  alarm (180);

  for (i = 0; i < NFORKS; i++)
    {
      pid_t pid;

      pid = fork ();

      if (pid > 0)
	{
	  /* Parent.  */
	  childs[i] = pid;
	}
      else if (pid == 0)
	{
	  /* Child.  */
	  fork_child ();
	  return 0;
	}
      else
	{
	  perror ("fork");
	  return 1;
	}
    }

  while (num_exited != NFORKS)
    {
      pid_t pid = wait (&status);

      if (pid == -1)
	{
	  perror ("wait");
	  return 1;
	}

      if (WIFEXITED (status))
	{
	  num_exited++;
	}
      else
	{
	  printf ("Hmm, unexpected wait status 0x%x from child %d\n", status,
		  pid);
	}
    }

  return 0;
}
Esempio n. 8
0
static void child_died(struct context *ctx, int cur __attribute__ ((unused)))
{
    if (ctx->ac) {		// might be called multiple times else
	int i = ctx->index;
	DebugIn(DEBUG_PROC);

	if (ctx->mcx->cx[i]->counter < 2) {
	    logmsg("%s: %lu: terminated before finishing first request", ctx->mcx->argv[0], (u_long) ctx->pid);
	    ctx->mcx->reaphist[ctx->mcx->reapcur] = io_now.tv_sec + REAPINT;
	    ctx->mcx->reapcur++;
	    ctx->mcx->reapcur %= REAPMAX;
	    ctx->mcx->usage--;
	} else
	    logmsg("%s: %lu: terminated after processing %llu requests", ctx->mcx->argv[0], (u_long) ctx->pid, ctx->mcx->cx[i]->counter);

	ctx->mcx->cx[i]->counter = 0;

	io_child_set(ctx->pid, NULL, NULL);

	if (ctx->fd_in > -1) {
	    io_close(ctx->mcx->io, ctx->fd_in);
	    ctx->fd_in = -1;
	}
	if (ctx->fd_out > -1) {
	    io_close(ctx->mcx->io, ctx->fd_out);
	    ctx->fd_out = -1;
	}

	ctx->index = -1;

	RB_insert(ctx->mcx->junkcontexts, ctx);

#ifdef DEBUG_RB
	fprintf(stderr, "EXT insert junkcontexts %p\n", ctx);
#endif

	ctx->mcx->cx[i] = NULL;
	ctx->mcx->child_cur--;

	fork_child(ctx->mcx, i);

	if (ctx->mcx->cx[i]) {
	    ctx->mcx->cx[i]->ac = ctx->ac;
	    ctx->ac = NULL;

	    ctx->mcx->cx_stat[i].counter++;
	    ctx->mcx->cx_stat[i].counter_p++;
	    start_query(ctx->mcx->cx[i]);
	}

	DebugOut(DEBUG_PROC);
    }
}
Esempio n. 9
0
int main(int argc, char *argv[]) {
    int num_procs;
    int c, i, j, msqid;
    int proc_done=0;
    int child_msqids[20];

    if ((c=getopt(argc, argv, "n:")) != -1) {
        if (c == 'n') { 
	    num_procs = atoi(optarg);
	    if (num_procs > 20) {
		//printf("Number of processes should not exceed 20. Setting to 20.\n");
		num_procs = 20;
	    } 
	}
	else num_procs = 1;
	}
    else num_procs = 1;
    

    //Create parser-to-sorters msg queue
    msqid = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666);

    //Create sorters-to-combiner msg queues and fork the sorter children
    for (i=1;i<num_procs+1;i++) { 
	child_msqids[i] = msgget(IPC_PRIVATE, IPC_CREAT | IPC_EXCL | 0666);

        fork_child(msqid, child_msqids[i], i);
    }

    //Parse while children running
    parser(msqid, num_procs, child_msqids);

    //Combine while children running
    combiner(num_procs, child_msqids);

    //Reap the children
    for (j=1;j<num_procs+1;j++){ 
	if (wait(NULL) != -1) proc_done++;
    }

    // printf("Number of processes done: %d\n", proc_done);
 
    //Combine after the children are done -- nope doesn't work
    //  combiner(num_procs, child_msqids);
 
    //Destroy the parser-to-sorters message queue
     if (proc_done == num_procs){
	 msgctl(msqid, IPC_RMID, NULL);
     }

     exit(EXIT_SUCCESS);
     return 0;
}
Esempio n. 10
0
/*
 * Start up a window to monitor the trace file.
 *
 * @param[in] path	Trace file path. On Unix, this can be NULL to indicate
 * 			that the trace is just being piped.
 * @param[in] pipefd	Array of pipe file descriptors.
 */
static void
start_trace_window(const char *path, int pipefd[])
{
    switch (tracewindow_pid = fork_child()) {
    case 0:	/* child process */
	(void) execlp("xterm", "xterm", "-title", path? path: "trace",
		"-sb", "-e", "/bin/sh", "-c", xs_buffer("cat <&%d", pipefd[0]),
		NULL);
	(void) perror("exec(xterm) failed");
	_exit(1);
	break;
    default:	/* parent */
	(void) close(pipefd[0]);
	++children;
	break;
    case -1:	/* error */
	popup_an_errno(errno, "fork() failed");
	break;
    }
}
Esempio n. 11
0
int					go_fork(int cs)
{
	pid_t					parent;
	t_uenv					*user;

	user = NULL;
	if ((parent = fork()) == -1)
	{
		return (0);
	}
	if (!parent)
	{
		printf("[\x1B[32mO\x1B[0m] New user [%d] connected\n", cs);
		env_push_back(&user, cs);
		fork_child(user);
		printf("[\x1B[31mX\x1B[0m] User [%d] disconnected\n", cs);
		close(cs);
		return (0);
	}
	return (1);
}
Esempio n. 12
0
File: main.c Progetto: albfan/rlwrap
int
main(int argc, char **argv)
{ 
  char *command_name;
  
  init_completer();
  command_name = read_options_and_command_name(argc, argv);
  
  /* by now, optind points to <command>, and &argv[optind] is <command>'s argv */
  if (!isatty(STDIN_FILENO) && execvp(argv[optind], &argv[optind]) < 0)
    /* if stdin is not a tty, just execute <command> */ 
    myerror("Cannot execute %s", argv[optind]);	
  init_rlwrap();
  install_signal_handlers();	
  block_all_signals();
  fork_child(command_name, argv);
  if (filter_command)
    spawn_filter(filter_command);
  
  main_loop();
  return 42;			/* not reached, but some compilers are unhappy without this ... */
}
Esempio n. 13
0
int main(int argc, char **argv)
{
	int						port;
	int						sock;
	int						cs;
	unsigned int			cslen;
	struct sockaddr_in		csin;
	t_uenv					*user;
	pid_t					parent;
	
	user = NULL;
	if (argc != 2)
		usage(argv[0]);
	port = ft_atoi(argv[1]);
	sock = create_server(port);
	while ((cs = accept(sock, (struct sockaddr*)&csin, &cslen)))
	{
		if ((parent = fork()) == -1)
		{
			return (0);
		}
		if (!parent)
		{
			printf("[\x1B[32mO\x1B[0m] New user [%d] connected\n", cs);
			env_push_back(&user, cs);
			fork_child(user);
			printf("[\x1B[31mX\x1B[0m] User [%d] disconnected\n", cs);
			close(cs);
			return(0);
		}
	}
	if (cs == -1)
		printf("Socket error\n");
	close (cs);
	close (sock);
	return 0;
}
Esempio n. 14
0
int main(int argc, char ** argv)
{
	int i, ret, err = 0, status;
	pid_t pid;

	test_init(argc, argv);

	for (i = 0; i < NUM_CASES; i++)
		if (fork_child(i))
			break;

	if (i != NUM_CASES) {
		int j;
		for (j = 0; j < i; j++)
			kill(testcases[j].pid, SIGTERM);
		return 1;
	}

	test_daemon();

	test_waitsig();

	for (i = 0; i < NUM_CASES; i++) {
		pid_t pid = testcases[i].pid;
		pid_t sid = getsid(pid);

		if (sid != testcases[i].sid) {
			fail("The process %d (%x) has SID=%d (expected %d)",
				pid, testcases[i].flags, sid, testcases[i].sid);
			err++;
		}

		ret = kill(pid, SIGKILL);
		if (ret == -1) {
			pr_perror("kill failed");
			err++;
		}
		waitpid(pid, NULL, 0);

		if (testcases[i].flags & CHANGESID) {
			pid = wait(&status);
			if (pid == -1) {
				pr_perror("wait() failed");
				err++;
			}
			if (!WIFEXITED(status) || WEXITSTATUS(status)) {
				fail("The process with pid %d returns %d\n", pid, status);
				err++;
			}
		}
	}

	pid = wait(&status);
	if (pid != -1 || errno != ECHILD) {
		pr_perror("%d isn't waited", pid);
		err++;
	}

	if (!err)
		pass();

	return err > 0;
}
Esempio n. 15
0
int
main( int argc, char **argv )
{
	int		i, j;
	char		*uri = NULL;
	char		*host = "localhost";
	char		*port = NULL;
	char		*manager = NULL;
	char		*passwd = NULL;
	char		*dirname = NULL;
	char		*progdir = NULL;
	int		loops = LOOPS;
	char		*outerloops = OUTERLOOPS;
	char		*retries = RETRIES;
	char		*delay = "0";
	DIR		*datadir;
	struct dirent	*file;
	int		friendly = 0;
	int		chaserefs = 0;
	int		noattrs = 0;
	int		nobind = 0;
	int		noinit = 1;
	char		*ignore = NULL;
	/* search */
	char		*sfile = NULL;
	char		*sreqs[MAXREQS];
	char		*sattrs[MAXREQS];
	char		*sbase[MAXREQS];
	LDAPURLDesc	*slud[MAXREQS];
	int		snum = 0;
	char		*sargs[MAXARGS];
	int		sanum;
	int		sextra_args = 0;
	char		scmd[MAXPATHLEN];
	int		swamp = 0;
	char		swampopt[sizeof("-SSS")];
	/* static so that its address can be used in initializer below. */
	static char	sloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)];
	/* read */
	char		*rfile = NULL;
	char		*rreqs[MAXREQS];
	int		rnum = 0;
	char		*rargs[MAXARGS];
	char		*rflts[MAXREQS];
	int		ranum;
	int		rextra_args = 0;
	char		rcmd[MAXPATHLEN];
	static char	rloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)];
	/* addel */
	char		*afiles[MAXREQS];
	int		anum = 0;
	char		*aargs[MAXARGS];
	int		aanum;
	char		acmd[MAXPATHLEN];
	static char	aloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)];
	/* modrdn */
	char		*nfile = NULL;
	char		*nreqs[MAXREQS];
	int		nnum = 0;
	char		*nargs[MAXARGS];
	int		nanum;
	char		ncmd[MAXPATHLEN];
	static char	nloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)];
	/* modify */
	char		*mfile = NULL;
	char		*mreqs[MAXREQS];
	char		*mdn[MAXREQS];
	int		mnum = 0;
	char		*margs[MAXARGS];
	int		manum;
	char		mcmd[MAXPATHLEN];
	static char	mloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)];
	/* bind */
	char		*bfile = NULL;
	char		*breqs[MAXREQS];
	char		*bcreds[MAXREQS];
	char		*battrs[MAXREQS];
	int		bnum = 0;
	char		*bargs[MAXARGS];
	int		banum;
	char		bcmd[MAXPATHLEN];
	static char	bloops[LDAP_PVT_INTTYPE_CHARS(unsigned long)];
	char		**bargs_extra = NULL;

	char		*friendlyOpt = NULL;
	int		pw_ask = 0;
	char		*pw_file = NULL;

	/* extra action to do after bind... */
	typedef struct extra_t {
		char		*action;
		struct extra_t	*next;
	}		extra_t;

	extra_t		*extra = NULL;
	int		nextra = 0;

	tester_init( "slapd-tester", TESTER_TESTER );

	sloops[0] = '\0';
	rloops[0] = '\0';
	aloops[0] = '\0';
	nloops[0] = '\0';
	mloops[0] = '\0';
	bloops[0] = '\0';

	while ( ( i = getopt( argc, argv, "AB:CD:d:FH:h:Ii:j:L:l:NP:p:r:St:Ww:y:" ) ) != EOF )
	{
		switch ( i ) {
		case 'A':
			noattrs++;
			break;

		case 'B': {
			char	**p,
				**b = ldap_str2charray( optarg, "," );
			extra_t	**epp;

			for ( epp = &extra; *epp; epp = &(*epp)->next )
				;

			for ( p = b; p[0]; p++ ) {
				*epp = calloc( 1, sizeof( extra_t ) );
				(*epp)->action = p[0];
				epp = &(*epp)->next;
				nextra++;
			}

			ldap_memfree( b );
			} break;

		case 'C':
			chaserefs++;
			break;

		case 'D':		/* slapd manager */
			manager = ArgDup( optarg );
			break;

		case 'd':		/* data directory */
			dirname = strdup( optarg );
			break;

		case 'F':
			friendly++;
			break;

		case 'H':		/* slapd uri */
			uri = strdup( optarg );
			break;

		case 'h':		/* slapd host */
			host = strdup( optarg );
			break;

		case 'I':
			noinit = 0;
			break;

		case 'i':
			ignore = optarg;
			break;

		case 'j':		/* the number of parallel clients */
			if ( lutil_atoi( &maxkids, optarg ) != 0 ) {
				usage( argv[0], 'j' );
			}
			break;

		case 'l':		/* the number of loops per client */
			if ( !isdigit( (unsigned char) optarg[0] ) ) {
				char	**p,
					**l = ldap_str2charray( optarg, "," );

				for ( p = l; p[0]; p++) {
					struct {
						struct berval	type;
						char		*buf;
					} types[] = {
						{ BER_BVC( "add=" ),	aloops },
						{ BER_BVC( "bind=" ),	bloops },
						{ BER_BVC( "modify=" ),	mloops },
						{ BER_BVC( "modrdn=" ),	nloops },
						{ BER_BVC( "read=" ),	rloops },
						{ BER_BVC( "search=" ),	sloops },
						{ BER_BVNULL,		NULL }
					};
					int	c, n;

					for ( c = 0; types[c].type.bv_val; c++ ) {
						if ( strncasecmp( p[0], types[c].type.bv_val, types[c].type.bv_len ) == 0 ) {
							break;
						}
					}

					if ( types[c].type.bv_val == NULL ) {
						usage( argv[0], 'l' );
					}

					if ( lutil_atoi( &n, &p[0][types[c].type.bv_len] ) != 0 ) {
						usage( argv[0], 'l' );
					}

					snprintf( types[c].buf, sizeof( aloops ), "%d", n );
				}

				ldap_charray_free( l );

			} else if ( lutil_atoi( &loops, optarg ) != 0 ) {
				usage( argv[0], 'l' );
			}
			break;

		case 'L':		/* the number of outerloops per client */
			outerloops = strdup( optarg );
			break;

		case 'N':
			nobind++;
			break;

		case 'P':		/* prog directory */
			progdir = strdup( optarg );
			break;

		case 'p':		/* the servers port number */
			port = strdup( optarg );
			break;

		case 'r':		/* the number of retries in case of error */
			retries = strdup( optarg );
			break;

		case 'S':
			swamp++;
			break;

		case 't':		/* the delay in seconds between each retry */
			delay = strdup( optarg );
			break;

		case 'w':		/* the managers passwd */
			passwd = ArgDup( optarg );
			memset( optarg, '*', strlen( optarg ) );
			break;

		case 'W':
			pw_ask++;
			break;

		case 'y':
			pw_file = optarg;
			break;

		default:
			usage( argv[0], '\0' );
			break;
		}
	}

	if (( dirname == NULL ) || ( port == NULL && uri == NULL ) ||
			( manager == NULL ) || ( passwd == NULL ) || ( progdir == NULL ))
	{
		usage( argv[0], '\0' );
	}

#ifdef HAVE_WINSOCK
	children = malloc( maxkids * sizeof(HANDLE) );
#endif
	/* get the file list */
	if ( ( datadir = opendir( dirname )) == NULL ) {
		fprintf( stderr, "%s: couldn't open data directory \"%s\".\n",
					argv[0], dirname );
		exit( EXIT_FAILURE );
	}

	/*  look for search, read, modrdn, and add/delete files */
	for ( file = readdir( datadir ); file; file = readdir( datadir )) {

		if ( !strcasecmp( file->d_name, TSEARCHFILE )) {
			sfile = get_file_name( dirname, file->d_name );
			continue;
		} else if ( !strcasecmp( file->d_name, TREADFILE )) {
			rfile = get_file_name( dirname, file->d_name );
			continue;
		} else if ( !strcasecmp( file->d_name, TMODRDNFILE )) {
			nfile = get_file_name( dirname, file->d_name );
			continue;
		} else if ( !strcasecmp( file->d_name, TMODIFYFILE )) {
			mfile = get_file_name( dirname, file->d_name );
			continue;
		} else if ( !strncasecmp( file->d_name, TADDFILE, strlen( TADDFILE ))
			&& ( anum < MAXREQS )) {
			afiles[anum++] = get_file_name( dirname, file->d_name );
			continue;
		} else if ( !strcasecmp( file->d_name, TBINDFILE )) {
			bfile = get_file_name( dirname, file->d_name );
			continue;
		}
	}

	closedir( datadir );

	if ( pw_ask ) {
		passwd = getpassphrase( _("Enter LDAP Password: "******"no data files found.\n" );
		exit( EXIT_FAILURE );
	}

	/* look for search requests */
	if ( sfile ) {
		snum = get_search_filters( sfile, sreqs, sattrs, sbase, slud );
		if ( snum < 0 ) {
			fprintf( stderr,
				"unable to parse file \"%s\" line %d\n",
				sfile, -2*(snum + 1));
			exit( EXIT_FAILURE );
		}
	}

	/* look for read requests */
	if ( rfile ) {
		rnum = get_read_entries( rfile, rreqs, rflts );
		if ( rnum < 0 ) {
			fprintf( stderr,
				"unable to parse file \"%s\" line %d\n",
				rfile, -2*(rnum + 1) );
			exit( EXIT_FAILURE );
		}
	}

	/* look for modrdn requests */
	if ( nfile ) {
		nnum = get_read_entries( nfile, nreqs, NULL );
		if ( nnum < 0 ) {
			fprintf( stderr,
				"unable to parse file \"%s\" line %d\n",
				nfile, -2*(nnum + 1) );
			exit( EXIT_FAILURE );
		}
	}

	/* look for modify requests */
	if ( mfile ) {
		mnum = get_search_filters( mfile, mreqs, NULL, mdn, NULL );
		if ( mnum < 0 ) {
			fprintf( stderr,
				"unable to parse file \"%s\" line %d\n",
				mfile, -2*(mnum + 1) );
			exit( EXIT_FAILURE );
		}
	}

	/* look for bind requests */
	if ( bfile ) {
		bnum = get_search_filters( bfile, bcreds, battrs, breqs, NULL );
		if ( bnum < 0 ) {
			fprintf( stderr,
				"unable to parse file \"%s\" line %d\n",
				bfile, -2*(bnum + 1) );
			exit( EXIT_FAILURE );
		}
	}

	/* setup friendly option */
	switch ( friendly ) {
	case 0:
		break;

	case 1:
		friendlyOpt = "-F";
		break;

	default:
		/* NOTE: right now we don't need it more than twice */
	case 2:
		friendlyOpt = "-FF";
		break;
	}

	/* setup swamp option */
	if ( swamp ) {
		swampopt[0] = '-';
		if ( swamp > 3 ) swamp = 3;
		swampopt[swamp + 1] = '\0';
		for ( ; swamp-- > 0; ) swampopt[swamp + 1] = 'S';
	}

	/* setup loop options */
	if ( sloops[0] == '\0' ) snprintf( sloops, sizeof( sloops ), "%d", 10 * loops );
	if ( rloops[0] == '\0' ) snprintf( rloops, sizeof( rloops ), "%d", 20 * loops );
	if ( aloops[0] == '\0' ) snprintf( aloops, sizeof( aloops ), "%d", loops );
	if ( nloops[0] == '\0' ) snprintf( nloops, sizeof( nloops ), "%d", loops );
	if ( mloops[0] == '\0' ) snprintf( mloops, sizeof( mloops ), "%d", loops );
	if ( bloops[0] == '\0' ) snprintf( bloops, sizeof( bloops ), "%d", 20 * loops );

	/*
	 * generate the search clients
	 */

	sanum = 0;
	snprintf( scmd, sizeof scmd, "%s" LDAP_DIRSEP SEARCHCMD,
		progdir );
	sargs[sanum++] = scmd;
	if ( uri ) {
		sargs[sanum++] = "-H";
		sargs[sanum++] = uri;
	} else {
		sargs[sanum++] = "-h";
		sargs[sanum++] = host;
		sargs[sanum++] = "-p";
		sargs[sanum++] = port;
	}
	sargs[sanum++] = "-D";
	sargs[sanum++] = manager;
	sargs[sanum++] = "-w";
	sargs[sanum++] = passwd;
	sargs[sanum++] = "-l";
	sargs[sanum++] = sloops;
	sargs[sanum++] = "-L";
	sargs[sanum++] = outerloops;
	sargs[sanum++] = "-r";
	sargs[sanum++] = retries;
	sargs[sanum++] = "-t";
	sargs[sanum++] = delay;
	if ( friendly ) {
		sargs[sanum++] = friendlyOpt;
	}
	if ( chaserefs ) {
		sargs[sanum++] = "-C";
	}
	if ( noattrs ) {
		sargs[sanum++] = "-A";
	}
	if ( nobind ) {
		sargs[sanum++] = "-N";
	}
	if ( ignore ) {
		sargs[sanum++] = "-i";
		sargs[sanum++] = ignore;
	}
	if ( swamp ) {
		sargs[sanum++] = swampopt;
	}
	sargs[sanum++] = "-b";
	sargs[sanum++] = NULL;		/* will hold the search base */
	sargs[sanum++] = "-s";
	sargs[sanum++] = NULL;		/* will hold the search scope */
	sargs[sanum++] = "-f";
	sargs[sanum++] = NULL;		/* will hold the search request */

	sargs[sanum++] = NULL;
	sargs[sanum++] = NULL;		/* might hold the "attr" request */
	sextra_args += 2;

	sargs[sanum] = NULL;

	/*
	 * generate the read clients
	 */

	ranum = 0;
	snprintf( rcmd, sizeof rcmd, "%s" LDAP_DIRSEP READCMD,
		progdir );
	rargs[ranum++] = rcmd;
	if ( uri ) {
		rargs[ranum++] = "-H";
		rargs[ranum++] = uri;
	} else {
		rargs[ranum++] = "-h";
		rargs[ranum++] = host;
		rargs[ranum++] = "-p";
		rargs[ranum++] = port;
	}
	rargs[ranum++] = "-D";
	rargs[ranum++] = manager;
	rargs[ranum++] = "-w";
	rargs[ranum++] = passwd;
	rargs[ranum++] = "-l";
	rargs[ranum++] = rloops;
	rargs[ranum++] = "-L";
	rargs[ranum++] = outerloops;
	rargs[ranum++] = "-r";
	rargs[ranum++] = retries;
	rargs[ranum++] = "-t";
	rargs[ranum++] = delay;
	if ( friendly ) {
		rargs[ranum++] = friendlyOpt;
	}
	if ( chaserefs ) {
		rargs[ranum++] = "-C";
	}
	if ( noattrs ) {
		rargs[ranum++] = "-A";
	}
	if ( ignore ) {
		rargs[ranum++] = "-i";
		rargs[ranum++] = ignore;
	}
	if ( swamp ) {
		rargs[ranum++] = swampopt;
	}
	rargs[ranum++] = "-e";
	rargs[ranum++] = NULL;		/* will hold the read entry */

	rargs[ranum++] = NULL;
	rargs[ranum++] = NULL;		/* might hold the filter arg */
	rextra_args += 2;

	rargs[ranum] = NULL;

	/*
	 * generate the modrdn clients
	 */

	nanum = 0;
	snprintf( ncmd, sizeof ncmd, "%s" LDAP_DIRSEP MODRDNCMD,
		progdir );
	nargs[nanum++] = ncmd;
	if ( uri ) {
		nargs[nanum++] = "-H";
		nargs[nanum++] = uri;
	} else {
		nargs[nanum++] = "-h";
		nargs[nanum++] = host;
		nargs[nanum++] = "-p";
		nargs[nanum++] = port;
	}
	nargs[nanum++] = "-D";
	nargs[nanum++] = manager;
	nargs[nanum++] = "-w";
	nargs[nanum++] = passwd;
	nargs[nanum++] = "-l";
	nargs[nanum++] = nloops;
	nargs[nanum++] = "-L";
	nargs[nanum++] = outerloops;
	nargs[nanum++] = "-r";
	nargs[nanum++] = retries;
	nargs[nanum++] = "-t";
	nargs[nanum++] = delay;
	if ( friendly ) {
		nargs[nanum++] = friendlyOpt;
	}
	if ( chaserefs ) {
		nargs[nanum++] = "-C";
	}
	if ( ignore ) {
		nargs[nanum++] = "-i";
		nargs[nanum++] = ignore;
	}
	nargs[nanum++] = "-e";
	nargs[nanum++] = NULL;		/* will hold the modrdn entry */
	nargs[nanum] = NULL;
	
	/*
	 * generate the modify clients
	 */

	manum = 0;
	snprintf( mcmd, sizeof mcmd, "%s" LDAP_DIRSEP MODIFYCMD,
		progdir );
	margs[manum++] = mcmd;
	if ( uri ) {
		margs[manum++] = "-H";
		margs[manum++] = uri;
	} else {
		margs[manum++] = "-h";
		margs[manum++] = host;
		margs[manum++] = "-p";
		margs[manum++] = port;
	}
	margs[manum++] = "-D";
	margs[manum++] = manager;
	margs[manum++] = "-w";
	margs[manum++] = passwd;
	margs[manum++] = "-l";
	margs[manum++] = mloops;
	margs[manum++] = "-L";
	margs[manum++] = outerloops;
	margs[manum++] = "-r";
	margs[manum++] = retries;
	margs[manum++] = "-t";
	margs[manum++] = delay;
	if ( friendly ) {
		margs[manum++] = friendlyOpt;
	}
	if ( chaserefs ) {
		margs[manum++] = "-C";
	}
	if ( ignore ) {
		margs[manum++] = "-i";
		margs[manum++] = ignore;
	}
	margs[manum++] = "-e";
	margs[manum++] = NULL;		/* will hold the modify entry */
	margs[manum++] = "-a";;
	margs[manum++] = NULL;		/* will hold the ava */
	margs[manum] = NULL;

	/*
	 * generate the add/delete clients
	 */

	aanum = 0;
	snprintf( acmd, sizeof acmd, "%s" LDAP_DIRSEP ADDCMD,
		progdir );
	aargs[aanum++] = acmd;
	if ( uri ) {
		aargs[aanum++] = "-H";
		aargs[aanum++] = uri;
	} else {
		aargs[aanum++] = "-h";
		aargs[aanum++] = host;
		aargs[aanum++] = "-p";
		aargs[aanum++] = port;
	}
	aargs[aanum++] = "-D";
	aargs[aanum++] = manager;
	aargs[aanum++] = "-w";
	aargs[aanum++] = passwd;
	aargs[aanum++] = "-l";
	aargs[aanum++] = aloops;
	aargs[aanum++] = "-L";
	aargs[aanum++] = outerloops;
	aargs[aanum++] = "-r";
	aargs[aanum++] = retries;
	aargs[aanum++] = "-t";
	aargs[aanum++] = delay;
	if ( friendly ) {
		aargs[aanum++] = friendlyOpt;
	}
	if ( chaserefs ) {
		aargs[aanum++] = "-C";
	}
	if ( ignore ) {
		aargs[aanum++] = "-i";
		aargs[aanum++] = ignore;
	}
	aargs[aanum++] = "-f";
	aargs[aanum++] = NULL;		/* will hold the add data file */
	aargs[aanum] = NULL;

	/*
	 * generate the bind clients
	 */

	banum = 0;
	snprintf( bcmd, sizeof bcmd, "%s" LDAP_DIRSEP BINDCMD,
		progdir );
	bargs[banum++] = bcmd;
	if ( !noinit ) {
		bargs[banum++] = "-I";	/* init on each bind */
	}
	if ( uri ) {
		bargs[banum++] = "-H";
		bargs[banum++] = uri;
	} else {
		bargs[banum++] = "-h";
		bargs[banum++] = host;
		bargs[banum++] = "-p";
		bargs[banum++] = port;
	}
	bargs[banum++] = "-l";
	bargs[banum++] = bloops;
	bargs[banum++] = "-L";
	bargs[banum++] = outerloops;
#if 0
	bargs[banum++] = "-r";
	bargs[banum++] = retries;
	bargs[banum++] = "-t";
	bargs[banum++] = delay;
#endif
	if ( friendly ) {
		bargs[banum++] = friendlyOpt;
	}
	if ( chaserefs ) {
		bargs[banum++] = "-C";
	}
	if ( ignore ) {
		bargs[banum++] = "-i";
		bargs[banum++] = ignore;
	}
	if ( nextra ) {
		bargs[banum++] = "-B";
		bargs_extra = &bargs[banum++];
	}
	bargs[banum++] = "-D";
	bargs[banum++] = NULL;
	bargs[banum++] = "-w";
	bargs[banum++] = NULL;
	bargs[banum] = NULL;

#define	DOREQ(n,j) ((n) && ((maxkids > (n)) ? ((j) < maxkids ) : ((j) < (n))))

	for ( j = 0; j < MAXREQS; j++ ) {
		/* search */
		if ( DOREQ( snum, j ) ) {
			int	jj = j % snum;
			int	x = sanum - sextra_args;

			/* base */
			if ( sbase[jj] != NULL ) {
				sargs[sanum - 7] = sbase[jj];

			} else {
				sargs[sanum - 7] = slud[jj]->lud_dn;
			}

			/* scope */
			if ( slud[jj] != NULL ) {
				sargs[sanum - 5] = (char *)ldap_pvt_scope2str( slud[jj]->lud_scope );

			} else {
				sargs[sanum - 5] = "sub";
			}

			/* filter */
			if ( sreqs[jj] != NULL ) {
				sargs[sanum - 3] = sreqs[jj];

			} else if ( slud[jj]->lud_filter != NULL ) {
				sargs[sanum - 3] = slud[jj]->lud_filter;

			} else {
				sargs[sanum - 3] = "(objectClass=*)";
			}

			/* extras */
			sargs[x] = NULL;

			/* attr */
			if ( sattrs[jj] != NULL ) {
				sargs[x++] = "-a";
				sargs[x++] = sattrs[jj];
			}

			/* attrs */
			if ( slud[jj] != NULL && slud[jj]->lud_attrs != NULL ) {
				int	i;

				for ( i = 0; slud[jj]->lud_attrs[ i ] != NULL && x + i < MAXARGS - 1; i++ ) {
					sargs[x + i] = slud[jj]->lud_attrs[ i ];
				}
				sargs[x + i] = NULL;
			}

			fork_child( scmd, sargs );
		}

		/* read */
		if ( DOREQ( rnum, j ) ) {
			int	jj = j % rnum;
			int	x = ranum - rextra_args;

			rargs[ranum - 3] = rreqs[jj];
			if ( rflts[jj] != NULL ) {
				rargs[x++] = "-f";
				rargs[x++] = rflts[jj];
			}
			rargs[x] = NULL;
			fork_child( rcmd, rargs );
		}

		/* rename */
		if ( j < nnum ) {
			nargs[nanum - 1] = nreqs[j];
			fork_child( ncmd, nargs );
		}

		/* modify */
		if ( j < mnum ) {
			margs[manum - 3] = mdn[j];
			margs[manum - 1] = mreqs[j];
			fork_child( mcmd, margs );
		}

		/* add/delete */
		if ( j < anum ) {
			aargs[aanum - 1] = afiles[j];
			fork_child( acmd, aargs );
		}

		/* bind */
		if ( DOREQ( bnum, j ) ) {
			int	jj = j % bnum;

			if ( nextra ) {
				int	n = ((double)nextra)*rand()/(RAND_MAX + 1.0);
				extra_t	*e;

				for ( e = extra; n-- > 0; e = e->next )
					;
				*bargs_extra = e->action;
			}

			if ( battrs[jj] != NULL ) {
				bargs[banum - 3] = manager ? manager : "";
				bargs[banum - 1] = passwd ? passwd : "";

				bargs[banum + 0] = "-b";
				bargs[banum + 1] = breqs[jj];
				bargs[banum + 2] = "-f";
				bargs[banum + 3] = bcreds[jj];
				bargs[banum + 4] = "-a";
				bargs[banum + 5] = battrs[jj];
				bargs[banum + 6] = NULL;

			} else {
				bargs[banum - 3] = breqs[jj];
				bargs[banum - 1] = bcreds[jj];
				bargs[banum] = NULL;
			}

			fork_child( bcmd, bargs );
			bargs[banum] = NULL;
		}
	}

	wait4kids( -1 );

	exit( EXIT_SUCCESS );
}
Esempio n. 16
0
int main(int argc, char **argv) {
  Display *dpy;
  int g15screen_fd;
  int event;
  int error;
  BOOL enabled = True;
  CARD16 power = 0;
  int dummy = 0, lights = True, change = 0;
  int i;
  struct sigaction new_action;
  char * enable_cmd = NULL;
  char * disable_cmd = NULL;
  int dpms_timeout = 0;
  
  for (i=0;i<argc;i++) {
    char argument[20];
    memset(argument,0,20);
    strncpy(argument,argv[i],19);
    if (!strncmp(argument, "-a",2) || !strncmp(argument, "--activate",10)) {
      if(argv[i+1]!=NULL){
        sscanf(argv[i+1],"%i",&dpms_timeout);
        i++;
      }else{
        printf("%s --activate requires an argument <minutes to activation>\n",argv[0]);
        exit(1);
      }
    }
    if (!strncmp(argument, "-e",2) || !strncmp(argument, "--cmd-enable",12)) {
      if(argv[i+1]!=NULL){
        enable_cmd = malloc(strlen(argv[i+1])+1);
        memcpy(enable_cmd, argv[i+1], strlen(argv[i+1]));
        i++;
      }
    }
    if (!strncmp(argument, "-d",2) || !strncmp(argument, "--cmd-disable",13)) {
      if(argv[i+1]!=NULL){
        disable_cmd = malloc(strlen(argv[i+1])+1);
        memcpy(disable_cmd, argv[i+1], strlen(argv[i+1]));
        i++;
      }
    }
    if (!strncmp(argument, "-v",2) || !strncmp(argument, "--version",9)) {
      printf("%s version %s\n",argv[0],VERSION);
      exit(0);     
    }
    if (!strncmp(argument, "-b",2) || !strncmp(argument, "--bright",13)) {
      bright=1;
    }
    if (!strncmp(argument, "-h",2) || !strncmp(argument, "--help",6)) {
      printf("  %s version %s\n  (c)2007 Mike Lampard\n\n",argv[0],VERSION);
      printf(" -a or --activate <minutes>        - cause DPMS to be activated in <minutes> if no activity.\n");
      printf("                                     By default, %s will simply monitor DPMS status, and\n",argv[0]);
      printf("                                     requires a screensaver to activate. \n");
      printf("                                     In this mode, no screensver is needed.\n");
      printf("                                     %s will shut the monitor down on its own after <minutes>\n",argv[0]);
      printf("                                     with no keyboard or mouse activity.\n");
      printf(" -e or --cmd-enable <cmd to run>   - Run program <cmd> when DPMS is activated.\n\n");
      printf(" -d or --cmd-disable <cmd to run>  - Run program <cmd> when DPMS is de-activated.\n\n");
      printf(" -b or --bright                    - When the keyboard is woken up, set the LEDs to bright.\n\n");
      printf(" -v or --version                   - Show program version\n\n");
      printf(" -h or --help                      - This page\n\n");
      exit(0);
    }
  }
  dpy = XOpenDisplay(getenv("DISPLAY"));
  if (!dpy) {
    printf("Unable to open display %s - exiting\n",getenv("DISPLAY"));
    return 1;
  }
  
  do {
      if((g15screen_fd = new_g15_screen(G15_G15RBUF))<0){
        printf("Sorry, cant connect to the G15daemon - retrying\n");
        sleep(2);
      }
  }while(g15screen_fd<0);

  if (!DPMSQueryExtension (dpy, &event, &error)) {
     printf ("XDPMS extension not supported.\n");
     return 1;
  }

  if (!DPMSCapable(dpy)) {
     printf("DPMS is not enabled... exiting\n");
     return 1; 
  }
  
  if (dpms_timeout>0) {
    DPMSEnable(dpy);
    DPMSSetTimeouts(dpy,dpms_timeout*60,dpms_timeout*60,0);
  }

  new_action.sa_handler = sighandler;
  new_action.sa_flags = 0;
  sigaction(SIGINT, &new_action, NULL);
  sigaction(SIGQUIT, &new_action, NULL);

  while(!leaving) {    
   int fg_check = g15_send_cmd (g15screen_fd, G15DAEMON_IS_FOREGROUND, dummy);
   
   if (! DPMSInfo (dpy, &power, &enabled)) {
     printf ("unable to get DPMS state.\n");
     return 1;
   }

   switch(power) {
    case DPMSModeOn: {
      if (lights == False) {
          change = 1;
          fork_child(enable_cmd);
      }
      lights = True;
      break;
    }
    case DPMSModeStandby:
    case DPMSModeSuspend:
    case DPMSModeOff: {
      if(lights == True){
          change = 1;
          fork_child(disable_cmd);
      }
      lights = False;
      break;
    }
   }
   
   if (fg_check==1 && lights == True && change == 0) { // foreground 
       g15_send_cmd (g15screen_fd, G15DAEMON_SWITCH_PRIORITIES, dummy);
   }

   if(change) {
    toggle_g15lights(g15screen_fd,lights,fg_check);
    change=0;
   }
   sleep (1);
  }

  close(g15screen_fd);
  if(enable_cmd!=NULL)
    free(enable_cmd);
  if(disable_cmd!=NULL)
    free(disable_cmd);
  XCloseDisplay(dpy);
  return 0;
}
Esempio n. 17
0
/* Callback for "OK" button on trace popup */
static void
tracefile_callback(Widget w, XtPointer client_data, XtPointer call_data _is_unused)
{
	char *tfn = CN;
	int devfd = -1;
#if defined(X3270_DISPLAY) /*[*/
	int pipefd[2];
	Boolean just_piped = False;
#endif /*]*/
	char *buf;

#if defined(X3270_DISPLAY) /*[*/
	if (w)
		tfn = XawDialogGetValueString((Widget)client_data);
	else
#endif /*]*/
		tfn = (char *)client_data;
	tfn = do_subst(tfn, DS_VARS | DS_TILDE | DS_UNIQUE);
	if (strchr(tfn, '\'') ||
	    ((int)strlen(tfn) > 0 && tfn[strlen(tfn)-1] == '\\')) {
		popup_an_error("Illegal file name: %s", tfn);
		Free(tfn);
		goto done;
	}

	tracef_max = 0;

	if (!strcmp(tfn, "stdout")) {
		tracef = stdout;
	} else {
#if defined(X3270_DISPLAY) /*[*/
		FILE *pipefile = NULL;

		if (!strcmp(tfn, "none") || !tfn[0]) {
			just_piped = True;
			if (!appres.trace_monitor) {
				popup_an_error("Must specify a trace file "
				    "name");
				free(tfn);
				goto done;
			}
		}

		if (appres.trace_monitor) {
			if (pipe(pipefd) < 0) {
				popup_an_errno(errno, "pipe() failed");
				Free(tfn);
				goto done;
			}
			pipefile = fdopen(pipefd[1], "w");
			if (pipefile == NULL) {
				popup_an_errno(errno, "fdopen() failed");
				(void) close(pipefd[0]);
				(void) close(pipefd[1]);
				Free(tfn);
				goto done;
			}
			(void) SETLINEBUF(pipefile);
			(void) fcntl(pipefd[1], F_SETFD, 1);
		}

		if (just_piped) {
			tracef = pipefile;
		} else
#endif /*]*/
		{
		    	Boolean append = False;

#if defined(X3270_DISPLAY) /*[*/
			tracef_pipe = pipefile;
#endif /*]*/
			/* Get the trace file maximum. */
			get_tracef_max();

			/* Open and configure the file. */
			if ((devfd = get_devfd(tfn)) >= 0)
				tracef = fdopen(dup(devfd), "a");
			else if (!strncmp(tfn, ">>", 2)) {
			    	append = True;
				tracef = fopen(tfn + 2, "a");
			} else
				tracef = fopen(tfn, "w");
			if (tracef == (FILE *)NULL) {
				popup_an_errno(errno, "%s", tfn);
#if defined(X3270_DISPLAY) /*[*/
				fclose(tracef_pipe);
				(void) close(pipefd[0]);
				(void) close(pipefd[1]);
#endif /*]*/
				Free(tfn);
				goto done;
			}
			tracef_size = ftello(tracef);
			Replace(tracefile_name,
				NewString(append? tfn + 2: tfn));
			(void) SETLINEBUF(tracef);
#if !defined(_WIN32) /*[*/
			(void) fcntl(fileno(tracef), F_SETFD, 1);
#endif /*]*/
		}
	}

#if defined(X3270_DISPLAY) /*[*/
	/* Start the monitor window */
	if (tracef != stdout && appres.trace_monitor) {
		switch (tracewindow_pid = fork_child()) {
		    case 0:	/* child process */
			{
				char cmd[64];

				(void) snprintf(cmd, sizeof(cmd), "cat <&%d",
					pipefd[0]);
				(void) execlp("xterm", "xterm",
				    "-title", just_piped? "trace": tfn,
				    "-sb", "-e", "/bin/sh", "-c",
				    cmd, CN);
			}
			(void) perror("exec(xterm) failed");
			_exit(1);
		    default:	/* parent */
			(void) close(pipefd[0]);
			++children;
			break;
		    case -1:	/* error */
			popup_an_errno(errno, "fork() failed");
			break;
		}
	}
#endif /*]*/

#if defined(_WIN32) && defined(C3270) /*[*/
	/* Start the monitor window. */
	if (tracef != stdout && appres.trace_monitor && is_installed) {
		STARTUPINFO startupinfo;
		PROCESS_INFORMATION process_information;
		char *path;
		char *args;

	    	(void) memset(&startupinfo, '\0', sizeof(STARTUPINFO));
		startupinfo.cb = sizeof(STARTUPINFO);
		startupinfo.lpTitle = tfn;
		(void) memset(&process_information, '\0',
			      sizeof(PROCESS_INFORMATION));
		path = xs_buffer("%scatf.exe", instdir);
		args = xs_buffer("\"%scatf.exe\" \"%s\"", instdir, tfn);
		if (CreateProcess(
		    path,
		    args,
		    NULL,
		    NULL,
		    FALSE,
		    CREATE_NEW_CONSOLE,
		    NULL,
		    NULL,
		    &startupinfo,
		    &process_information) == 0) {
		    	popup_an_error("CreateProcess(%s) failed: %s",
				path, win32_strerror(GetLastError()));
			Free(path);
			Free(args);
		} else {
			Free(path);
		    	Free(args);
			tracewindow_handle = process_information.hProcess;
			CloseHandle(process_information.hThread);
		}
	}
#endif /*]*/

	Free(tfn);

	/* We're really tracing, turn the flag on. */
	appres.toggle[trace_reason].value = True;
	appres.toggle[trace_reason].changed = True;
	menubar_retoggle(&appres.toggle[trace_reason], trace_reason);

	/* Display current status. */
	buf = create_tracefile_header("started");
	do_ts = False;
	wtrace("%s", buf);
	Free(buf);

done:
#if defined(X3270_DISPLAY) /*[*/
	if (w)
		XtPopdown(trace_shell);
#endif /*]*/
	return;
}
Esempio n. 18
0
// Forks a child process with no support for shared memory
//
// Forks a child process and handles creating the ProcessInfo
// structure associated with it. The default timeout is 90
// seconds.
//
// @param[in] set_core True if set_affinity should be called
// @return Returns the PID of the child process or ERR
pid_t MathildaFork::fork_child(bool set_core) {
	return fork_child(set_core, false, false, DEFAULT_TIMEOUT);
}
Esempio n. 19
0
void fork_init()
{
    if (!strcmp(GetCommandLineA(), "/?/fork"))
    {
        /* We're a fork child */
        log_info("We're a fork child.\n");
        fork_child();
    }
    else
    {
#ifdef _WIN64
        /* On Win64, the default base address for ET_EXEC executable is 0x400000
         * which is problematic that sometimes win32 dlls will allocate memory there
         * To workaround this issue, we first check if the address space there is
         * occupied. If so, we create a suspended child process and pre-reserve
         * the memory region, then transfer control to the child process.
         * The child process detects such circumstances and release the preserved
         * memory before use.
         */
        size_t region_start = 0x400000;
        size_t region_size = 0x10000000; /* 256MB maximum executable size */
        MEMORY_BASIC_INFORMATION info;
        VirtualQuery(region_start, &info, sizeof(MEMORY_BASIC_INFORMATION));
        if (info.State == MEM_FREE && info.RegionSize >= region_size)
        {
            /* That's good, reserve the space now */
            VirtualAlloc(region_start, region_size, MEM_RESERVE, PAGE_NOACCESS);
        }
        else if (info.State == MEM_RESERVE && info.RegionSize == region_size)
        {
            /* We're a child who has the pages protected by the parent, nothing to do here */
        }
        else
        {
            /* Not good, create a child process and hope this time we can do it better */
            log_warning("The address %p is occupied, we have to create another process to proceed.\n", region_start);
            wchar_t filename[MAX_PATH];
            GetModuleFileNameW(NULL, filename, sizeof(filename) / sizeof(filename[0]));
            PROCESS_INFORMATION info;
            STARTUPINFOW si = { 0 };
            si.cb = sizeof(si);
            if (!CreateProcessW(filename, GetCommandLineW(), NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, NULL, &si, &info))
            {
                log_error("CreateProcessW() failed, error code: %d\n", GetLastError());
                ExitProcess(1);
            }
            /* Pre-reserve the memory */
            if (!VirtualAllocEx(info.hProcess, region_start, region_size, MEM_RESERVE, PAGE_NOACCESS))
            {
                log_error("VirtualAllocEx() failed, error code: %d\n", GetLastError());
                ExitProcess(1);
            }
            /* All done */
            log_shutdown();
            ResumeThread(info.hThread);
            ExitProcess(0);
        }
#endif
        /* Allocate fork_info memory early to avoid possible VirtualAlloc() collision */
        VirtualAlloc(FORK_INFO_BASE, BLOCK_SIZE, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
        /* Return control flow to main() */
    }
}