root/exec/exec1.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. main

     1 /* Program execution for Emacs.
     2 
     3 Copyright (C) 2023 Free Software Foundation, Inc.
     4 
     5 This file is part of GNU Emacs.
     6 
     7 GNU Emacs is free software: you can redistribute it and/or modify
     8 it under the terms of the GNU General Public License as published by
     9 the Free Software Foundation, either version 3 of the License, or (at
    10 your option) any later version.
    11 
    12 GNU Emacs is distributed in the hope that it will be useful,
    13 but WITHOUT ANY WARRANTY; without even the implied warranty of
    14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    15 GNU General Public License for more details.
    16 
    17 You should have received a copy of the GNU General Public License
    18 along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
    19 
    20 #include <config.h>
    21 #include <unistd.h>
    22 #include <stdlib.h>
    23 #include <stdio.h>
    24 #include <sys/wait.h>
    25 
    26 #include "exec.h"
    27 
    28 /* exec1 is a program which takes another program and its arguments,
    29    forks, and executes that program, all while tracing it and its
    30    children to use the program execution mechanism defined in exec.c.
    31 
    32    This is necessary to bypass security restrictions which prohibit
    33    Emacs from loading executables from certain directories, by, in
    34    effect, replacing the executable loader in the Linux kernel.  */
    35 
    36 
    37 
    38 int
    39 main (int argc, char **argv)
    40 {
    41   pid_t pid, pid1;
    42   extern char **environ;
    43   int wstatus;
    44 
    45   pid1 = getpid ();
    46   pid = fork ();
    47 
    48   if (!pid)
    49     {
    50       /* Set the process group used to the parent.  */
    51       if (setpgid (0, pid1))
    52         perror ("setpgid");
    53 
    54       tracing_execve (argv[2], argv + 2, environ);
    55 
    56       /* An error occured.  Exit with failure.  */
    57       exit (127);
    58     }
    59   else
    60     {
    61       /* Provide the file name of the loader.  */
    62       exec_init (argv[1]);
    63 
    64       if (after_fork (pid))
    65         exit (127);
    66 
    67       /* Start waiting for the process to exit.  */
    68 
    69       while (true)
    70         {
    71           pid1 = exec_waitpid (-1, &wstatus, 0);
    72 
    73           /* If the child process exits normally, exit with its status
    74              code.  If not, raise the signal that caused it to
    75              exit.  */
    76 
    77           if (pid == pid1)
    78             {
    79               if (WIFEXITED (wstatus))
    80                 exit (WEXITSTATUS (wstatus));
    81               else /* if WIFSIGNALED (wstatus) */
    82                 {
    83                   raise (WTERMSIG (wstatus));
    84 
    85                   /* Just in case the signal raised doesn't cause an
    86                      exit.  */
    87                   exit (127);
    88                 }
    89             }
    90 
    91           /* Otherwise, continue looping.  */
    92         }
    93     }
    94 }

/* [<][>][^][v][top][bottom][index][help] */