static int
do_test (void)
{
  if (signal (SIGUSR1, sigusr1_handler) == SIG_ERR)
    FAIL_EXIT1 ("signal (SIGUSR1): %m\n");

  sigset_t sigs;
  sigemptyset (&sigs);
  sigaddset (&sigs, SIGUSR2);
  if (sigprocmask (SIG_BLOCK, &sigs, NULL) != 0)
    FAIL_EXIT1 ("sigprocmask (SIGBLOCK, SIGUSR2): %m");
  pid_t pid = signal_sender ();
  int signo = 0;
  int ret = sigwait (&sigs, &signo);
  if (ret != 0)
    {
      support_record_failure ();
      errno = ret;
      printf ("error: sigwait failed: %m (%d)\n", ret);
    }
  TEST_VERIFY (signo == SIGUSR2);

  int status;
  xwaitpid (pid, &status, 0);
  TEST_VERIFY (status == 0);

  return 0;
}
Exemple #2
0
int	exec_pipe(char **command, int tab_len)
{
  int	pid;

  pid = parse_command_for_pipe(command, tab_len, 1);
  xwaitpid(pid, 0, WUNTRACED);
  return (0);
}
Exemple #3
0
/*
 * Execute the program supplied on the command line. If restart was set
 * then send the child process SIGTERM and restart it.
 */
void
run_utility(char *argv[]) {
	int pid;
	int i, m;
	int ret, status;
	struct timespec delay = { 0, 1000000 };
	char **new_argv;
	char *p, *arg_buf;
	int argc;

	if (restart_opt == 1)
		terminate_utility();

	/* clone argv on each invocation to make the implementation of more
	 * complex subsitution rules possible and easy
	 */
	for (argc=0; argv[argc]; argc++);
	arg_buf = malloc(ARG_MAX);
	new_argv = calloc(argc+1, sizeof(char *));
	for (m=0, i=0, p=arg_buf; i<argc; i++) {
		new_argv[i] = p;
		if ((m < 1) && (strcmp(argv[i], "/_")) == 0) {
			p += strlen(xrealpath(leading_edge->fn, p));
			m++;
		}
		else
			p += strlcpy(p, argv[i], ARG_MAX - (p - arg_buf));
		p++;
	}

	pid = xfork();
	if (pid == -1)
		err(1, "can't fork");

	if (pid == 0) {
		if (clear_opt == 1)
			(void) system("/usr/bin/clear");
		/* Set process group so subprocess can be signaled */
		if (restart_opt == 1)
			setpgid(0, getpid());
		/* wait up to 1 seconds for each file to become available */
		for (i=0; i < 10; i++) {
			ret = xexecvp(new_argv[0], new_argv);
			if (errno == ETXTBSY) nanosleep(&delay, NULL);
			else break;
		}
		if (ret != 0)
			err(1, "exec %s", new_argv[0]);
	}
	child_pid = pid;

	if (restart_opt == 0)
		xwaitpid(pid, &status, 0);

	xfree(arg_buf);
	xfree(new_argv);
}
Exemple #4
0
void
terminate_utility() {
	int status;

	if (child_pid > 0) {
		xkillpg(child_pid, SIGTERM);
		xwaitpid(child_pid, &status, 0);
		child_pid = 0;
	}
}
Exemple #5
0
static void
init_invalid_status (void)
{
  pid_t pid = xfork ();
  if (pid == 0)
    _exit (127);
  xwaitpid (pid, &invalid_status, 0);
  if (WIFEXITED (invalid_status))
    invalid_status = WEXITSTATUS (invalid_status);
}
Exemple #6
0
void
terminate_utility() {
	int status;

	if (child_pid > 0) {
		#ifdef DEBUG
		fprintf(stderr, "signal %d sent to pid %d\n", SIGTERM,
		    child_pid);
		#endif
		xkill(child_pid, SIGTERM);
		xwaitpid(child_pid, &status, 0);
		child_pid = 0;
	}
}
Exemple #7
0
static int	manage_fork(t_fifo_elem *cur, t_sllist **myenv, int *status)
{
  cur->data.pid = xfork();
  if (!cur->data.pid)
    return (manage_child(cur, myenv));
  if (cur->data.flag[DSYNC])
    {
      printf("%s%d\n", BG_PRC, cur->data.pid);
      usleep(150);
    }
  else if (cur->data.pid > 0 &&
	   (!F_ISPIPE(cur->data.flag[OUT]) ||
	    !cur->next))
    {
      xwaitpid(cur->data.pid, status, 0);
      manage_status(cur, *status);
    }
  return (EXIT_SUCCESS);
}
Exemple #8
0
int	exec_father(char *command, int *p, int pid, int first)
{
  int	ret;

  xclose(p[1]);
  if (first)
    ret = xdup(0);
  if (first && ret < 0)
    return (pid);
  if (xdup2(p[0], 0) < 0)
    return (pid);
  parse_command_redir(command);
  xwaitpid(pid, 0, (first ? WNOHANG : WNOHANG & WUNTRACED));
  if (first && xdup2(ret, 0) < 0)
    return (pid);
  xclose(p[0]);
  xclose(ret);
  return (0);
}
Exemple #9
0
int		gere_left(t_info *info, char *str, int flag)
{
  int		pid;
  int		value;

  if (flag == CHILD)
    {
      value = gere_left_next(info, str, flag);
      exit(value);
    }
  else
    {
      if ((pid = xfork()) == -1)
	return (status(info, EXIT_FAILURE));
      if (pid == 0)
	{
	  value = gere_left_next(info, str, flag);
	  exit(value);
	}
      else if (xwaitpid(pid, &value, 0) == EXIT_FAILURE)
	return (status(info, EXIT_FAILURE));
    }
  return (EXIT_SUCCESS);
}
bool AsyncReadFile::iter(QStringList *errors, QByteArray *acc_contents, bool *p_made_progress)
{
    *p_made_progress = true;

    if(m_ofn.isEmpty()) {
        errors->append(QStringLiteral("iter() called but start() not called before"));
        return false;
    }

    // ******** ITER 1

    if(m_fn.isEmpty()) {
        m_fn = m_ofn;
        c_filename = ::strdup(m_fn.toLocal8Bit().constData());

        if(c_filename == NULL) {
            errors->append(QStringLiteral("out of memory"));
            return false;
        }

        return true;
    }

    // ******** ITER 2

    if(!m_done_pipefork) {

        int fds_msg[2] = { -1, -1 };
        int fds_content[2] = { -1, -1 };

        if(!xpipe2(fds_msg, "error message", errors)) {
            return false;
        }

        if(m_getc) {
            if(!xpipe2(fds_content, "content", errors)) {
                return false;
            }
        }

        m_pid = ::fork();

        if(m_pid < 0) {
            errors->append(make_latin1_errmsg(errno, "could not fork"));
            return false;
        }

        if(m_pid == 0) {
            char c_filename_short[20 + 1] = "";
            {
                const size_t l = strlen(c_filename);
                const size_t start = (l > 20 ? l - 20 : 0);
                strncpy(c_filename_short, c_filename + start, 20);
            }

            if(::close(fds_msg[PIPE_READ])) {
                CLDMYDBG("close(error message FD=%d): %s", fds_msg[PIPE_READ], strerror(errno));
            }
            else {
                CLDMYDBG("close(error message FD=%d)", fds_msg[PIPE_READ]);
            }

            fds_msg[PIPE_READ] = (-1);

            if(m_getc) {


                if(::close(fds_content[PIPE_READ])) {
                    CLDMYDBG("close(content FD=%d): %s", fds_content[PIPE_READ], strerror(errno));
                }
                else {
                    CLDMYDBG("close(content FD=%d)", fds_content[PIPE_READ]);
                }


                fds_content[PIPE_READ] = (-1);
            }

            FILE *msgfd_h = ::fdopen(fds_msg[PIPE_WRITE], "w");

            if(msgfd_h == NULL) {
                CLDMYDBG("could not fdopen(error message pipe FD=%d): %s", fds_msg[PIPE_WRITE], strerror(errno));
                char msg[] = "could not convert pipe to FILE";
                ssize_t byteswritten = ::write(fds_msg[PIPE_WRITE], msg, sizeof(msg));
                (void) byteswritten;
                ::_exit(1);
            }

            // pipe write, file read
            child_read_file(c_filename, c_filename_short, msgfd_h, fds_content[PIPE_WRITE], m_maxreadsize, pipe_bufsize, fileread_bufsize, CLF_DEBUG_enabled);
            // should never return
        }

        ASFMYDBG("spawned kid PID=%d", m_pid);

        (void)xclose(fds_msg[PIPE_WRITE], "error message");

        if(m_getc) {
            (void)xclose(fds_content[PIPE_WRITE], "content");
        }

        m_msgfd = fds_msg[PIPE_READ];
        m_contentfd = fds_content[PIPE_READ];

        m_done_pipefork = true;
        return true;
    }

    // ******** ITER 3...

    if(!m_done) {
        bool got_errmsg_data = false;

        if(m_msgfd >= 0) {

            if(m_errmsg_buffer == NULL) {
                m_errmsg_buffer = (char *)::malloc(errmsg_bufsize);

                if(m_errmsg_buffer == NULL) {
                    force_kill_child("no memory");
                    errors->append(make_latin1_errmsg(0, "no memory"));
                    return false;
                }
            }

            if(!xread(m_msgfd, "error message", m_errmsg_buffer, errmsg_bufsize, errors, &m_acc_err, &got_errmsg_data)) {
                return false;
            }

        }

        bool got_content_data = false;

        if(m_contentfd >= 0 && m_getc) {

            if(m_content_buffer_pipe_from_child == NULL) {
                m_content_buffer_pipe_from_child = (char *)::malloc(pipe_bufsize);

                if(m_content_buffer_pipe_from_child == NULL) {
                    force_kill_child("no memory");
                    errors->append(make_latin1_errmsg(0, "no memory"));
                    return false;
                }
            }

            if(!xread(m_contentfd, "content", m_content_buffer_pipe_from_child, pipe_bufsize, errors, acc_contents, &got_content_data)) {
                return false;
            }

        }

        *p_made_progress = (got_errmsg_data || got_content_data);

        // we only do this if there is a chance it will be interesting
        if((m_msgfd < 0 && m_contentfd < 0) || (!*p_made_progress)) {

            bool got_pid_change = false;

            if(!xwaitpid(m_pid, errors, &got_pid_change)) {
                m_done = true;
                *p_made_progress  = true;

                if(!m_acc_err.isEmpty()) {
                    errors->append(err_xbin_2_local_qstring(m_acc_err));
                    m_acc_err.clear();
                }

                return false;
            }

            *p_made_progress = (got_errmsg_data || got_content_data || got_pid_change);
        }

        return true;

    }

    // ******** ITER LAST

    if(m_done) {
        *p_made_progress = false;
        return false;
    }

    PROGRAMMERERROR("WTF");

}