main.c

filter: Statementstmt 0.0% 0 / 344 Branchbran 0.0% 0 / 180 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 524 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« PPerl.xs ↑ Summary pass_fd.c »
1 /* pperl - run perl scripts persistently */
2  
3 #include <stdio.h>
4 #include <string.h>
5 #include <stdlib.h>
6 #include <stdarg.h>
7 #include <memory.h>
8 #include <errno.h>
9 #include <sys/types.h>
10 #include <sys/wait.h>
11 #include <sys/socket.h>
12 #include <sys/un.h>
13 #include <sys/stat.h>
14 #include <sys/time.h>
15 #include <fcntl.h>
16 #include <limits.h>
17 #include <signal.h>
18 #include <unistd.h>
19 #include "pperl.h"
20  
21 #include "pass_fd.h" /* the stuff borrowed from stevens */
22  
23 #define DEBUG 1
24  
25 /* must never be less than 3 */
26 #define BUF_SIZE 4096
27  
28  
29 #ifdef ENOBUFS
30 # define NO_BUFSPC(e) ((e) == ENOBUFS || (e) == ENOMEM)
31 #else
32 # define NO_BUFSPC(e) ((e) == ENOMEM)
33 #endif
34  
35 static void Usage( char *pName );
36 static void DecodeParm( char *pArg );
37 static int DispatchCall( char *scriptname, int argc, char **argv );
38  
39 char *pVersion = PPERL_VERSION;
40 char perl_options[1024];
41 extern char **environ;
42 pid_t connected_to;
43 int kill_script = 0;
44 int any_user = 0;
45 int prefork = 5;
46 int maxclients = 100;
47 int path_max;
48 int no_cleanup = 0;
49 FILE *log_fd = NULL;
50  
51 #if DEBUG
52 #define Dx(x) (x)
53 #else
54 #define Dx(x)
55 #endif
56  
57 static
58 0 void Debug( const char * format, ...)
59 {
60 va_list args;
61
62 0 ▶ if (!log_fd)
Branch: 1 branch
BranchTrueFalseTotal
if (!log_fd) 0 0 0
63 0 return;
64
65 0 va_start(args, format);
66 0 vfprintf(log_fd, format, args);
67 0 va_end(args);
68 0 fflush(log_fd);
69 }
70  
71  
72 0 int main( int argc, char **argv )
73 {
74 int i;
75 char *pArg;
76 0 int pperl_section = 0;
77 0 int return_code = 0;
78  
79 0 ▶ if( argc < 2 )
Branch: 1 branch
BranchTrueFalseTotal
if( argc < 2 ) 0 0 0
80 0 Usage( argv[0] );
81
82 #ifdef PATH_MAX
83 0 path_max = PATH_MAX;
84 #else
85 path_max = pathconf (path, _PC_PATH_MAX);
86 if (path_max <= 0) {
87 path_max = 4096;
88 }
89 #endif
90  
91 0 pperl_section = 0;
92 0 ▶ for ( i = 1; i < argc; i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
for ( i = 1; i < argc; i++ ) { 0 0 0
93 0 pArg = argv[i];
94 /* fprintf(stderr, "Parsing arg: %s\n", pArg); */
95 0 ▶ if (*pArg != '-') break;
Branch: 1 branch
BranchTrueFalseTotal
if (*pArg != '-') break; 0 0 0
96 0 ▶ if ( !strcmp(pArg, "-k") || !strcmp(pArg, "--kill") )
Branch: 2 branches
BranchTrueFalseTotal
if ( !strcmp(pArg, "-k") || !strcmp(pArg, "--kill") ) 0 0 0
if ( !strcmp(pArg, "-k") || !strcmp(pArg, "--kill") ) 0 0 0
97 0 kill_script = 1;
98 0 ▶ else if (!strncmp(pArg, "--prefork", 9) ) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp(pArg, "--prefork", 9) ) { 0 0 0
99 int newval;
100 0 ▶ if (pArg[9] == '=') /* "--prefork=20" */
Branch: 1 branch
BranchTrueFalseTotal
if (pArg[9] == '=') /* "--prefork=20" */ 0 0 0
101 0 pArg += 10;
102 else /* "--prefork" "20" */
103 0 pArg = argv[++i];
104  
105 0 newval = atoi(pArg);
106 0 ▶ if (newval > 0) prefork = newval;
Branch: 1 branch
BranchTrueFalseTotal
if (newval > 0) prefork = newval; 0 0 0
107 }
108 0 ▶ else if (!strncmp(pArg, "--logfile", 7) ) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp(pArg, "--logfile", 7) ) { 0 0 0
109 int newval;
110 char *filename;
111 0 ▶ if (pArg[7] == '=') /* --logfile=.... */
Branch: 1 branch
BranchTrueFalseTotal
if (pArg[7] == '=') /* --logfile=.... */ 0 0 0
112 0 pArg += 13;
113 else
114 0 pArg = argv[++i];
115
116 0 filename = pArg;
117 0 newval = atoi(pArg);
118 0 ▶ if (newval == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (newval == 0) { 0 0 0
119 0 fprintf(stderr, "opening log_fd: %s\n", filename);
120 0 log_fd = fopen(filename, "a");
121 0 ▶ if (!log_fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (!log_fd) { 0 0 0
122 0 perror("Cannot open logfile");
123 0 exit(1);
124 }
125 }
126 else {
127 0 log_fd = fdopen(newval, "a");
128 0 ▶ if (!log_fd) {
Branch: 1 branch
BranchTrueFalseTotal
if (!log_fd) { 0 0 0
129 0 perror("fd for --logfile error");
130 0 exit(1);
131 }
132 }
133 }
134 0 ▶ else if (!strncmp(pArg, "--no-cleanup", 12) ) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp(pArg, "--no-cleanup", 12) ) { 0 0 0
135 0 no_cleanup = 1;
136 }
137 0 ▶ else if (!strncmp(pArg, "--maxclients", 12) ) {
Branch: 1 branch
BranchTrueFalseTotal
else if (!strncmp(pArg, "--maxclients", 12) ) { 0 0 0
138 int newval;
139 0 ▶ if (pArg[12] == '=') /* "--maxclients=20" */
Branch: 1 branch
BranchTrueFalseTotal
if (pArg[12] == '=') /* "--maxclients=20" */ 0 0 0
140 0 pArg += 13;
141 else /* "--maxclients" "20" */
142 0 pArg = argv[++i];
143  
144 0 newval = atoi(pArg);
145 0 ▶ if (newval > 0) maxclients = newval;
Branch: 1 branch
BranchTrueFalseTotal
if (newval > 0) maxclients = newval; 0 0 0
146 }
147 0 ▶ else if ( !strcmp(pArg, "-z") || !strcmp(pArg, "--anyuser") )
Branch: 2 branches
BranchTrueFalseTotal
else if ( !strcmp(pArg, "-z") || !strcmp(pArg, "--anyuser") ) 0 0 0
else if ( !strcmp(pArg, "-z") || !strcmp(pArg, "--anyuser") ) 0 0 0
148 0 any_user = 1;
149 0 ▶ else if ( !strcmp(pArg, "-h") || !strcmp(pArg, "--help") )
Branch: 2 branches
BranchTrueFalseTotal
else if ( !strcmp(pArg, "-h") || !strcmp(pArg, "--help") ) 0 0 0
else if ( !strcmp(pArg, "-h") || !strcmp(pArg, "--help") ) 0 0 0
150 0 Usage( NULL );
151 0 ▶ else if ( !strncmp(pArg, "--", 2) )
Branch: 1 branch
BranchTrueFalseTotal
else if ( !strncmp(pArg, "--", 2) ) 0 0 0
152 ; /* do nothing - backward compatibility */
153 else {
154 0 DecodeParm( pArg );
155 }
156 }
157
158 0 i++;
159 0 return_code = DispatchCall( pArg, argc - i, (char**)(argv + i) );
160 0 Dx(Debug("done, returning %d\n", return_code));
161 0 ▶ if (log_fd) fclose(log_fd);
Branch: 1 branch
BranchTrueFalseTotal
if (log_fd) fclose(log_fd); 0 0 0
162 0 return return_code;
163 }
164  
165 0 static void DecodeParm( char *pArg )
166 {
167 0 ▶ if ( (strlen(perl_options) + strlen(pArg) + 1) > 1000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (strlen(perl_options) + strlen(pArg) + 1) > 1000 ) { 0 0 0
168 0 fprintf(stderr, "param list too long. Sorry.");
169 0 exit(1);
170 }
171 0 strcat(perl_options, pArg);
172 0 strcat(perl_options, " ");
173 0 }
174  
175 0 static void Usage( char *pName )
176 {
177 0 printf( "pperl version %s\n", pVersion );
178  
179 0 ▶ if( pName == NULL )
Branch: 1 branch
BranchTrueFalseTotal
if( pName == NULL ) 0 0 0
180 {
181 0 printf( "Usage: pperl [options] filename\n" );
182 }
183 else
184 {
185 0 printf( "Usage: %.255s [options] filename\n", pName );
186 }
187 0 printf("perl options are passed to your perl executable (see the perlrun man page).\n"
188 "pperl options control the persistent perl behaviour\n"
189 "\n"
190 "PPerl Options:\n"
191 " -k or --kill Kill the currently running pperl for that script\n"
192 " -h or --help This page\n"
193 " --prefork The number of child processes to prefork (default=5)\n"
194 " --maxclients The number of client connections each child\n"
195 " will process (default=100)\n"
196 " -z or --anyuser Allow any user (after the first) to access the socket\n"
197 " WARNING: This has severe security implications. Use\n"
198 " at your own risk\n"
199 " --no-cleanup Skip the cleanup stage at the end of running your script\n"
200 " this may make your code run faster, but if you forget\n"
201 " to close files then they will remain unflushed and unclosed\n"
202 );
203 0 exit( 1 );
204 }
205  
206 static void *
207 0 my_malloc(size_t size)
208 {
209 0 void *mem = malloc(size);
210 0 ▶ if (mem == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (mem == NULL) { 0 0 0
211 0 perror("malloc failed");
212 0 exit(-1);
213 }
214 0 return mem;
215 }
216  
217 /* make socket name from scriptname, switching / for _ */
218 static char *
219 0 MakeSockName(char * scriptname )
220 {
221 char * sockname;
222 char * save;
223 /* strict C compilers can't/won't do char foo[variant]; */
224 0 char *fullpath = my_malloc(path_max);
225 0 int i = 0;
226  
227 0 ▶ if (realpath(scriptname, fullpath) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (realpath(scriptname, fullpath) == NULL) { 0 0 0
228 0 perror("pperl: resolving full pathname to script failed");
229 0 exit(1);
230 }
231 0 Dx(Debug("realpath returned: %s\n", fullpath));
232 /* Ugh. I am a terrible C programmer! */
233 0 sockname = my_malloc(strlen(P_tmpdir) + strlen(fullpath) + 3);
234 0 save = sockname;
235 0 sprintf(sockname, "%s/", P_tmpdir);
236 0 sockname += strlen(P_tmpdir) + 1;
237 0 ▶ while (fullpath[i] != '\0') {
Branch: 1 branch
BranchTrueFalseTotal
while (fullpath[i] != '\0') { 0 0 0
238 0 ▶ if (fullpath[i] == '/') {
Branch: 1 branch
BranchTrueFalseTotal
if (fullpath[i] == '/') { 0 0 0
239 0 *sockname = '_';
240 }
241 0 ▶ else if (fullpath[i] == '.') {
Branch: 1 branch
BranchTrueFalseTotal
else if (fullpath[i] == '.') { 0 0 0
242 0 *sockname = '_';
243 }
244 else {
245 0 *sockname = fullpath[i];
246 }
247 0 sockname++; i++;
248 }
249 0 *sockname = '\0';
250 0 free(fullpath);
251 0 return save;
252 }
253  
254  
255 static void
256 0 sig_handler(int sig)
257 {
258 0 kill(connected_to, sig);
259 0 signal(sig, sig_handler);
260 /* skreech_to_a_halt++; */
261 0 }
262  
263 static int handle_socket(int sd, int argc, char **argv );
264 0 static int DispatchCall( char *scriptname, int argc, char **argv )
265 {
266 register int i, sd, len;
267 int error_number;
268 ssize_t readlen;
269 struct sockaddr_un saun;
270 struct stat stat_buf;
271 struct stat sock_stat;
272 char *sock_name;
273 char buf[BUF_SIZE];
274 0 int respawn_script = 0;
275 0 sd = 0;
276  
277 /* create socket name */
278 0 Dx(Debug("pperl: %s\n", scriptname));
279 0 sock_name = MakeSockName(scriptname);
280 0 Dx(Debug("got socket: %s\n", sock_name));
281  
282 0 ▶ if (!stat(sock_name, &sock_stat) && !stat(scriptname, &stat_buf)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!stat(sock_name, &sock_stat) && !stat(scriptname, &stat_buf)) { 0 0 0
if (!stat(sock_name, &sock_stat) && !stat(scriptname, &stat_buf)) { 0 0 0
283 0 ▶ if (stat_buf.st_mtime >= sock_stat.st_mtime) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat_buf.st_mtime >= sock_stat.st_mtime) { 0 0 0
284 0 respawn_script = 1;
285 0 Dx(Debug("respawning slave - top level script changed\n"));
286 }
287 }
288
289 0 ▶ if (kill_script || respawn_script) {
Branch: 2 branches
BranchTrueFalseTotal
if (kill_script || respawn_script) { 0 0 0
if (kill_script || respawn_script) { 0 0 0
290 int pid_fd, sock_name_len;
291 char *pid_file;
292 0 pid_t pid = 0;
293
294 0 respawn_script = 0; /* reset so we can use it later :-) */
295
296 0 sock_name_len = strlen(sock_name);
297 0 pid_file = my_malloc(sock_name_len + 5);
298 0 strncpy(pid_file, sock_name, sock_name_len);
299 0 pid_file[sock_name_len] = '.';
300 0 pid_file[sock_name_len+1] = 'p';
301 0 pid_file[sock_name_len+2] = 'i';
302 0 pid_file[sock_name_len+3] = 'd';
303 0 pid_file[sock_name_len+4] = '\0';
304
305 0 Dx(Debug("opening pid_file: %s\n", pid_file));
306 0 pid_fd = open(pid_file, O_RDONLY);
307 0 ▶ if (pid_fd == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid_fd == -1) { 0 0 0
308 0 Dx(Debug("Cannot open pid file (perhaps PPerl wasn't running for that script?)\n"));
309 0 write(1, "No process killed - no pid file\n", 32);
310 0 goto killed;
311 }
312
313 0 readlen = read(pid_fd, buf, BUF_SIZE);
314 0 ▶ if (readlen == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (readlen == -1) { 0 0 0
315 0 perror("pperl: nothing in pid file?");
316 0 goto killed;
317 }
318 0 buf[readlen] = '\0';
319
320 0 close(pid_fd);
321
322 0 pid = atoi(buf);
323 0 Dx(Debug("got pid %d (%s)\n", pid, buf));
324 0 ▶ if (kill(pid, SIGINT) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill(pid, SIGINT) == -1) { 0 0 0
325 0 ▶ if (errno == ESRCH) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno == ESRCH) { 0 0 0
326 0 perror("pperl kill");
327 0 Dx(Debug("Process didn't exist. Unlinking %s and %s\n", pid_file, sock_name));
328 0 unlink(pid_file);
329 0 unlink(sock_name);
330 }
331 else {
332 0 perror("pperl: could not kill process");
333 }
334 }
335
336 0 free(pid_file);
337  
338 0 killed:
339
340 0 ▶ if (kill_script) {
Branch: 1 branch
BranchTrueFalseTotal
if (kill_script) { 0 0 0
341 0 free(sock_name); /* Hmm, should probably do this everywhere else we return too */
342 0 return 0;
343 }
344
345 0 ▶ if (pid != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pid != 0) { 0 0 0
346 /* cheesy - let the child go away proper */
347 0 ▶ while (!kill(pid, 0)) {}
Branch: 1 branch
BranchTrueFalseTotal
while (!kill(pid, 0)) {} 0 0 0
348 }
349 }
350
351 0 ▶ for (i = 0; i < 10; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 10; i++) { 0 0 0
352 0 sd = socket(PF_UNIX, SOCK_STREAM, PF_UNSPEC);
353 0 ▶ if (sd != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (sd != -1) { 0 0 0
354 0 break;
355 }
356 0 ▶ else if (NO_BUFSPC(errno)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (NO_BUFSPC(errno)) { 0 0 0
else if (NO_BUFSPC(errno)) { 0 0 0
357 0 sleep(1);
358 }
359 else {
360 0 perror("pperl: Couldn't create socket");
361 0 return 1;
362 }
363 }
364  
365 0 saun.sun_family = PF_UNIX;
366 0 strcpy(saun.sun_path, sock_name);
367  
368 0 len = sizeof(saun.sun_family) + strlen(saun.sun_path) + 1;
369  
370 0 Dx(Debug("%d connecting\n", getpid()));
371  
372 0 ▶ if (stat((const char*)sock_name, &stat_buf)) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat((const char*)sock_name, &stat_buf)) { 0 0 0
373 0 ▶ if (errno == ENOENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (errno == ENOENT) { 0 0 0
374 /* socket doesn't exist. good */
375 0 Dx(Debug("socket doesn't exist yet (good)\n"));
376 }
377 else {
378 0 perror("Socket stat error");
379 0 exit(1);
380 }
381 }
382
383 /* is there a race between stat() and connect() here? Or is it irrelevant? */
384
385 0 ▶ if (connect(sd, (struct sockaddr *)&saun, len) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (connect(sd, (struct sockaddr *)&saun, len) < 0) { 0 0 0
386 /* Consider spawning Perl here and try again */
387 FILE *source;
388 int tmp_fd;
389 char temp_file[BUF_SIZE];
390 char *lock_file;
391 int sock_name_len;
392 int lock_fd;
393 0 int start_checked = 0;
394 0 int wrote_footer = 0; /* we may encounter __END__ or __DATA__ */
395 int line;
396 0 int retry_connect = 0;
397 0 int exit_code = 0;
398  
399 int pid, itmp, exitstatus;
400 sigset_t mask, omask;
401  
402 0 Dx(Debug("Couldn't connect, spawning new server: %s\n", strerror(errno)));
403
404 0 sock_name_len = strlen(sock_name);
405 0 lock_file = my_malloc(sock_name_len + 6);
406 0 strncpy(lock_file, sock_name, sock_name_len);
407 0 lock_file[sock_name_len] = '.';
408 0 lock_file[sock_name_len+1] = 'l';
409 0 lock_file[sock_name_len+2] = 'o';
410 0 lock_file[sock_name_len+3] = 'c';
411 0 lock_file[sock_name_len+4] = 'k';
412 0 lock_file[sock_name_len+5] = '\0';
413
414 0 Dx(Debug("opening lock_file: %s\n", lock_file));
415 0 lock_fd = open(lock_file, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
416 0 ▶ if (lock_fd == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (lock_fd == -1) { 0 0 0
417 0 perror("Cannot open lock file");
418 0 exit_code = 1;
419 0 goto cleanup;
420 }
421 0 ▶ while (flock(lock_fd, LOCK_EX|LOCK_NB) == -1) {
Branch: 1 branch
BranchTrueFalseTotal
while (flock(lock_fd, LOCK_EX|LOCK_NB) == -1) { 0 0 0
422 0 Dx(Debug("flock failed - someone else is probably waiting to spawn - sleeping\n"));
423 0 retry_connect = 1;
424 0 sleep(1);
425 }
426
427 0 ▶ if (retry_connect) {
Branch: 1 branch
BranchTrueFalseTotal
if (retry_connect) { 0 0 0
428 0 ▶ if (connect(sd, (struct sockaddr *)&saun, len) >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (connect(sd, (struct sockaddr *)&saun, len) >= 0) { 0 0 0
429 0 goto cleanup; /* everything is now OK! */
430 }
431 /* otherwise we try ourselves to re-spawn */
432 }
433
434 /*
435 if (unlink(sock_name) != 0 && errno != ENOENT) {
436 perror("pperl: removal of old socket failed");
437 exit_code = 1;
438 goto cleanup;
439 }
440 */
441
442 /* Create temp file with adjusted script... */
443 0 ▶ if (!(source = fopen(scriptname, "r"))) {
Branch: 1 branch
BranchTrueFalseTotal
if (!(source = fopen(scriptname, "r"))) { 0 0 0
444 0 perror("pperl: Cannot open perl script");
445 0 exit_code = 1;
446 0 goto cleanup;
447 }
448  
449 0 snprintf(temp_file, BUF_SIZE, "%s/%s", P_tmpdir, "pperlXXXXXX");
450 0 tmp_fd = mkstemp(temp_file);
451 0 ▶ if (tmp_fd == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (tmp_fd == -1) { 0 0 0
452 0 perror("pperl: Cannot create temporary file");
453 0 exit_code = 1;
454 0 goto cleanup;
455 }
456
457 0 write(tmp_fd, "### Temp File ###\n", 18);
458 0 write(tmp_fd, perl_header, strlen(perl_header));
459  
460 /* rewrite the perl script with pperl.h.header contents wrapper
461 and do some other fixups in the process */
462 0 line = 0;
463 0 ▶ while ( fgets( buf, BUF_SIZE, source ) ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( fgets( buf, BUF_SIZE, source ) ) { 0 0 0
464 0 readlen = strlen(buf);
465 0 Dx(Debug("read '%s' %d \n", buf, readlen));
466  
467 0 ▶ if (!start_checked) { /* first line */
Branch: 1 branch
BranchTrueFalseTotal
if (!start_checked) { /* first line */ 0 0 0
468 0 start_checked = 1;
469  
470 0 ▶ if (buf[0] == '#' && buf[1] == '!') {
Branch: 2 branches
BranchTrueFalseTotal
if (buf[0] == '#' && buf[1] == '!') { 0 0 0
if (buf[0] == '#' && buf[1] == '!') { 0 0 0
471 char *args;
472 /* solaris sometimes doesn't propogate all the
473 * shebang line - so we do that here */
474 0 ▶ if ( (args = strstr(buf, " ")) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ( (args = strstr(buf, " ")) ) { 0 0 0
475 0 strncat(perl_options, args, strlen(args) - 1);
476 }
477  
478 0 write(tmp_fd, "\n#line 2 ", 9);
479 0 write(tmp_fd, scriptname, strlen(scriptname));
480 0 write(tmp_fd, "\n", 1);
481  
482 0 line = 2;
483 0 continue;
484 }
485 else {
486 0 write(tmp_fd, "\n#line 1 ", 9);
487 0 write(tmp_fd, scriptname, strlen(scriptname));
488 0 write(tmp_fd, "\n", 1);
489 }
490 }
491 0 ▶ if ((!strcmp(buf, "__END__\n") ||
Branch: 1 branch
BranchTrueFalseTotal
if ((!strcmp(buf, "__END__\n") || 0 0 0
492 0 ▶ !strcmp(buf, "__DATA__\n")) &&
Branch: 2 branches
BranchTrueFalseTotal
!strcmp(buf, "__DATA__\n")) && 0 0 0
!strcmp(buf, "__DATA__\n")) && 0 0 0
493 !wrote_footer) {
494 char text_line[BUF_SIZE];
495 0 wrote_footer = 1;
496 0 write(tmp_fd, perl_footer, strlen(perl_footer));
497 0 snprintf(text_line, BUF_SIZE, "package main;\n#line %d %s\n", line, scriptname);
498 0 write(tmp_fd, text_line, strlen(text_line));
499 }
500 0 write(tmp_fd, buf, readlen);
501 0 ▶ if (buf[readlen] == '\n') ++line;
Branch: 1 branch
BranchTrueFalseTotal
if (buf[readlen] == '\n') ++line; 0 0 0
502 }
503
504 0 ▶ if (fclose(source)) {
Branch: 1 branch
BranchTrueFalseTotal
if (fclose(source)) { 0 0 0
505 0 perror("pperl: Error reading perl script");
506 0 exit_code = 1;
507 0 goto cleanup;
508 }
509  
510 0 ▶ if (!wrote_footer)
Branch: 1 branch
BranchTrueFalseTotal
if (!wrote_footer) 0 0 0
511 0 write(tmp_fd, perl_footer, strlen(perl_footer));
512  
513 0 Dx(Debug("wrote file %s\n", temp_file));
514  
515 0 close(tmp_fd);
516  
517 /*** Temp file creation done ***/
518  
519 0 snprintf(buf, BUF_SIZE, "%s %s %s %s %d %d %d %d %s",
520 PERL_INTERP, perl_options, temp_file,
521 sock_name, prefork, maxclients,
522 any_user, no_cleanup, scriptname);
523 0 Dx(Debug("syscall: %s\n", buf));
524  
525 /* block SIGCHLD so noone else can wait() on the child before we do */
526 0 sigemptyset(&mask);
527 0 sigaddset(&mask, SIGCHLD);
528 0 sigprocmask(SIG_BLOCK, &mask, &omask);
529  
530 0 ▶ if ((pid = system(buf)) != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((pid = system(buf)) != 0) { 0 0 0
531 0 unlink(temp_file);
532 0 ▶ if (stat((const char*)sock_name, &stat_buf) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (stat((const char*)sock_name, &stat_buf) == 0) { 0 0 0
533 /* socket exists - perhaps we should just try and connect to it? */
534 /* possible cause is a race condition. So ignore this and just try
535 the connect() call again. */
536 0 perror("pperl: perl script failed to start, but lets be gung-ho and try and connect again anyway!");
537 }
538 0 perror("pperl: perl script failed to start");
539 0 exit_code = 1;
540 0 goto cleanup;
541 }
542 else {
543 0 Dx(Debug("waiting for perl to return...\n"));
544 0 ▶ while ((itmp = waitpid(0, &exitstatus, 0)) == -1 && errno == EINTR)
Branch: 2 branches
BranchTrueFalseTotal
while ((itmp = waitpid(0, &exitstatus, 0)) == -1 && errno == EINTR) 0 0 0
while ((itmp = waitpid(0, &exitstatus, 0)) == -1 && errno == EINTR) 0 0 0
545 ;
546 0 sigprocmask(SIG_SETMASK, &omask, NULL);
547 0 Dx(Debug("returned.\n"));
548
549 /* now remove the perl script */
550 0 unlink(temp_file);
551 }
552
553 /* try and connect to the new socket */
554 0 ▶ while ((i++ <= 30) && (connect(sd, (struct sockaddr *)&saun, len) < 0))
Branch: 2 branches
BranchTrueFalseTotal
while ((i++ <= 30) && (connect(sd, (struct sockaddr *)&saun, len) < 0)) 0 0 0
while ((i++ <= 30) && (connect(sd, (struct sockaddr *)&saun, len) < 0)) 0 0 0
555 {
556 0 Dx(Debug("."));
557 0 sleep(1);
558 }
559 0 ▶ if (i >= 30) {
Branch: 1 branch
BranchTrueFalseTotal
if (i >= 30) { 0 0 0
560 /* If we really *really* couldn't connect, try and delete the socket if it exists */
561 0 ▶ if (unlink(sock_name) != 0 && errno != ENOENT) {
Branch: 2 branches
BranchTrueFalseTotal
if (unlink(sock_name) != 0 && errno != ENOENT) { 0 0 0
if (unlink(sock_name) != 0 && errno != ENOENT) { 0 0 0
562 0 perror("pperl: removal of old socket failed");
563 }
564 0 perror("pperl: persistent perl process failed to start after 30 seconds");
565 0 exit_code = 1;
566 0 goto cleanup;
567 }
568
569 0 Dx(Debug("Connected\n"));
570
571 0 cleanup:
572 0 flock(lock_fd, LOCK_UN);
573 0 close(lock_fd);
574 0 free(lock_file);
575 0 ▶ if (exit_code > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (exit_code > 0) { 0 0 0
576 0 free(sock_name);
577 0 exit(exit_code);
578 }
579 }
580
581 0 free(sock_name);
582 0 return handle_socket(sd, argc, argv);
583 }
584  
585 static
586 int
587 0 handle_socket(int sd, int argc, char **argv) {
588 long max_fd;
589 char **env;
590 int i;
591 char buf[BUF_SIZE];
592  
593 0 Dx(Debug("connected over %d\n", sd));
594  
595 0 read(sd, buf, 10);
596 0 buf[10] = '\0';
597 0 connected_to = atoi(buf);
598 0 Dx(Debug("chatting to %d, hooking signals\n", connected_to));
599  
600 /* bad magic number, there only seem to be 30 signals on a linux
601 * box -- richardc*/
602 0 ▶ for (i = 1; i < 32; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 1; i < 32; i++) 0 0 0
603 0 signal(i, sig_handler);
604  
605  
606 0 Dx(Debug("sending fds\n"));
607 0 ▶ if ((max_fd = sysconf(_SC_OPEN_MAX)) < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((max_fd = sysconf(_SC_OPEN_MAX)) < 0) { 0 0 0
608 0 perror("pperl: dunno how many fds to check");
609 0 exit(1);
610 }
611  
612 0 ▶ for (i = 0; i < max_fd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < max_fd; i++) { 0 0 0
613 0 ▶ if (fcntl(i, F_GETFL, -1) >= 0 && i != sd) {
Branch: 2 branches
BranchTrueFalseTotal
if (fcntl(i, F_GETFL, -1) >= 0 && i != sd) { 0 0 0
if (fcntl(i, F_GETFL, -1) >= 0 && i != sd) { 0 0 0
614 int ret;
615 0 write(sd, &i, sizeof(int));
616 0 ret = send_fd(sd, i);
617 0 Dx(Debug("send_fd %d %d\n", i, ret));
618 }
619 }
620 0 i = -1;
621 0 write(sd, &i, sizeof(int));
622 0 Dx(Debug("fds sent\n"));
623  
624 0 write(sd, "[PID]", 6);
625 0 snprintf(buf, BUF_SIZE, "%d", getpid());
626 0 write(sd, buf, strlen(buf) + 1);
627  
628
629 /* print to socket... */
630 0 write(sd, "[ENV]", 6);
631 0 ▶ for (i= 0, env = environ; *env; i++, env++);
Branch: 1 branch
BranchTrueFalseTotal
for (i= 0, env = environ; *env; i++, env++); 0 0 0
632 0 snprintf(buf, BUF_SIZE, "%d", i);
633 0 write(sd, buf, strlen(buf) + 1);
634
635 0 ▶ while ( *environ != NULL ) {
Branch: 1 branch
BranchTrueFalseTotal
while ( *environ != NULL ) { 0 0 0
636 0 size_t len = strlen(*environ) + 1;
637 /* Dx(Debug("sending environ: %s\n", *environ)); */
638 0 write(sd, *environ, len);
639 0 environ++;
640 }
641  
642 0 write(sd, "[CWD]", 6);
643 0 ▶ if (getcwd(buf, BUF_SIZE) == NULL) {
Branch: 1 branch
BranchTrueFalseTotal
if (getcwd(buf, BUF_SIZE) == NULL) { 0 0 0
644 0 perror("pperl: getcwd");
645 0 exit (1);
646 }
647 0 write(sd, buf, strlen(buf) + 1);
648  
649 0 Dx(Debug("sending %d args\n", argc));
650 0 write(sd, "[ARGV]", 7);
651 0 snprintf(buf, BUF_SIZE, "%d", argc);
652 0 write(sd, buf, strlen(buf) + 1);
653 0 ▶ for (i = 0; i < argc; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < argc; i++) { 0 0 0
654 0 size_t len = strlen(argv[i]) + 1;
655 0 Dx(Debug("sending argv[%d]: '%s'\n", i, argv[i]));
656 0 write(sd, argv[i], len);
657 }
658  
659 0 write(sd, "[DONE]", 7);
660  
661 0 Dx(Debug("waiting for OK message from %d\n", sd));
662 0 ▶ if (read(sd, buf, 3) != 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (read(sd, buf, 3) != 3) { 0 0 0
663 0 perror("pperl: failed to read 3 bytes for an OK message");
664 0 exit(1);
665 }
666 0 ▶ if (strncmp(buf, "OK\n", 3)) {
Branch: 1 branch
BranchTrueFalseTotal
if (strncmp(buf, "OK\n", 3)) { 0 0 0
667 0 i = read(sd, buf, BUF_SIZE - 1);
668 0 buf[i] = '\0';
669 0 fprintf(stderr, "pperl: expected 'OK\\n', got: '%s'\n", buf);
670 0 exit(1);
671 }
672 0 Dx(Debug("got it\n"));
673  
674 0 Dx(Debug("reading return code\n"));
675 0 i = read(sd, buf, BUF_SIZE - 1);
676 0 ▶ if (i == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if (i == -1) { 0 0 0
677 0 perror("Nothing read back from socket!");
678 }
679 0 buf[i] = '\0';
680 0 Dx(Debug("socket read '%s'\n", buf));
681  
682 0 ▶ for (i = 0; i < max_fd; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < max_fd; i++) { 0 0 0
683 0 close(i);
684 }
685
686 0 exit (atoi(buf));
687 }
688  
689  
690  
691 /*
692 Local Variables:
693 mode: C
694 c-basic-offset: 4
695 tab-width: 4
696 indent-tabs-mode: nil
697 End:
698 */
« PPerl.xs ↑ Summary pass_fd.c »