summaryrefslogtreecommitdiff
path: root/exec/test.c
blob: 7185c958b8702772cb39caed9059e3ae83baf117 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/* Program execution for Emacs.

Copyright (C) 2023-2024 Free Software Foundation, Inc.

This file is part of GNU Emacs.

GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */

#include <config.h>

#include <signal.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#include <sys/wait.h>

#include "exec.h"



static void
print_usage (void)
{
  fprintf (stderr, "test loader-name program [args...]\n"
	   "Run the given program using the specified loader.\n");
}



extern char **environ;

/* This program uses libexec to wrap the execution of a child
   process.  */

int
main (int argc, char **argv)
{
  pid_t pid, child;
  int sig;
  sigset_t sigset;

  /* Check that there are a sufficient number of arguments.  */

  if (argc < 3)
    {
      print_usage ();
      return 1;
    }

  exec_init (argv[1]);

  /* Block SIGCHLD to avoid reentrant modification of the child
     process list.  */

  sigemptyset (&sigset);
  sigaddset (&sigset, SIGCHLD);
  sigprocmask (SIG_BLOCK, &sigset, NULL);

  if (!(pid = fork ()))
    {
      tracing_execve (argv[2], argv + 2, environ);
      fprintf (stderr, "tracing_execve: %s\n",
	       strerror (errno));
      exit (1);
    }
  else if (after_fork (pid))
    {
      fprintf (stderr, "after_fork: %s\n",
	       strerror (errno));
      exit (1);
    }

  /* Now start waiting for child processes to exit.  */

  while (true)
    {
      child = exec_waitpid (-1, &sig, 0);

      /* If pid is -1, a system call has been handled.  */

      if (child == -1)
	continue;

      /* If the main process exits, then exit as well.  */

      if (child == pid && !WIFSTOPPED (sig))
	return (WIFEXITED (sig)
		? WEXITSTATUS (sig)
		: WTERMSIG (sig));
    }
}