Updated Spanish translation
[anjuta-git-plugin.git] / plugins / valgrind / process.c
bloba6d15c2eb0819a8616c137628d2f633e7a80d4db
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * Authors: Jeffrey Stedfast <fejj@ximian.com>
5 * Copyright 2003 Ximian, Inc. (www.ximian.com)
7 * This program 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 2 of the License, or
10 * (at your option) any later version.
12 * This program 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.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #include <libgnome/gnome-i18n.h>
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <sys/wait.h>
33 #include <signal.h>
34 #include <unistd.h>
35 #include <fcntl.h>
36 #include <errno.h>
38 #include "process.h"
40 #define d(x)
42 pid_t
43 process_fork (const char *path, char **argv, gboolean redirect, int ignfd, int *infd, int *outfd, int *errfd, GError **err)
45 int errnosav, fds[6], i;
46 pid_t pid;
48 for (i = 0; i < 6; i++)
49 fds[i] = -1;
51 for (i = 0; i < 6; i += 2) {
52 if (pipe (fds + i) == -1) {
53 errnosav = errno;
54 g_set_error (err, g_quark_from_string ("process"), errno,
55 _("Failed to create pipe to '%s': %s"),
56 argv[0], g_strerror (errno));
58 for (i = 0; i < 6; i++) {
59 if (fds[i] == -1)
60 break;
61 close (fds[i]);
64 errno = errnosav;
66 return -1;
70 #if d(!)0
71 fprintf (stderr, "exec()'ing %s\n", path);
72 for (i = 0; argv[i]; i++)
73 fprintf (stderr, "%s ", argv[i]);
74 fprintf (stderr, "\n");
75 #endif
77 if (!(pid = fork ())) {
78 /* child process */
79 int maxfd, nullfd = -1;
81 if (!redirect) {
82 if (!infd || !outfd || !errfd)
83 nullfd = open ("/dev/null", O_WRONLY);
85 if (dup2 (infd ? fds[0] : nullfd, STDIN_FILENO) == -1)
86 _exit (255);
88 if (dup2 (outfd ? fds[3] : nullfd, STDOUT_FILENO) == -1)
89 _exit (255);
91 if (dup2 (errfd ? fds[5] : nullfd, STDERR_FILENO) == -1)
92 _exit (255);
95 setsid ();
97 if ((maxfd = sysconf (_SC_OPEN_MAX)) > 0) {
98 int fd;
100 for (fd = 3; fd < maxfd; fd++) {
101 if (fd != ignfd)
102 fcntl (fd, F_SETFD, FD_CLOEXEC);
106 execv (path, argv);
107 _exit (255);
108 } else if (pid == -1) {
109 g_set_error (err, g_quark_from_string ("process"), errno,
110 _("Failed to create child process '%s': %s"),
111 argv[0], g_strerror (errno));
112 return -1;
115 /* parent process */
116 close (fds[0]);
117 close (fds[3]);
118 close (fds[5]);
120 if (infd)
121 *infd = fds[1];
122 else
123 close (fds[1]);
125 if (outfd)
126 *outfd = fds[2];
127 else
128 close (fds[2]);
130 if (errfd)
131 *errfd = fds[4];
132 else
133 close (fds[4]);
135 return pid;
140 process_wait (pid_t pid)
142 sigset_t mask, omask;
143 int status;
144 pid_t r;
146 sigemptyset (&mask);
147 sigaddset (&mask, SIGALRM);
148 sigprocmask (SIG_BLOCK, &mask, &omask);
149 alarm (1);
151 r = waitpid (pid, &status, 0);
153 alarm (0);
154 sigprocmask (SIG_SETMASK, &omask, NULL);
156 if (r == (pid_t) -1 && errno == EINTR) {
157 kill (pid, SIGTERM);
158 sleep (1);
159 r = waitpid (pid, &status, WNOHANG);
160 if (r == (pid_t) 0) {
161 kill (pid, SIGKILL);
162 sleep (1);
163 r = waitpid (pid, &status, WNOHANG);
167 if (r != (pid_t) -1 && WIFEXITED (status))
168 return WEXITSTATUS (status);
169 else
170 return -1;
175 process_kill (pid_t pid)
177 int status;
178 pid_t r;
180 kill (pid, SIGTERM);
181 sleep (1);
182 r = waitpid (pid, &status, WNOHANG);
183 if (r == (pid_t) 0) {
184 kill (pid, SIGKILL);
185 sleep (1);
186 r = waitpid (pid, &status, WNOHANG);
189 if (r != (pid_t) -1 && WIFEXITED (status))
190 return WEXITSTATUS (status);
191 else
192 return -1;