/[LeafOK_CVS]/pvpgn-1.7.4/src/bnetd/runprog.c
ViewVC logotype

Contents of /pvpgn-1.7.4/src/bnetd/runprog.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1.1.1 - (show annotations) (vendor branch)
Tue Jun 6 03:41:37 2006 UTC (19 years, 9 months ago) by sysadm
Branch: GNU, MAIN
CVS Tags: pvpgn_1-7-4-0_MIL, arelease, HEAD
Changes since 1.1: +0 -0 lines
Content type: text/x-csrc
no message

1 /*
2 * Copyright (C) 2000 Ross Combs (rocombs@cs.nmsu.edu)
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
6 * as published by the Free Software Foundation; either version 2
7 * of 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 License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"
19 #include <stdio.h>
20 #ifdef HAVE_STDDEF_H
21 # include <stddef.h>
22 #else
23 # ifndef NULL
24 # define NULL ((void *)0)
25 # endif
26 #endif
27 #ifdef STDC_HEADERS
28 # include <stdlib.h>
29 #endif
30 #ifdef HAVE_STRING_H
31 # include <string.h>
32 #endif
33 #include <errno.h>
34 #include "compat/strerror.h"
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include "compat/stdfileno.h"
39 #ifdef HAVE_SYS_TYPES_H
40 # include <sys/types.h>
41 #endif
42 #ifdef HAVE_SYS_WAIT_H
43 # include <sys/wait.h>
44 #endif
45 #include "common/eventlog.h"
46 #include "runprog.h"
47 #include "common/setup_after.h"
48
49
50 #ifdef DO_SUBPROC
51 static pid_t currpid=0;
52 #endif
53
54
55 extern FILE * runprog_open(char const * command)
56 {
57 #ifndef DO_SUBPROC
58 return NULL; /* always fail */
59 #else
60 int fds[2];
61 FILE * pp;
62
63 if (!command)
64 {
65 eventlog(eventlog_level_error,__FUNCTION__,"got NULL command");
66 return NULL;
67 }
68
69 if (pipe(fds)<0)
70 {
71 eventlog(eventlog_level_error,__FUNCTION__,"could not create pipe (pipe: %s)",pstrerror(errno));
72 return NULL;
73 }
74
75 switch ((currpid = fork()))
76 {
77 case 0:
78 close(fds[0]);
79
80 close(STDINFD);
81 close(STDOUTFD);
82 close(STDERRFD);
83 /* FIXME: we should close all other fds to make sure the program doesn't use them.
84 For now, leave it alone because we would either have to keep track of them all
85 or do a for for (fd=0; fd<BIGNUMBER; fd++) close(fd); loop :( */
86
87 /* assume prog doesn't use stdin */
88 if (fds[1]!=STDOUTFD)
89 dup2(fds[1],STDOUTFD);
90 if (fds[1]!=STDERRFD)
91 dup2(fds[1],STDERRFD);
92 if (fds[1]!=STDOUTFD && fds[1]!=STDERRFD)
93 close(fds[1]);
94
95 if (execlp(command,command,(char *)NULL)<0)
96 eventlog(eventlog_level_error,__FUNCTION__,"could not execute \"%s\" (execlp: %s)",command,pstrerror(errno));
97
98 exit(127); /* popen exec failure code */
99
100 case -1:
101 eventlog(eventlog_level_error,__FUNCTION__,"could not fork (fork: %s)",pstrerror(errno));
102 close(fds[0]);
103 close(fds[1]);
104 return NULL;
105
106 default:
107 close(fds[1]);
108
109 if (!(pp = fdopen(fds[0],"r")))
110 {
111 eventlog(eventlog_level_error,__FUNCTION__,"could not streamify output (fdopen: %s)",pstrerror(errno));
112 close(fds[0]);
113 return NULL;
114 }
115 }
116
117 return pp;
118 #endif
119 }
120
121
122 extern int runprog_close(FILE * pp)
123 {
124 #ifndef DO_SUBPROC
125 return -1; /* always fail */
126 #else
127 int status;
128 pid_t pid;
129
130 if (!pp)
131 {
132 eventlog(eventlog_level_error,__FUNCTION__,"got NULL pp");
133 return -1;
134 }
135
136 if (fclose(pp)<0)
137 {
138 eventlog(eventlog_level_error,__FUNCTION__,"could not close process (fclose: %s)",pstrerror(errno));
139 return -1;
140 }
141
142 for (;;)
143 {
144 # ifdef HAVE_WAITPID
145 pid = waitpid(currpid,&status,0);
146 # else
147 # ifdef HAVE_WAIT
148 pid = wait(&status);
149 if (pid!=currpid)
150 continue;
151 # else
152 return 0; /* assume program succeeded and hope that SIGCHLD handles the zombie */
153 # endif
154 # endif
155 if (pid!=-1)
156 return status;
157 if (errno!=EINTR)
158 return 0;
159 }
160 #endif
161 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1