File: glibcsystem.cpp

package info (click to toggle)
dmtcp 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,496 kB
  • sloc: cpp: 33,592; ansic: 28,099; sh: 6,735; makefile: 1,950; perl: 1,690; python: 1,241; asm: 138; java: 13
file content (125 lines) | stat: -rw-r--r-- 3,909 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* Copyright (C) 1991-2000,2002,2003,2005,2007 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

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

   The GNU C Library 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
   Lesser General Public License for more details.

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

/*
 * This code is a simplified version of do_system() from glibc-2.9.
 * Its primary use is to be called by the wrapper, execwrappers.cpp:system().
 * This way, calls to system() go to our do_system(), which inherits our
 * wrappers around fork/exec.
 *   Without this, any call to system() would go directly to glibc, where
 * it would pick up the _unwrapped_ versions of fork/exec already defined
 * in glibc.  If you really want an unwrapped version of glibc's system(),
 * then call execwrappers.cpp:_real_system().
 */
#include <errno.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

#define  SHELL_PATH  "/bin/sh"  /* Path of the shell.  */
#define  SHELL_NAME  "sh"    /* Name to give it.  */

/* Execute LINE as a shell command, returning its status.  */
__attribute__ ((visibility ("hidden")))
int do_system (const char *line)
{
  int status, save;
  pid_t pid;
  struct sigaction sa;
  struct sigaction intr, quit;
  sigset_t omask;

  sa.sa_handler = SIG_IGN;
  sa.sa_flags = 0;
  sigemptyset (&sa.sa_mask);

  if (sigaction (SIGINT, &sa, &intr) < 0) {
    goto out;
  }
  if (sigaction (SIGQUIT, &sa, &quit) < 0) {
    save = errno;
    goto out_restore_sigint;
  }

  /* We reuse the bitmap in the 'sa' structure.  */
  sigaddset (&sa.sa_mask, SIGCHLD);
  save = errno;
  if (sigprocmask (SIG_BLOCK, &sa.sa_mask, &omask) < 0) {
    if (errno == ENOSYS) {
      errno = save;
    } else {
      save = errno;
      (void) sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
out_restore_sigint:
      (void) sigaction (SIGINT, &intr, (struct sigaction *) NULL);
      errno = save;
      //set_errno (save);
out:
      return -1;
    }
  }

  pid = fork ();
  if (pid == (pid_t) 0) {
    /* Child side.  */
    const char *new_argv[4];
    new_argv[0] = SHELL_NAME;
    new_argv[1] = "-c";
    new_argv[2] = line;
    new_argv[3] = NULL;

    /* Restore the signals.  */
    (void) sigaction (SIGINT, &intr, (struct sigaction *) NULL);
    (void) sigaction (SIGQUIT, &quit, (struct sigaction *) NULL);
    (void) sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL);

    /* Exec the shell.  */
    (void) execve (SHELL_PATH, (char *const *) new_argv, __environ);
    _exit (127);
  } else if (pid < (pid_t) 0) {
    /* The fork failed.  */
    status = -1;
  } else {
    /* Parent side.  */
    /* Note that system() is a cancellation point.  But since we call
       waitpid() which itself is a cancellation point we do not
       have to do anything here.  */
    do {
      if (TEMP_FAILURE_RETRY (waitpid (pid, &status, 0)) != pid) {
        status = -1;
        if (status == -1 && errno == ECHILD) {
          break;
        }
      }
    }
    while (WIFEXITED(status) == 0);
  }

  save = errno;
  if (((sigaction (SIGINT, &intr, (struct sigaction *) NULL)
        | sigaction (SIGQUIT, &quit, (struct sigaction *) NULL)) != 0)
      || sigprocmask (SIG_SETMASK, &omask, (sigset_t *) NULL) != 0) {
    /* glibc cannot be used on systems without waitpid.  */
    if (errno == ENOSYS)
      errno = save;
    else
      status = -1;
  }

  return status;
}