diff options
Diffstat (limited to 'utils/shd.c')
-rw-r--r-- | utils/shd.c | 82 |
1 files changed, 45 insertions, 37 deletions
diff --git a/utils/shd.c b/utils/shd.c index cb4b0ad0..a1a4b26b 100644 --- a/utils/shd.c +++ b/utils/shd.c @@ -1,5 +1,5 @@ -/* - Copyright (C) 1994, 1995 Free Software Foundation +/* + Copyright (C) 1994,95,99,2002 Free Software Foundation This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as @@ -23,6 +23,7 @@ #include <device/device.h> #include <unistd.h> #include <errno.h> +#include <error.h> #include <sys/wait.h> #include <fcntl.h> #include <stdlib.h> @@ -44,7 +45,7 @@ reap (pid_t waitfor) if (pid == -1) { if (errno != ECHILD && errno != EWOULDBLOCK) - perror ("waitpid"); + error (0, errno, "waitpid"); return; } else if (WIFEXITED (status)) @@ -54,7 +55,7 @@ reap (pid_t waitfor) printf ("PID %d %s\n", pid, strsignal (WTERMSIG (status))); else if (WIFSTOPPED (status)) - printf ("PID %d stopped: %s\n", + printf ("PID %d stopped: %s\n", pid, strsignal (WSTOPSIG (status))); else printf ("PID %d bizarre status %#x\n", pid, status); @@ -69,6 +70,7 @@ run (char **argv, int fd0, int fd1) { file_t file; char *program; + error_t err; if (strchr (argv[0], '/') != NULL) program = argv[0]; @@ -84,18 +86,22 @@ run (char **argv, int fd0, int fd1) file = file_name_lookup (program, O_EXEC, 0); if (file == MACH_PORT_NULL) { - perror (program); + error (0, errno, "%s", program); return -1; } else { task_t task; pid_t pid; - - errno = task_create (mach_task_self (), 0, &task); - if (errno) + + err = task_create (mach_task_self (), +#ifdef KERN_INVALID_LEDGER + NULL, 0, /* OSF Mach */ +#endif + 0, &task); + if (err) { - perror ("task_create"); + error (0, err, "task_create"); pid = -1; } else @@ -109,12 +115,12 @@ run (char **argv, int fd0, int fd1) *save = dup (to); if (*save < 0) { - perror ("dup"); + error (0, errno, "dup"); return -1; } if (dup2 (from, to) != to) { - perror ("dup2"); + error (0, errno, "dup2"); return -1; } close (from); @@ -126,7 +132,7 @@ run (char **argv, int fd0, int fd1) return 0; if (dup2 (*save, to) != to) { - perror ("dup2"); + error (0, errno, "dup2"); return -1; } close (*save); @@ -135,13 +141,16 @@ run (char **argv, int fd0, int fd1) pid = task2pid (task); if (pid == -1) - perror ("task2pid"), pid = 0; - errno = proc_child (proc, task); - if (errno) - perror ("proc_child"); + { + error (0, errno, "task2pid"); + pid = 0; + } + err = proc_child (proc, task); + if (err) + error (0, err, "proc_child"); if (pause_startup) { - printf ("Pausing..."); + printf ("Pausing (child PID %d)...", pid); fflush (stdout); getchar (); } @@ -149,25 +158,26 @@ run (char **argv, int fd0, int fd1) if (movefd (fd0, 0, &save0) || movefd (fd1, 1, &save1)) return -1; - - errno = _hurd_exec (task, file, argv, environ); + + err = _hurd_exec (task, file, argv, environ); if (restorefd (fd0, 0, &save0) || restorefd (fd1, 1, &save1)) return -1; - if (errno) + if (err) { - perror ("_hurd_exec"); - errno = task_terminate (task); - if (errno) - perror ("task_terminate"); + error (0, err, "_hurd_exec"); + err = task_terminate (task); + if (err) + error (0, err, "task_terminate"); } mach_port_deallocate (mach_task_self (), task); } mach_port_deallocate (mach_task_self (), file); + errno = err; return pid; } } @@ -183,7 +193,7 @@ command (int argc, char **argv) bg = !strcmp (argv[argc - 1], "&"); if (bg) argv[--argc] = NULL; - + start = 0; for (i = 1; i < argc; ++i) if (! strcmp (argv[i], "|")) @@ -192,7 +202,7 @@ command (int argc, char **argv) argv[i] = NULL; if (pipe (fds)) { - perror ("pipe"); + error (0, errno, "pipe"); return; } pid = run (argv + start, fd0, fds[1]); @@ -213,7 +223,7 @@ command (int argc, char **argv) int -main () +main (int argc, char *argv[]) { char *linebuf = NULL; size_t linebufsize = 0; @@ -228,15 +238,15 @@ main () mach_port_t hostp, masterd; err = proc_getprivports (proc, &hostp, &masterd); assert (!err); - + err = device_open (masterd, D_WRITE|D_READ, "console", &outp); assert (!err); - + stdin = mach_open_devstream (outp, "r"); stdout = stderr = mach_open_devstream (outp, "w+"); } #endif - + /* Kludge to give boot a port to the auth server. */ exec_init (getdport (0), getauth (), MACH_PORT_NULL, MACH_MSG_TYPE_COPY_SEND); @@ -266,7 +276,7 @@ main () { if (feof (stdin)) return 0; - perror ("getline"); + error (0, errno, "getline"); continue; } @@ -321,7 +331,7 @@ main () if (argc != 2) fprintf (stderr, "Usage: cd DIRECTORY\n"); else if (chdir (argv[1])) - perror ("chdir"); + error (0, errno, "chdir"); } else if (!strcmp (argv[0], "exec")) { @@ -343,7 +353,7 @@ main () if (execv (program, &argv[1]) == 0) fprintf (stderr, "execv (%s) returned 0!\n", program); else - perror ("execv"); + error (0, errno, "execv"); } } else if (!strcmp (argv[0], "setenv")) @@ -351,7 +361,7 @@ main () if (argc != 3) fprintf (stderr, "Usage: setenv VAR VALUE\n"); else if (setenv (argv[1], argv[2], 1)) - perror ("setenv"); + error (0, errno, "setenv"); } else if (!strcmp (argv[0], "fork")) { @@ -359,7 +369,7 @@ main () switch (pid) { case -1: - perror ("fork"); + error (0, errno, "fork"); break; case 0: printf ("I am the child, PID %d.\n", (int) getpid ()); @@ -377,5 +387,3 @@ main () fflush (stderr); } } - - |