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 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
/* pperl - run perl scripts persistently */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <memory.h>
#include <errno.h>
#include <pwd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/file.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <fcntl.h>
#include <limits.h>
#include <signal.h>
#include <unistd.h>
#include "pperl.h"
#include "pass_fd.h" /* the stuff borrowed from stevens */
#define DEBUG 1
/* must never be less than 3 */
#define BUF_SIZE 4096
#ifdef ENOBUFS
# define NO_BUFSPC(e) ((e) == ENOBUFS || (e) == ENOMEM)
#else
# define NO_BUFSPC(e) ((e) == ENOMEM)
#endif
static void Usage( char *pName );
static void DecodeParm( char *pArg );
static int DispatchCall( char *scriptname, int argc, char **argv );
char *pVersion = PPERL_VERSION;
char perl_options[1024];
extern char **environ;
pid_t connected_to;
int kill_script = 0;
int any_user = 0;
int prefork = 5;
int maxclients = 100;
int path_max;
char* tmp_path;
int no_cleanup = 0;
FILE *log_fd = NULL;
#if DEBUG
#define Dx(x) (x)
#else
#define Dx(x)
#endif
static
void Debug( const char * format, ...)
{
va_list args;
if (!log_fd)
return;
va_start(args, format);
vfprintf(log_fd, format, args);
va_end(args);
fflush(log_fd);
}
static void *my_malloc(size_t);
/* Set tmp_path to a user-specific path for the pperl control files. */
static void
setup_tmp_path(void)
{
uid_t me = getuid();
struct passwd *pw = getpwuid(me);
tmp_path = my_malloc(path_max);
if (pw) {
snprintf(tmp_path, path_max, "%s/.pperl", pw->pw_dir);
mkdir(tmp_path, 0700); /* ignore failure */
} else {
snprintf(tmp_path, path_max, "%s/pperl.%u",
P_tmpdir, (unsigned)pw->pw_uid);
if (mkdir(tmp_path, 0600) == -1) {
/* The directory already exists. Check ownership and
permissions. */
struct stat buf;
if (lstat(tmp_path, &buf) == -1) {
fprintf(stderr, "could not stat %s: %s\n",
tmp_path, strerror(errno));
exit(1);
}
if (!S_ISDIR(buf.st_mode)) {
fprintf(stderr, "%s: not a directory\n",
tmp_path);
exit(1);
}
if (buf.st_uid != me) {
fprintf(stderr, "%s: wrong directory ownership\n",
tmp_path);
exit(1);
}
if ((buf.st_mode & 0777) != 0700) {
fprintf(stderr, "%s: wrong directory mode\n",
tmp_path);
exit(1);
}
}
}
}
int main( int argc, char **argv )
{
int i;
char *pArg = 0;
int pperl_section = 0;
int return_code = 0;
if( argc < 2 )
Usage( argv[0] );
#ifdef PATH_MAX
path_max = PATH_MAX;
#else
path_max = pathconf (path, _PC_PATH_MAX);
if (path_max <= 0) {
path_max = 4096;
}
#endif
tmp_path = getenv("PPERL_TMP_PATH");
if (!tmp_path)
setup_tmp_path();
pperl_section = 0;
for ( i = 1; i < argc; i++ ) {
pArg = argv[i];
/* fprintf(stderr, "Parsing arg: %s\n", pArg); */
if (*pArg != '-') break;
if ( !strcmp(pArg, "-k") || !strcmp(pArg, "--kill") )
kill_script = 1;
else if (!strncmp(pArg, "--prefork", 9) ) {
int newval;
if (pArg[9] == '=') /* "--prefork=20" */
pArg += 10;
else /* "--prefork" "20" */
pArg = argv[++i];
newval = atoi(pArg);
if (newval > 0) prefork = newval;
}
else if (!strncmp(pArg, "--logfile", 7) ) {
int newval;
char *filename;
if (pArg[7] == '=') /* --logfile=.... */
pArg += 13;
else
pArg = argv[++i];
filename = pArg;
newval = atoi(pArg);
if (newval == 0) {
fprintf(stderr, "opening log_fd: %s\n", filename);
log_fd = fopen(filename, "a");
if (!log_fd) {
perror("Cannot open logfile");
exit(1);
}
}
else {
log_fd = fdopen(newval, "a");
if (!log_fd) {
perror("fd for --logfile error");
exit(1);
}
}
}
else if (!strncmp(pArg, "--no-cleanup", 12) ) {
no_cleanup = 1;
}
else if (!strncmp(pArg, "--maxclients", 12) ) {
int newval;
if (pArg[12] == '=') /* "--maxclients=20" */
pArg += 13;
else /* "--maxclients" "20" */
pArg = argv[++i];
newval = atoi(pArg);
if (newval > 0) maxclients = newval;
}
else if ( !strcmp(pArg, "-z") || !strcmp(pArg, "--anyuser") )
any_user = 1;
else if ( !strcmp(pArg, "-h") || !strcmp(pArg, "--help") )
Usage( NULL );
else if ( !strncmp(pArg, "--", 2) )
; /* do nothing - backward compatibility */
else {
DecodeParm( pArg );
}
}
i++;
return_code = DispatchCall( pArg, argc - i, (char**)(argv + i) );
Dx(Debug("done, returning %d\n", return_code));
if (log_fd) fclose(log_fd);
return return_code;
}
static void DecodeParm( char *pArg )
{
if ( (strlen(perl_options) + strlen(pArg) + 1) > 1000 ) {
fprintf(stderr, "param list too long. Sorry.");
exit(1);
}
strcat(perl_options, pArg);
strcat(perl_options, " ");
}
static void Usage( char *pName )
{
printf( "pperl version %s\n", pVersion );
if( pName == NULL )
{
printf( "Usage: pperl [options] filename\n" );
}
else
{
printf( "Usage: %.255s [options] filename\n", pName );
}
printf("perl options are passed to your perl executable (see the perlrun man page).\n"
"pperl options control the persistent perl behaviour\n"
"\n"
"PPerl Options:\n"
" -k or --kill Kill the currently running pperl for that script\n"
" -h or --help This page\n"
" --prefork The number of child processes to prefork (default=5)\n"
" --maxclients The number of client connections each child\n"
" will process (default=100)\n"
" -z or --anyuser Allow any user (after the first) to access the socket\n"
" WARNING: This has severe security implications. Use\n"
" at your own risk\n"
" --no-cleanup Skip the cleanup stage at the end of running your script\n"
" this may make your code run faster, but if you forget\n"
" to close files then they will remain unflushed and unclosed\n"
);
exit( 1 );
}
static void *
my_malloc(size_t size)
{
void *mem = malloc(size);
if (mem == NULL) {
perror("malloc failed");
exit(-1);
}
return mem;
}
/* make socket name from scriptname, switching / for _ */
static char *
MakeSockName(char * scriptname )
{
char * sockname;
char * save;
/* strict C compilers can't/won't do char foo[variant]; */
char *fullpath = my_malloc(path_max);
int i = 0;
if (realpath(scriptname, fullpath) == NULL) {
perror("pperl: resolving full pathname to script failed");
exit(1);
}
Dx(Debug("realpath returned: %s\n", fullpath));
/* Ugh. I am a terrible C programmer! */
sockname = my_malloc(strlen(tmp_path) + strlen(fullpath) + 3);
save = sockname;
sprintf(sockname, "%s/", tmp_path);
sockname += strlen(tmp_path) + 1;
while (fullpath[i] != '\0') {
if (fullpath[i] == '/') {
*sockname = '_';
}
else if (fullpath[i] == '.') {
*sockname = '_';
}
else {
*sockname = fullpath[i];
}
sockname++; i++;
}
*sockname = '\0';
free(fullpath);
return save;
}
static void
sig_handler(int sig)
{
kill(connected_to, sig);
signal(sig, sig_handler);
/* skreech_to_a_halt++; */
}
static int handle_socket(int sd, int argc, char **argv );
static int DispatchCall( char *scriptname, int argc, char **argv )
{
register int i, sd, len;
ssize_t readlen;
struct sockaddr_un saun;
struct stat stat_buf;
struct stat sock_stat;
char *sock_name;
char buf[BUF_SIZE];
int respawn_script = 0;
sd = 0;
/* create socket name */
Dx(Debug("pperl: %s\n", scriptname));
sock_name = MakeSockName(scriptname);
Dx(Debug("got socket: %s\n", sock_name));
if (!stat(sock_name, &sock_stat) && !stat(scriptname, &stat_buf)) {
if (stat_buf.st_mtime >= sock_stat.st_mtime) {
respawn_script = 1;
Dx(Debug("respawning slave - top level script changed\n"));
}
}
if (kill_script || respawn_script) {
int pid_fd, sock_name_len;
char *pid_file;
pid_t pid = 0;
respawn_script = 0; /* reset so we can use it later :-) */
sock_name_len = strlen(sock_name);
pid_file = my_malloc(sock_name_len + 5);
strncpy(pid_file, sock_name, sock_name_len);
pid_file[sock_name_len] = '.';
pid_file[sock_name_len+1] = 'p';
pid_file[sock_name_len+2] = 'i';
pid_file[sock_name_len+3] = 'd';
pid_file[sock_name_len+4] = '\0';
Dx(Debug("opening pid_file: %s\n", pid_file));
pid_fd = open(pid_file, O_RDONLY);
if (pid_fd == -1) {
Dx(Debug("Cannot open pid file (perhaps PPerl wasn't running for that script?)\n"));
write(1, "No process killed - no pid file\n", 32);
goto killed;
}
readlen = read(pid_fd, buf, BUF_SIZE);
if (readlen == -1) {
perror("pperl: nothing in pid file?");
goto killed;
}
buf[readlen] = '\0';
close(pid_fd);
pid = atoi(buf);
Dx(Debug("got pid %d (%s)\n", pid, buf));
if (kill(pid, SIGINT) == -1) {
if (errno == ESRCH) {
perror("pperl kill");
Dx(Debug("Process didn't exist. Unlinking %s and %s\n", pid_file, sock_name));
unlink(pid_file);
unlink(sock_name);
}
else {
perror("pperl: could not kill process");
}
}
free(pid_file);
killed:
if (kill_script) {
free(sock_name); /* Hmm, should probably do this everywhere else we return too */
return 0;
}
if (pid != 0) {
/* cheesy - let the child go away proper */
while (!kill(pid, 0)) {}
}
}
for (i = 0; i < 10; i++) {
sd = socket(PF_UNIX, SOCK_STREAM, PF_UNSPEC);
if (sd != -1) {
break;
}
else if (NO_BUFSPC(errno)) {
sleep(1);
}
else {
perror("pperl: Couldn't create socket");
return 1;
}
}
saun.sun_family = PF_UNIX;
strcpy(saun.sun_path, sock_name);
len = sizeof(saun.sun_family) + strlen(saun.sun_path) + 1;
Dx(Debug("%d connecting\n", getpid()));
if (stat((const char*)sock_name, &stat_buf)) {
if (errno == ENOENT) {
/* socket doesn't exist. good */
Dx(Debug("socket doesn't exist yet (good)\n"));
}
else {
perror("Socket stat error");
exit(1);
}
}
/* is there a race between stat() and connect() here? Or is it irrelevant? */
if (connect(sd, (struct sockaddr *)&saun, len) < 0) {
/* Consider spawning Perl here and try again */
FILE *source;
int tmp_fd;
char temp_file[BUF_SIZE];
char *lock_file;
int sock_name_len;
int lock_fd;
int start_checked = 0;
int wrote_footer = 0; /* we may encounter __END__ or __DATA__ */
int line;
int retry_connect = 0;
int exit_code = 0;
int pid, itmp, exitstatus;
sigset_t mask, omask;
Dx(Debug("Couldn't connect, spawning new server: %s\n", strerror(errno)));
sock_name_len = strlen(sock_name);
lock_file = my_malloc(sock_name_len + 6);
strncpy(lock_file, sock_name, sock_name_len);
lock_file[sock_name_len] = '.';
lock_file[sock_name_len+1] = 'l';
lock_file[sock_name_len+2] = 'o';
lock_file[sock_name_len+3] = 'c';
lock_file[sock_name_len+4] = 'k';
lock_file[sock_name_len+5] = '\0';
Dx(Debug("opening lock_file: %s\n", lock_file));
lock_fd = open(lock_file, O_CREAT|O_WRONLY, S_IRUSR|S_IWUSR);
if (lock_fd == -1) {
perror("Cannot open lock file");
exit_code = 1;
goto cleanup;
}
while (flock(lock_fd, LOCK_EX|LOCK_NB) == -1) {
Dx(Debug("flock failed - someone else is probably waiting to spawn - sleeping\n"));
retry_connect = 1;
sleep(1);
}
if (retry_connect) {
if (connect(sd, (struct sockaddr *)&saun, len) >= 0) {
goto cleanup; /* everything is now OK! */
}
/* otherwise we try ourselves to re-spawn */
}
/*
if (unlink(sock_name) != 0 && errno != ENOENT) {
perror("pperl: removal of old socket failed");
exit_code = 1;
goto cleanup;
}
*/
/* Create temp file with adjusted script... */
if (!(source = fopen(scriptname, "r"))) {
perror("pperl: Cannot open perl script");
exit_code = 1;
goto cleanup;
}
snprintf(temp_file, BUF_SIZE, "%s/%s", tmp_path, "pperlXXXXXX");
tmp_fd = mkstemp(temp_file);
if (tmp_fd == -1) {
perror("pperl: Cannot create temporary file");
exit_code = 1;
goto cleanup;
}
write(tmp_fd, "### Temp File ###\n", 18);
write(tmp_fd, perl_header, strlen(perl_header));
/* rewrite the perl script with pperl.h.header contents wrapper
and do some other fixups in the process */
line = 0;
while ( fgets( buf, BUF_SIZE, source ) ) {
readlen = strlen(buf);
Dx(Debug("read '%s' %d \n", buf, readlen));
if (!start_checked) { /* first line */
start_checked = 1;
if (buf[0] == '#' && buf[1] == '!') {
char *args;
/* solaris sometimes doesn't propogate all the
* shebang line - so we do that here */
if ( (args = strstr(buf, " ")) ) {
strncat(perl_options, args, strlen(args) - 1);
}
write(tmp_fd, "\n#line 2 ", 9);
write(tmp_fd, scriptname, strlen(scriptname));
write(tmp_fd, "\n", 1);
line = 2;
continue;
}
else {
write(tmp_fd, "\n#line 1 ", 9);
write(tmp_fd, scriptname, strlen(scriptname));
write(tmp_fd, "\n", 1);
}
}
if ((!strcmp(buf, "__END__\n") ||
!strcmp(buf, "__DATA__\n")) &&
!wrote_footer) {
char text_line[BUF_SIZE];
wrote_footer = 1;
write(tmp_fd, perl_footer, strlen(perl_footer));
snprintf(text_line, BUF_SIZE, "package main;\n#line %d %s\n", line, scriptname);
write(tmp_fd, text_line, strlen(text_line));
}
write(tmp_fd, buf, readlen);
if (buf[readlen] == '\n') ++line;
}
if (fclose(source)) {
perror("pperl: Error reading perl script");
exit_code = 1;
goto cleanup;
}
if (!wrote_footer)
write(tmp_fd, perl_footer, strlen(perl_footer));
Dx(Debug("wrote file %s\n", temp_file));
close(tmp_fd);
/*** Temp file creation done ***/
snprintf(buf, BUF_SIZE, "%s %s %s %s %d %d %d %d %s",
PERL_INTERP, perl_options, temp_file,
sock_name, prefork, maxclients,
any_user, no_cleanup, scriptname);
Dx(Debug("syscall: %s\n", buf));
/* block SIGCHLD so noone else can wait() on the child before we do */
sigemptyset(&mask);
sigaddset(&mask, SIGCHLD);
sigprocmask(SIG_BLOCK, &mask, &omask);
if ((pid = system(buf)) != 0) {
unlink(temp_file);
if (stat((const char*)sock_name, &stat_buf) == 0) {
/* socket exists - perhaps we should just try and connect to it? */
/* possible cause is a race condition. So ignore this and just try
the connect() call again. */
perror("pperl: perl script failed to start, but lets be gung-ho and try and connect again anyway!");
}
perror("pperl: perl script failed to start");
exit_code = 1;
goto cleanup;
}
else {
Dx(Debug("waiting for perl to return...\n"));
while ((itmp = waitpid(0, &exitstatus, 0)) == -1 && errno == EINTR)
;
sigprocmask(SIG_SETMASK, &omask, NULL);
Dx(Debug("returned.\n"));
/* now remove the perl script */
unlink(temp_file);
}
/* try and connect to the new socket */
while ((i++ <= 30) && (connect(sd, (struct sockaddr *)&saun, len) < 0))
{
Dx(Debug("."));
sleep(1);
}
if (i >= 30) {
/* If we really *really* couldn't connect, try and delete the socket if it exists */
if (unlink(sock_name) != 0 && errno != ENOENT) {
perror("pperl: removal of old socket failed");
}
perror("pperl: persistent perl process failed to start after 30 seconds");
exit_code = 1;
goto cleanup;
}
Dx(Debug("Connected\n"));
cleanup:
flock(lock_fd, LOCK_UN);
close(lock_fd);
free(lock_file);
if (exit_code > 0) {
free(sock_name);
exit(exit_code);
}
}
free(sock_name);
return handle_socket(sd, argc, argv);
}
static
int
handle_socket(int sd, int argc, char **argv) {
long max_fd;
char **env;
int i;
char buf[BUF_SIZE];
Dx(Debug("connected over %d\n", sd));
read(sd, buf, 10);
buf[10] = '\0';
connected_to = atoi(buf);
Dx(Debug("chatting to %d, hooking signals\n", connected_to));
/* bad magic number, there only seem to be 30 signals on a linux
* box -- richardc*/
for (i = 1; i < 32; i++)
signal(i, sig_handler);
Dx(Debug("sending fds\n"));
if ((max_fd = sysconf(_SC_OPEN_MAX)) < 0) {
perror("pperl: dunno how many fds to check");
exit(1);
}
for (i = 0; i < max_fd; i++) {
if (fcntl(i, F_GETFL, -1) >= 0 && i != sd) {
int ret;
write(sd, &i, sizeof(int));
ret = send_fd(sd, i);
Dx(Debug("send_fd %d %d\n", i, ret));
}
}
i = -1;
write(sd, &i, sizeof(int));
Dx(Debug("fds sent\n"));
write(sd, "[PID]", 6);
snprintf(buf, BUF_SIZE, "%d", getpid());
write(sd, buf, strlen(buf) + 1);
/* print to socket... */
write(sd, "[ENV]", 6);
for (i= 0, env = environ; *env; i++, env++);
snprintf(buf, BUF_SIZE, "%d", i);
write(sd, buf, strlen(buf) + 1);
while ( *environ != NULL ) {
size_t len = strlen(*environ) + 1;
/* Dx(Debug("sending environ: %s\n", *environ)); */
write(sd, *environ, len);
environ++;
}
write(sd, "[CWD]", 6);
if (getcwd(buf, BUF_SIZE) == NULL) {
perror("pperl: getcwd");
exit (1);
}
write(sd, buf, strlen(buf) + 1);
Dx(Debug("sending %d args\n", argc));
write(sd, "[ARGV]", 7);
snprintf(buf, BUF_SIZE, "%d", argc);
write(sd, buf, strlen(buf) + 1);
for (i = 0; i < argc; i++) {
size_t len = strlen(argv[i]) + 1;
Dx(Debug("sending argv[%d]: '%s'\n", i, argv[i]));
write(sd, argv[i], len);
}
write(sd, "[DONE]", 7);
Dx(Debug("waiting for OK message from %d\n", sd));
if (read(sd, buf, 3) != 3) {
perror("pperl: failed to read 3 bytes for an OK message");
exit(1);
}
if (strncmp(buf, "OK\n", 3)) {
i = read(sd, buf, BUF_SIZE - 1);
buf[i] = '\0';
fprintf(stderr, "pperl: expected 'OK\\n', got: '%s'\n", buf);
exit(1);
}
Dx(Debug("got it\n"));
Dx(Debug("reading return code\n"));
i = read(sd, buf, BUF_SIZE - 1);
if (i == -1) {
perror("Nothing read back from socket!");
}
buf[i] = '\0';
Dx(Debug("socket read '%s'\n", buf));
for (i = 0; i < max_fd; i++) {
close(i);
}
exit (atoi(buf));
}
/*
Local Variables:
mode: C
c-basic-offset: 4
tab-width: 4
indent-tabs-mode: nil
End:
*/
|