summaryrefslogtreecommitdiff
path: root/exec/exec1.c
blob: aaff9a94c62827fc1e084c747440bb0226990fd8 (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
/* 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 <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>

#include "exec.h"

/* exec1 is a program which takes another program and its arguments,
   forks, and executes that program, all while tracing it and its
   children to use the program execution mechanism defined in exec.c.

   This is necessary to bypass security restrictions which prohibit
   Emacs from loading executables from certain directories, by, in
   effect, replacing the executable loader in the Linux kernel.  */



int
main (int argc, char **argv)
{
  pid_t pid, pid1;
  extern char **environ;
  int wstatus;

  pid1 = getpid ();
  pid = fork ();

  if (!pid)
    {
      /* Set the process group used to the parent.  */
      if (setpgid (0, pid1))
	perror ("setpgid");

      tracing_execve (argv[2], argv + 2, environ);

      /* An error occurred.  Exit with failure.  */
      exit (127);
    }
  else
    {
      /* Provide the file name of the loader.  */
      exec_init (argv[1]);

      if (after_fork (pid))
	exit (127);

      /* Start waiting for the process to exit.  */

      while (true)
	{
	  pid1 = exec_waitpid (-1, &wstatus, 0);

	  /* If the child process exits normally, exit with its status
	     code.  If not, raise the signal that caused it to
	     exit.  */

	  if (pid == pid1)
	    {
	      if (WIFEXITED (wstatus))
		exit (WEXITSTATUS (wstatus));
	      else /* if WIFSIGNALED (wstatus) */
		{
		  raise (WTERMSIG (wstatus));

		  /* Just in case the signal raised doesn't cause an
		     exit.  */
		  exit (127);
		}
	    }

	  /* Otherwise, continue looping.  */
	}
    }
}