Debian Codegraph @

libsrc/os/unix/osp.c

raw
1 /*===========================================================================
2 Copyright (C) 1995-2009 European Southern Observatory (ESO)
3
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of
7 the License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with this program; if not, write to the Free
16 Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
17 MA 02139, USA.
18
19 Correspondence concerning ESO-MIDAS should be addressed as follows:
20 Internet e-mail: [email protected]
21 Postal address: European Southern Observatory
22 Data Management Division
23 Karl-Schwarzschild-Strasse 2
24 D 85748 Garching bei Muenchen
25 GERMANY
26 ===========================================================================*/
27
28 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29 .TYPE Module
30 .NAME osp
31 .LANGUAGE C
32 .AUTHOR IPG-ESO Garching
33 .CATEGORY Host operating system interfaces, Process control.
34 .COMMENTS These routines give the basic capabilitites for controlling
35 processes.
36 Processes are addressed by an integer number called process
37 identifier. The process activation function defines the file
38 with the executable code (possibly in a remote node) and the
39 process name, the routine returns the process identification
40 number to be used in further references. Processes can be
41 activated or aborted, also the process logical name associated
42 to it can be changed.
43 At process creation, one can redirect the standard input and/or
44 output of the newly created process.
45 Each process has some status information associated to it.
46 The status information is defined as a data structure (tag
47 prstatus) consisting of a process id (pid field),
48 background/foreground flag (bflg field) and name (procname field)
49 for checking the status of the child process.
50 Functions in this module allow to define and retrieve this
51 status information.
52 The routines return always
53 a non-negative integer number on successful return.
54 Otherwise, a value of -1 is set to indicate an error
55 condition and the variable ``oserror'' contains the
56 symbolic error code.
57
58 .VERSION [0.0] 25-Aug-1986 Definition B. Pirenne
59 .VERSION [1.0] 10-Nov-1986 Programmation BP
60 .VERSION [1.5] 26-Jun-1987 Add child process termination control BP
61 .VERSION [1.6] 07-Jul-1987 Allow redefin. of stdin and/or stdout of child
62 .VERSION [1.7] 04-Nov-1987 fix construction of process name
63 in ospcreate K. Banse
64 .VERSION [1.8] 01-Dec-1987 Add child status in proc.
65 struct in private memory I.Suisalu.
66 .VERSION [1.9] 25-May-1988 Cosmetic changes C. Guirao
67 .VERSION [1.10]14-OCT-1988 The child signal SIGCLD get SIG_DFL C.G.
68 .VERSION [1.11]19-Oct-1989 Conceal interal subroutines
69 .VERSION [1.12]25-Apr-1991 Added signal.h include + VOID
70 .REMARK =================== System V version ================
71
72 .REMARK On the management and information retrieval of the processes:
73 In order to be able to retrieve information on the processes
74 and in order to give a name to it, a special structure was
75 built(see "osparms.h"). Array of this structures are hold in
76 private memory of the calling program.
77
78 .VERSION [1.13]20-Jan-1992 CG. SHELL variable is used to spawn processes
79 .VERSION [1.14]14-Apr-1992 CG. Change 1.14 is removed. The Bourne-Shell
80 is the only one that do change the environment
81 .VERSION [2.0]13-Jul-1992 CG. chcatch is VOID. CG
82 .VERSION [2.1]14-Jul-1992 VOID is now called VOIDSIG. CG.
83
84 090324 last modif
85 -------------------------------------------------------------------------*/
86 /*
87 * Define _POSIX_SOURCE to indicate
88 * that this is a POSIX program
89 */
90 #define _POSIX_SOURCE 1
91
92 #include <sys/types.h>
93 #include <sys/wait.h>
94 #include <stdio.h>
95 #include <stdlib.h>
96 #include <string.h>
97 #include <signal.h>
98 #include <errno.h>
99 #include <unistd.h>
100 #include <proto_os.h> /* ANSI-C prototyping */
101
102
103 #ifdef OSERROR_D /* oserror already defined (Silicon G)*/
104 #define oserror midaserror
105 #endif
106
107 static int pid;
108 extern char *oserrmsg;
109 extern int oserror;
110
111 #ifdef __STDC__
112 static void alarm_handler(int sig)
113 #else
114 static void alarm_handler(sig)
115 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
116 .PURPOSE dummy routine to catch SIGALRM avoiding killing the process
117 -----------------------------------------------------------*/
118 int sig;
119 #endif
120 {
121 kill (pid, SIGKILL);
122 }
123
124 int ospcreate(phname, procname, backgrd, stin, stout, time_out)
125 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
126 .PURPOSE Activates a specified process whose name is phname and logical process
127 name is procname. (this last name can be modify using osprename)
128 Enviroment pointer also passed to the process. One argument (backgrd)
129 defines if the process will run in background/foreground.
130 Allows redirection of the process' standard input and/or output to
131 specified files if the corresponding file descriptors returned from a
132 previous open (osd-, osa-, osx-, ...) statement are given as last
133 parameters to this call.
134 .RETURNS Upon successful completion the
135 .RETURNS process identification is returned, otherwise -1.
136 .REMARKS System dependencies:
137 -- UNIX: fork(2), execl(2),open(2), close(2),
138 getuid(2), close(2), dup(2),
139 ------------------------------------------------------------*/
140
141 char *phname; /* IN : physical filename with executable code */
142 char *procname; /* IN : process logical name . OBSOLETE*/
143 int backgrd; /* Put backgound or not */
144 int stin; /* IN : file descr. of the new child's std. input */
145 int stout; /* IN : file descr. of the new child's std. output */
146 unsigned int time_out; /* IN : amount of seconds for timeout */
147 {
148 int chstat;
149 int dupfd;
150 struct sigaction ignore, saveintr, savequit;
151 char *ptr_bgr;
152
153 ignore.sa_handler = SIG_IGN;
154 sigemptyset(&ignore.sa_mask);
155 ignore.sa_flags = 0;
156 if (sigaction(SIGINT,&ignore,&saveintr) != 0) { oserror=errno; return(-1); };
157 if (sigaction(SIGQUIT,&ignore,&savequit) != 0) { oserror=errno; return(-1); };
158
159 /*
160 * In the parent process, fork returns the
161 * process number of the child. In the
162 * child process, 0 is returned.
163 */
164 switch (pid = fork()) {
165 case -1:
166 oserror =errno;
167 return(-1);
168 case 0: /* In child process */
169 /* Restore signals for child process */
170 (void) sigaction(SIGINT, &saveintr, (struct sigaction *)NULL);
171 (void) sigaction(SIGQUIT, &savequit, (struct sigaction *)NULL);
172 if (stin != 0) { /* redirected input of child process */
173 (void) close (0); /* close old standard input */
174 dupfd = dup(stin); /* duplicate file descriptor */
175 (void) close(stin); /* close unnecessary old file descr. */
176 }
177
178 if (stout != 1) { /* redirected output of child process */
179 (void) close (1); /* close old standard output */
180 dupfd = dup(stout); /* duplicate file descriptor */
181 (void) close(stout); /* close unnecessary old file descr. */
182 }
183
184 if (*phname == '$') { /* system command */
185 phname ++;
186 /* if background, ignore SIGINT and SIGQUIT */
187 if ( (ptr_bgr = strrchr(phname,'&')) != NULL) {
188 ptr_bgr++;
189 for ( ; *ptr_bgr && *ptr_bgr == ' '; ptr_bgr++) ;
190 if (!*ptr_bgr) {
191 sigaction(SIGINT,&ignore,&saveintr);
192 sigaction(SIGQUIT,&ignore,&savequit);
193 }
194 }
195 (void) execl("/bin/sh","sh","-c",phname,(char *)0);
196 }
197 else { /* Execute the process */
198 if (strchr(phname,' ') == (char *)0)
199 (void)execl(phname,phname,(char *)0);
200 else { /* the phname contains arguments */
201 char *arg[9];
202 char *log_name, *blank_ptr;
203 int i;
204 log_name = malloc ((size_t)strlen(phname)+1);
205 strcpy(log_name,phname);
206 arg[0]=log_name;
207 for (i=0; i < 8 ; i++) {
208 if ( !(blank_ptr = strchr(arg[i],' '))) {
209 arg[i+1] = (char *)0;
210 break;
211 }
212 while ( *blank_ptr == ' ' ) *blank_ptr++ = '\0';
213 arg[i+1] = blank_ptr;
214 }
215 (void)execl(arg[0],arg[0],arg[1],arg[2],arg[3],arg[4],arg[5],arg[6],arg[7],arg[8],(char *)0);
216 }
217 }
218
219 /* exit with the errno code + eight bit set, to be recognized by chstat */
220 /* Do not print the error. The application could then try in midwork */
221 /* printf("ERROR IN CHILD, errno = %d\n",errno); */
222 exit(0x80 | errno); /* suppress the fork */;
223
224 default: /* In father process */
225 if(!backgrd) {
226 struct sigaction act, oalrm;
227
228 if (time_out != 0) {
229
230 /* Catch the signal ALRM to avoid proccess to terminate */
231 act.sa_handler = alarm_handler;
232 sigemptyset(&act.sa_mask);
233 act.sa_flags = 0;
234 sigaction(SIGALRM,&act,&oalrm);
235
236 /* Set alarm to time_out */
237 (void) alarm(time_out);
238 }
239
240 oserror = 0;
241 while (waitpid(pid, &chstat, 0) != pid) {
242 if (errno != EINTR) {
243 oserror= errno;
244 break;
245 }
246 }
247
248 if (time_out != 0) {
249 (void) alarm(0);
250 sigaction(SIGALRM,&oalrm,&act);
251 }
252
253 /* If the execl failed */
254 if ( (chstat & 0x8000) == 0x8000 ) oserror = ( (chstat >> 8 ) & 0x7f );
255
256 if (!oserror) {
257 if ( WIFEXITED(chstat) ) oserror = WEXITSTATUS(chstat);
258 if ( WIFSIGNALED(chstat) ) {
259 int int_signal;
260 char errmsg[80];
261 int_signal = WTERMSIG(chstat);
262 sprintf(errmsg,"Child killed by signal %d",int_signal);
263 oserrmsg = errmsg;
264 oserror = -1;
265 }
266 }
267 }
268 /* restore previous signal actions */
269 sigaction(SIGINT,&saveintr,(struct sigaction *)NULL);
270 sigaction(SIGQUIT,&savequit,(struct sigaction *)NULL);
271 if (oserror) pid = -1;
272 }
273 return(pid);
274 }
275
276 void ospexit(stat)
277 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
278 .PURPOSE Normal termination of a process.
279 .RETURNS Nothing
280 .REMARKS System dependencies:
281 -- UNIX: exit(2)
282 ------------------------------------------------------------*/
283 int stat;
284 {
285 exit(stat); /* Terminates the process. */
286 }
287
288 int ospwait(time)
289 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
290 .PURPOSE Delays the execution of a process for a time interval.
291 Resolution of time interval is in seconds.
292 .RETURNS Always 0
293 .REMARKS System dependencies:
294 -- UNIX: sleep(2)
295 ------------------------------------------------------------*/
296 unsigned int time; /* IN : time delay in seconds */
297 {
298 sleep(time);
299 return(0);
300 }
301
302 /* CG. I favor to removed this, and use getpid directly
303 * Currently used by gui applications: XSpectra XFilter XEchelle & stella
304 */
305 int osppid()
306 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
307 .PURPOSE Obtains current process identification.
308 .RETURNS The process identification as a integer.
309 -------------------------------------------------------------*/
310 {
311 return(getpid());
312 }