xref: /onnv-gate/usr/src/tools/cscope-fast/exec.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28*0Sstevel@tonic-gate  * Use is subject to license terms.
29*0Sstevel@tonic-gate  */
30*0Sstevel@tonic-gate 
31*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*0Sstevel@tonic-gate 
33*0Sstevel@tonic-gate /*
34*0Sstevel@tonic-gate  *	cscope - interactive C symbol cross-reference
35*0Sstevel@tonic-gate  *
36*0Sstevel@tonic-gate  *	process execution functions
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include "global.h"
40*0Sstevel@tonic-gate #include <sys/types.h>
41*0Sstevel@tonic-gate #include <sys/wait.h>
42*0Sstevel@tonic-gate #include <stdarg.h>
43*0Sstevel@tonic-gate #include <unistd.h>
44*0Sstevel@tonic-gate #include <stdlib.h>
45*0Sstevel@tonic-gate #include <stdio.h>
46*0Sstevel@tonic-gate #include <libgen.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #define	getdtablesize()	_NFILE
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate #define	MAXARGS	100	/* maximum number of arguments to executed command */
51*0Sstevel@tonic-gate 
52*0Sstevel@tonic-gate pid_t	childpid;	/* child's process ID */
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static	SIGTYPE	(*oldsigquit)();	/* old value of quit signal */
55*0Sstevel@tonic-gate static	SIGTYPE	(*oldsigtstp)();	/* old value of terminal stop signal */
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate static pid_t myfork(void);
58*0Sstevel@tonic-gate static int join(pid_t p);
59*0Sstevel@tonic-gate 
60*0Sstevel@tonic-gate /*
61*0Sstevel@tonic-gate  * execute forks and executes a program or shell script, waits for it to
62*0Sstevel@tonic-gate  * finish, and returns its exit code.
63*0Sstevel@tonic-gate  */
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate /*VARARGS0*/
66*0Sstevel@tonic-gate int
execute(char * path,...)67*0Sstevel@tonic-gate execute(char *path, ...)
68*0Sstevel@tonic-gate {
69*0Sstevel@tonic-gate 	va_list	ap;
70*0Sstevel@tonic-gate 	char	*args[MAXARGS + 1];
71*0Sstevel@tonic-gate 	int	exitcode;
72*0Sstevel@tonic-gate 	int	i;
73*0Sstevel@tonic-gate 	char	msg[MSGLEN + 1];
74*0Sstevel@tonic-gate 	pid_t	p;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	/* fork and exec the program or shell script */
77*0Sstevel@tonic-gate 	exitcurses();
78*0Sstevel@tonic-gate 	if ((p = myfork()) == 0) {
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate 		/* close all files except stdin, stdout, and stderr */
81*0Sstevel@tonic-gate 		for (i = 3; i < getdtablesize() && close(i) == 0; ++i) {
82*0Sstevel@tonic-gate 			;
83*0Sstevel@tonic-gate 		}
84*0Sstevel@tonic-gate 		/* execute the program or shell script */
85*0Sstevel@tonic-gate 		va_start(ap, path);
86*0Sstevel@tonic-gate 		for (i = 0; i < MAXARGS &&
87*0Sstevel@tonic-gate 		    (args[i] = va_arg(ap, char *)) != NULL; ++i) {
88*0Sstevel@tonic-gate 		}
89*0Sstevel@tonic-gate 		va_end(ap);
90*0Sstevel@tonic-gate 		args[i] = NULL;			/* in case MAXARGS reached */
91*0Sstevel@tonic-gate 		args[0] = basename(args[0]);
92*0Sstevel@tonic-gate 		(void) execvp(path, args);	/* returns only on failure */
93*0Sstevel@tonic-gate 		(void) sprintf(msg, "\ncscope: cannot execute %s", path);
94*0Sstevel@tonic-gate 		(void) perror(msg);	/* display the reason */
95*0Sstevel@tonic-gate 		askforreturn();	 /* wait until the user sees the message */
96*0Sstevel@tonic-gate 		exit(1);		/* exit the child */
97*0Sstevel@tonic-gate 	} else {
98*0Sstevel@tonic-gate 		exitcode = join(p);	/* parent */
99*0Sstevel@tonic-gate 	}
100*0Sstevel@tonic-gate 	if (noacttimeout) {
101*0Sstevel@tonic-gate 		(void) fprintf(stderr,
102*0Sstevel@tonic-gate 		    "cscope: no activity time out--exiting\n");
103*0Sstevel@tonic-gate 		myexit(SIGALRM);
104*0Sstevel@tonic-gate 	}
105*0Sstevel@tonic-gate 	entercurses();
106*0Sstevel@tonic-gate 	return (exitcode);
107*0Sstevel@tonic-gate }
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate /* myfork acts like fork but also handles signals */
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate static pid_t
myfork(void)112*0Sstevel@tonic-gate myfork(void)
113*0Sstevel@tonic-gate {
114*0Sstevel@tonic-gate 	pid_t	p;		/* process number */
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	oldsigtstp = signal(SIGTSTP, SIG_DFL);
117*0Sstevel@tonic-gate 	/* the parent ignores the interrupt and quit signals */
118*0Sstevel@tonic-gate 	if ((p = fork()) > 0) {
119*0Sstevel@tonic-gate 		childpid = p;
120*0Sstevel@tonic-gate 		oldsigquit = signal(SIGQUIT, SIG_IGN);
121*0Sstevel@tonic-gate 	}
122*0Sstevel@tonic-gate 	/* so they can be used to stop the child */
123*0Sstevel@tonic-gate 	else if (p == 0) {
124*0Sstevel@tonic-gate 		(void) signal(SIGINT, SIG_DFL);
125*0Sstevel@tonic-gate 		(void) signal(SIGQUIT, SIG_DFL);
126*0Sstevel@tonic-gate 		(void) signal(SIGHUP, SIG_DFL);	/* restore hangup default */
127*0Sstevel@tonic-gate 	}
128*0Sstevel@tonic-gate 	/* check for fork failure */
129*0Sstevel@tonic-gate 	if (p == -1) {
130*0Sstevel@tonic-gate 		myperror("Cannot fork");
131*0Sstevel@tonic-gate 	}
132*0Sstevel@tonic-gate 	return (p);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate /* join is the compliment of fork */
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate static int
join(pid_t p)138*0Sstevel@tonic-gate join(pid_t p)
139*0Sstevel@tonic-gate {
140*0Sstevel@tonic-gate 	int	status;
141*0Sstevel@tonic-gate 	pid_t	w;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	/* wait for the correct child to exit */
144*0Sstevel@tonic-gate 	do {
145*0Sstevel@tonic-gate 		w = wait(&status);
146*0Sstevel@tonic-gate 	} while (p != -1 && w != p);
147*0Sstevel@tonic-gate 	childpid = 0;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/* restore signal handling */
150*0Sstevel@tonic-gate 	(void) signal(SIGQUIT, oldsigquit);
151*0Sstevel@tonic-gate 	(void) signal(SIGTSTP, oldsigtstp);
152*0Sstevel@tonic-gate 	/* return the child's exit code */
153*0Sstevel@tonic-gate 	return (status >> 8);
154*0Sstevel@tonic-gate }
155