File: startprog.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-10
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 56,880 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,429; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,701; csh: 3,928; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (210 lines) | stat: -rw-r--r-- 6,067 bytes parent folder | download | duplicates (6)
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
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/
#include <stdio.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h> 
#include <errno.h>

#include "uti/sge_rmon.h"
#include "uti/sge_unistd.h"
#include "uti/sge_log.h"
#include "uti/sge_time.h"
#include "uti/sge_prog.h"
#include "uti/sge_string.h"

#include "basis_types.h"
#include "startprog.h"
#include "msg_daemons_common.h"

#if defined(SOLARIS)
#   include "sge_smf.h"
#endif

static int do_wait(pid_t);

/*-----------------------------------------------------------------------
 * startprog
 * Start Sge program and wait until it exits. 
 * Usually it will be only useful for daemons, since it blocks the caller
 * until the child exits
 * Use "argv0" or "path" to build path to program
 * Note:  conf-struct may be uninitialized at this point
 * return 0 if ok
 *       -1 if fork() or exec() failed or child died through signal
 *       -2 if executable is not stat()able
 *       >0 the exit status of the child 
 *       exit status 8 is reserved for unsuccesfull exec() 
 *-----------------------------------------------------------------------*/
int startprog(char *argv0, char *path, char *name, ...)
{
 SGE_STRUCT_STAT sb;
 char prog_path[SGE_PATH_MAX];
 pid_t pid;
 int ret;
 char *ptr;
 va_list argnp;
 char *argv[256];
 int i;
 char *str;
 dstring ds;
 char buffer[128];
#if defined(SOLARIS)
 int err_length = 256;
 char err_str[err_length];
#endif

 DENTER(TOP_LAYER, "startprog");

 sge_dstring_init(&ds, buffer, sizeof(buffer));

 va_start(argnp, name);

 for (i=0; i<256; i++)
   argv[i] = NULL;
 
 for (i=1; i<256 && (str = va_arg(argnp, char*)); i++) {
   DPRINTF(("argv[%d] %s\n", i, str));   
   argv[i] = str;
 }   
 va_end(argnp);

 /* Check with $SGE_ROOT/bin/arch if argv0 == NULL */
 if (argv0) {
   sge_strlcpy(prog_path, argv0, sizeof(prog_path));
   if ((ptr = strrchr(prog_path, '/'))) {
       ptr++;
       *ptr = '\0';
       sge_strlcpy(prog_path, name, sizeof(prog_path));
       if (SGE_STAT(prog_path, &sb)) {
          ERROR((SGE_EVENT, MSG_FILE_STATFAILED_SS, 
               prog_path, strerror(errno)));
          DRETURN(-2);
       }
   } else {
      sge_strlcpy(prog_path, name, sizeof(prog_path));
   }
 } else {
    if (!path) {
       DRETURN(-2);
    }
    snprintf(prog_path, sizeof(prog_path), "%s/%s/%s", path, sge_get_arch(), name);
    if (SGE_STAT(prog_path, &sb)) {
       snprintf(prog_path, sizeof(prog_path), "%s/%s", path, name);
       if (SGE_STAT(prog_path, &sb)) {
          ERROR((SGE_EVENT, MSG_FILE_STATFAILED_SS, prog_path, strerror(errno)));
          DRETURN(-2);
       }
    }
 }

 argv[0] = prog_path;

 WARNING((SGE_EVENT, MSG_STARTUP_STARTINGPROGRAMMX_S, prog_path));

#if defined(SOLARIS)
 pid = sge_smf_contract_fork(err_str, err_length);
 if (pid < -1 && strlen(err_str) > 0) {
	 /* Print additional SMF related error message */
	 ERROR((SGE_EVENT, MSG_SMF_STARTPROG_FORK_FAILED_S, err_str));
 }
#else
 pid = fork();
#endif
 if (pid < 0) {
   ERROR((SGE_EVENT, MSG_PROC_CANTFORKPROCESSTOSTARTX_S, prog_path));
   DRETURN(-1);
 } else if (pid == 0) {
   /* child */
   if (getenv("SGE_DEBUG_LEVEL")) {
      putenv("SGE_DEBUG_LEVEL=0 0 0 0 0 0 0 0");
   }
   execvp(prog_path, argv);
   DEXIT;
   exit(8);
 } else {
    /* parent */
    ret = do_wait(pid);
    if (ret == -1) {
       CRITICAL((SGE_EVENT, MSG_PROC_CANTEXECPROCESSORPROCESSDIEDTHROUGHSIGNALX_S, prog_path));
    } else if (ret > 0) {
       CRITICAL((SGE_EVENT, MSG_PROC_CANTSTARTPROCESSX_S, prog_path));     
    }
    DRETURN(ret);
 }
 /* should never be reached */
 DRETURN(-1);
} 

/*-----------------------------------------------------------------------
 * do_wait
 * wait for child with given pid
 * return >= 0 exit status of child
 *       -1 if child exited with exit status 8 (exec failed)
 *          or child died through signal
 *-----------------------------------------------------------------------*/
static int do_wait(
pid_t pid 
) {
   pid_t npid;
   int status, exit_status, ret;

   DENTER(TOP_LAYER, "do_wait");

   /* This loop only ends if the process exited normally or
    * died through signal
    */
   do {
      npid = waitpid(pid, &status, 0);
   } while ((npid <= 0) || (!WIFSIGNALED(status) && !WIFEXITED(status)) ||
            (npid != pid));

   if (WIFEXITED(status))
      exit_status = WEXITSTATUS(status);
   else if (WIFSIGNALED(status))
      exit_status = 8;
   else {
      ERROR((SGE_EVENT, SFNMAX, MSG_PROC_WAITPIDRETURNEDUNKNOWNSTATUS));
      exit_status = 8;
   }
 
   ret = exit_status == 8 ? -1 : exit_status;
   
   DPRINTF(("exit status of child: %d\n",  ret));

   DRETURN(ret);
}