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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "config.h"
#include "gis.h"
#include "glocale.h"
#include "spawn.h"
#define MAX_ARGS 256
#define MAX_BINDINGS 256
#define MAX_SIGNALS 32
#define MAX_REDIRECTS 32
/****************************************************************
* G_spawn(char *command, ...)
*
* This is a more useful alternative to G_system(), which takes
* the command's arguments as separate parameters.
*
****************************************************************/
int G_spawn(char *command, ...)
{
va_list va;
char *args[MAX_ARGS];
int num_args = 0;
struct sigaction act, intr, quit;
sigset_t block, oldmask;
int status = -1;
pid_t pid;
args[0] = command;
va_start(va, command);
for (num_args = 1; num_args < MAX_ARGS; )
{
char *arg = va_arg(va, char *);
if (!arg)
break;
args[num_args++] = arg;
}
va_end(va);
if (num_args >= MAX_ARGS)
{
G_warning(_("too many arguments"));
return -1;
}
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
act.sa_handler = SIG_IGN;
if (sigaction(SIGINT, &act, &intr) < 0)
goto error_1;
if (sigaction(SIGQUIT, &act, &quit) < 0)
goto error_2;
sigemptyset(&block);
sigaddset(&block, SIGCHLD);
if (sigprocmask(SIG_BLOCK, &block, &oldmask) < 0)
goto error_3;
pid = fork();
if (pid < 0)
{
G_warning(_("unable to create a new process"));
goto error_4;
}
if (pid == 0)
{
sigaction(SIGINT, &intr, NULL);
sigaction(SIGQUIT, &quit, NULL);
execvp(command, args);
G_warning(_("unable to execute command"));
_exit(127);
}
else
{
pid_t n;
do n = waitpid(pid, &status, 0);
while (n == (pid_t) -1 && errno == EINTR);
if (n != pid)
status = -1;
}
error_4:
sigprocmask(SIG_SETMASK, &oldmask, NULL);
error_3:
sigaction(SIGQUIT, &quit, NULL);
error_2:
sigaction(SIGINT, &intr, NULL);
error_1:
return status;
}
/****************************************************************
* G_spawn_ex(char *command, ...)
*
* This is a more advanced version of G_spawn().
*
****************************************************************/
struct redirect
{
int dst_fd;
int src_fd;
char *file;
int mode;
};
struct signal
{
int which;
int action;
int signum;
int valid;
struct sigaction old_act;
sigset_t old_mask;
};
struct binding
{
char *var;
char *val;
};
static int undo_signals(struct signal *signals, int num_signals, int which)
{
int error = 0;
int i;
for (i = num_signals-1; i >= 0; i--)
{
struct signal *s = &signals[i];
if (s->which != which)
continue;
if (!s->valid)
continue;
switch (s->action)
{
case SSA_IGNORE:
case SSA_DEFAULT:
if (sigaction(s->signum, &s->old_act, NULL) < 0)
{
G_warning(_("G_spawn: unable to restore signal %d"), s->signum);
error = 1;
}
break;
case SSA_BLOCK:
case SSA_UNBLOCK:
if (sigprocmask(SIG_UNBLOCK, &s->old_mask, NULL) < 0)
{
G_warning(_("G_spawn: unable to restore signal %d"), s->signum);
error = 1;
}
break;
}
}
return !error;
}
static int do_signals(struct signal *signals, int num_signals, int which)
{
struct sigaction act;
sigset_t mask;
int error = 0;
int i;
sigemptyset(&act.sa_mask);
act.sa_flags = SA_RESTART;
for (i = 0; i < num_signals; i++)
{
struct signal *s = &signals[i];
if (s->which != which)
continue;
switch (s->action)
{
case SSA_IGNORE:
act.sa_handler = SIG_IGN;
if (sigaction(s->signum, &act, &s->old_act) < 0)
{
G_warning(_("G_spawn: unable to reset signal %d"), s->signum);
error = 1;
}
else
s->valid = 1;
break;
case SSA_DEFAULT:
act.sa_handler = SIG_DFL;
if (sigaction(s->signum, &act, &s->old_act) < 0)
{
G_warning(_("G_spawn: unable to ignore signal %d"), s->signum);
error = 1;
}
else
s->valid = 1;
break;
case SSA_BLOCK:
sigemptyset(&mask);
sigaddset(&mask, s->signum);
if (sigprocmask(SIG_BLOCK, &mask, &s->old_mask) < 0)
{
G_warning(_("G_spawn: unable to block signal %d"), s->signum);
error = 1;
}
break;
case SSA_UNBLOCK:
sigemptyset(&mask);
sigaddset(&mask, s->signum);
if (sigprocmask(SIG_UNBLOCK, &mask, &s->old_mask) < 0)
{
G_warning(_("G_spawn: unable to unblock signal %d"), s->signum);
error = 1;
}
else
s->valid = 1;
break;
}
}
return !error;
}
static void do_redirects(struct redirect *redirects, int num_redirects)
{
int i;
for (i = 0; i < num_redirects; i++)
{
struct redirect *r = &redirects[i];
if (r->file)
{
r->src_fd = open(r->file, r->mode, 0666);
if (r->src_fd < 0)
{
G_warning(_("G_spawn: unable to open file %s"), r->file);
_exit(127);
}
if (dup2(r->src_fd, r->dst_fd) < 0)
{
G_warning(_("G_spawn: unable to duplicate descriptor %d to %d"), r->src_fd, r->dst_fd);
_exit(127);
}
close(r->src_fd);
}
else if (r->src_fd >= 0)
{
if (dup2(r->src_fd, r->dst_fd) < 0)
{
G_warning(_("G_spawn: unable to duplicate descriptor %d to %d"), r->src_fd, r->dst_fd);
_exit(127);
}
}
else
close(r->dst_fd);
}
}
static void do_bindings(struct binding *bindings, int num_bindings)
{
int i;
for (i = 0; i < num_bindings; i++)
{
struct binding *b = &bindings[i];
char *str;
str = G_malloc(strlen(b->var) + strlen(b->val) + 2);
sprintf(str, "%s=%s", b->var, b->val);
putenv(str);
}
}
int G_spawn_ex(char *command, ...)
{
char *args[MAX_ARGS];
int num_args = 0;
struct redirect redirects[MAX_REDIRECTS];
int num_redirects = 0;
struct signal signals[MAX_SIGNALS];
int num_signals = 0;
struct binding bindings[MAX_BINDINGS];
int num_bindings = 0;
int background = 0;
char *directory = NULL;
va_list va;
char *var, *val;
int status = -1;
pid_t pid;
args[num_args++] = command;
va_start(va, command);
for (;;)
{
char *arg = va_arg(va, char *);
switch ((int) arg)
{
case 0:
args[num_args++] = NULL;
break;
case ((int) SF_REDIRECT_FILE):
redirects[num_redirects].dst_fd = va_arg(va, int);
redirects[num_redirects].src_fd = -1;
redirects[num_redirects].mode = va_arg(va, int);
redirects[num_redirects].file = va_arg(va, char *);
num_redirects++;
break;
case ((int) SF_REDIRECT_DESCRIPTOR):
redirects[num_redirects].dst_fd = va_arg(va, int);
redirects[num_redirects].src_fd = va_arg(va, int);
redirects[num_redirects].file = NULL;
num_redirects++;
break;
case ((int) SF_CLOSE_DESCRIPTOR):
redirects[num_redirects].dst_fd = va_arg(va, int);
redirects[num_redirects].src_fd = -1;
redirects[num_redirects].file = NULL;
num_redirects++;
break;
case ((int) SF_SIGNAL):
signals[num_signals].which = va_arg(va, int);
signals[num_signals].action = va_arg(va, int);
signals[num_signals].signum = va_arg(va, int);
signals[num_signals].valid = 0;;
num_signals++;
break;
case ((int) SF_VARIABLE):
var = va_arg(va, char *);
val = getenv(var);
args[num_args++] = val ? val : "";
break;
case ((int) SF_BINDING):
bindings[num_bindings].var = va_arg(va, char *);
bindings[num_bindings].val = va_arg(va, char *);
num_bindings++;
break;
case ((int) SF_BACKGROUND):
background = 1;
break;
case ((int) SF_DIRECTORY):
directory = va_arg(va, char *);
break;
default:
args[num_args++] = arg;
break;
}
if (!arg)
break;
}
va_end(va);
if (!do_signals(signals, num_signals, SST_PRE))
goto error_1;
pid = fork();
if (pid < 0)
{
G_warning(_("unable to create a new process"));
goto error_2;
}
if (pid == 0)
{
if (!undo_signals(signals, num_signals, SST_PRE))
_exit(127);
if (!do_signals(signals, num_signals, SST_CHILD))
_exit(127);
if (directory)
if (chdir(directory) < 0)
{
G_warning(_("unable to change directory to %s"), directory);
_exit(127);
}
do_redirects(redirects, num_redirects);
do_bindings(bindings, num_bindings);
execvp(command, args);
G_warning(_("unable to execute command"));
_exit(127);
}
do_signals(signals, num_signals, SST_POST);
if (background)
status = (int) pid;
else
{
pid_t n;
do n = waitpid(pid, &status, 0);
while (n == (pid_t) -1 && errno == EINTR);
if (n != pid)
status = -1;
}
undo_signals(signals, num_signals, SST_POST);
error_2:
undo_signals(signals, num_signals, SST_PRE);
error_1:
return status;
}
|