]> andersk Git - moira.git/blame - update/exec_002.c
WIFEXITED(waitb) is true if the script exits even with exit 0.
[moira.git] / update / exec_002.c
CommitLineData
de56407f 1/*
2 * $Source$
3 * $Header$
4 */
546bc43b 5/* (c) Copyright 1988 by the Massachusetts Institute of Technology. */
6/* For copying and distribution information, please see the file */
7/* <mit-copyright.h>. */
de56407f 8
9#ifndef lint
10static char *rcsid_exec_002_c = "$Header$";
11#endif lint
12
546bc43b 13#include <mit-copyright.h>
de56407f 14#include <stdio.h>
8b3ba58e 15#include <errno.h>
c57b6bd3 16#include <sys/types.h>
c91c8276 17#ifdef _AIX
18#undef _BSD
19#endif
de56407f 20#include <sys/wait.h>
c91c8276 21#ifdef _AIX
22#define _BSD 44
23#endif
6143ee87 24#include <signal.h>
c8dc853f 25#include <gdb.h>
2ad0a777 26#include <moira.h>
de56407f 27#include "update.h"
de56407f 28
29extern CONNECTION conn;
8b3ba58e 30extern int code, errno, uid;
31extern char *whoami;
32
984bd8ef 33#if defined(vax) || defined(ibm032)
34#define WEXITSTATUS(waitb) ((waitb).w_retcode)
35#endif
36
de56407f 37
38int
39exec_002(str)
40 char *str;
41{
8a7d0639 42#ifdef POSIX
984bd8ef 43 int waitb;
8a7d0639 44 sigset_t mask,oldmask;
984bd8ef 45#else
de56407f 46 union wait waitb;
8a7d0639 47 int mask;
984bd8ef 48#endif
8a7d0639 49 int n, pid;
de56407f 50
8b3ba58e 51 if (config_lookup("noexec")) {
52 code = EPERM;
53 code = send_object(conn, (char *)&code, INTEGER_T);
54 com_err(whoami, code, "Not allowed to execute");
984bd8ef 55 return(0);
8b3ba58e 56 }
de56407f 57 str += 8;
58 while (*str == ' ')
59 str++;
8a7d0639 60#ifdef POSIX
61 sigemptyset(&mask);
62 sigaddset(&mask,SIGCHLD);
63 sigprocmask(SIG_BLOCK,&mask,&oldmask);
64#else
6143ee87 65 mask = sigblock(sigmask(SIGCHLD));
8a7d0639 66#endif
de56407f 67 pid = fork();
68 switch (pid) {
69 case -1:
70 n = errno;
8a7d0639 71#ifdef POSIX
72 sigprocmask(SIG_UNBLOCK,&oldmask,&mask);
73#else
6143ee87 74 sigsetmask(mask);
8a7d0639 75#endif
de56407f 76 log_priority = log_ERROR;
77 com_err(whoami, errno, ": can't fork to run install script");
78 code = send_object(conn, (char *)&n, INTEGER_T);
79 if (code)
80 exit(1);
984bd8ef 81 return(0);
de56407f 82 case 0:
8b3ba58e 83 if (setuid(uid) < 0) {
84 com_err(whoami, errno, "Unable to setuid to %d\n", uid);
85 exit(1);
86 }
8a7d0639 87#ifdef POSIX
88 sigprocmask(SIG_UNBLOCK,&oldmask,&mask);
89#else
e19df9fb 90 sigsetmask(mask);
8a7d0639 91#endif
de56407f 92 execlp(str, str, (char *)NULL);
93 n = errno;
8a7d0639 94#ifdef POSIX
95 sigprocmask(SIG_UNBLOCK,&oldmask,&mask);
96#else
6143ee87 97 sigsetmask(mask);
8a7d0639 98#endif
de56407f 99 log_priority = log_ERROR;
100 com_err(whoami, n, ": %s", str);
101 (void) send_object(conn, (char *)&n, INTEGER_T);
102 exit(1);
103 default:
104 do {
105 n = wait(&waitb);
106 } while (n != -1 && n != pid);
8a7d0639 107#ifdef POSIX
108 sigprocmask(SIG_UNBLOCK,&oldmask,&mask);
109#else
6143ee87 110 sigsetmask(mask);
8a7d0639 111#endif
112#ifdef POSIX
4aba574b 113 if ( (WIFEXITED(waitb) && (WEXITSTATUS(waitb)!=0)) || WIFSIGNALED(waitb) ) {
114 /* This is not really correct. It will cause teh moira server to
115 report a bogus error message if the script died on a signal.
116 However this is the same thing that occurs in the non-POSIX
117 case, and I don't know how to come up with a useful error based
118 on the signal recieved.
119 */
984bd8ef 120 n = WEXITSTATUS(waitb) + ERROR_TABLE_BASE_sms;
de56407f 121 log_priority = log_ERROR;
984bd8ef 122 com_err(whoami, n, " child exited with status %d",
123 WEXITSTATUS(waitb));
8a7d0639 124#else
125 if (waitb.w_status) {
126 n = waitb.w_retcode + ERROR_TABLE_BASE_sms;
127 log_priority = log_ERROR;
128 com_err(whoami, n, " child exited with status %d",
129 waitb.w_retcode);
130#endif
de56407f 131 code = send_object(conn, (char *)&n, INTEGER_T);
c8dc853f 132 if (code) {
de56407f 133 exit(1);
c8dc853f 134 }
135 } else {
de56407f 136 code = send_ok();
137 if (code)
c8dc853f 138 exit(1);
de56407f 139 }
140 }
141}
This page took 0.676819 seconds and 5 git commands to generate.