From: Adam Dickmeiss Date: Tue, 12 Jun 2007 13:02:38 +0000 (+0000) Subject: Option -p specifies pidfile. Option -u specifies uid for child process. X-Git-Tag: PAZPAR2.1.0.0~55 X-Git-Url: http://git.indexdata.com/?a=commitdiff_plain;h=2dcca58dcfd42a54b98f413a2bbb8676e05302d4;p=pazpar2-moved-to-github.git Option -p specifies pidfile. Option -u specifies uid for child process. Non-zero exit code makes pazpar2 stop completely. --- diff --git a/src/pazpar2.c b/src/pazpar2.c index 2e031b1..e3d6627 100644 --- a/src/pazpar2.c +++ b/src/pazpar2.c @@ -1,4 +1,4 @@ -/* $Id: pazpar2.c,v 1.88 2007-06-08 13:58:46 adam Exp $ +/* $Id: pazpar2.c,v 1.89 2007-06-12 13:02:38 adam Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -33,8 +33,6 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA void child_handler(void *data) { - yaz_log(YLOG_LOG, "child_handler"); - start_proxy(); init_settings(); @@ -49,6 +47,7 @@ void child_handler(void *data) pazpar2_event_loop(); + } int main(int argc, char **argv) @@ -56,13 +55,14 @@ int main(int argc, char **argv) int ret; char *arg; const char *pidfile = "pazpar2.pid"; + const char *uid = 0; if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) yaz_log(YLOG_WARN|YLOG_ERRNO, "signal"); yaz_log_init_prefix("pazpar2"); - while ((ret = options("f:h:p:t:l:dX", argv, argc, &arg)) != -2) + while ((ret = options("f:h:p:t:u:l:dX", argv, argc, &arg)) != -2) { switch (ret) { @@ -74,11 +74,14 @@ int main(int argc, char **argv) strcpy(global_parameters.listener_override, arg); break; case 'p': - strcpy(global_parameters.proxy_override, arg); + pidfile = arg; break; case 't': strcpy(global_parameters.settings_path_override, arg); - break; + break; + case 'u': + uid = arg; + break; case 'd': global_parameters.dump_records = 1; break; @@ -92,9 +95,9 @@ int main(int argc, char **argv) fprintf(stderr, "Usage: pazpar2\n" " -f configfile\n" " -h [host:]port (REST protocol listener)\n" - " -p hostname[:portno] (HTTP proxy)\n" - " -z hostname[:portno] (Z39.50 proxy)\n" + " -p pidfile PID file\n" " -t settings\n" + " -u uid\n" " -d (show internal records)\n" " -l file log to file\n" " -X debug mode\n" @@ -111,8 +114,9 @@ int main(int argc, char **argv) global_parameters.server = config->servers; start_http_listener(); - pazpar2_process(global_parameters.debug_mode, 0, child_handler, 0, - pidfile, 0); + pazpar2_process(global_parameters.debug_mode, + child_handler, 0 /* child_data */, + pidfile, uid); return 0; } diff --git a/src/pazpar2.h b/src/pazpar2.h index ff005ee..d257874 100644 --- a/src/pazpar2.h +++ b/src/pazpar2.h @@ -1,4 +1,4 @@ -/* $Id: pazpar2.h,v 1.38 2007-06-08 13:57:19 adam Exp $ +/* $Id: pazpar2.h,v 1.39 2007-06-12 13:02:38 adam Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -175,7 +175,7 @@ struct record *ingest_record(struct client *cl, Z_External *rec, void session_alert_watch(struct session *s, int what); void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num); -int pazpar2_process(int debug, int flags, +int pazpar2_process(int debug, void (*work)(void *data), void *data, const char *pidfile, const char *uid); diff --git a/src/process.c b/src/process.c index 9a97da7..4f7ce94 100644 --- a/src/process.c +++ b/src/process.c @@ -1,4 +1,4 @@ -/* $Id: process.c,v 1.1 2007-06-08 13:57:19 adam Exp $ +/* $Id: process.c,v 1.2 2007-06-12 13:02:38 adam Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -29,6 +29,8 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include #include +#include +#include #include @@ -56,10 +58,11 @@ void kill_child_handler(int num) kill(child_pid, num); } -int pazpar2_process(int debug, int flags, +int pazpar2_process(int debug, void (*work)(void *data), void *data, const char *pidfile, const char *uid /* not yet used */) { + struct passwd *pw = 0; int run = 1; int cont = 1; void (*old_sighup)(int); @@ -73,7 +76,19 @@ int pazpar2_process(int debug, int flags, work(data); exit(0); } + /* running in production mode. */ + if (uid) + { + yaz_log(YLOG_LOG, "getpwnam"); + // OK to use the non-thread version here + if (!(pw = getpwnam(uid))) + { + yaz_log(YLOG_FATAL, "%s: Unknown user", uid); + exit(1); + } + } + /* keep signals in their original state and make sure that some signals to parent process also gets sent to the child.. Normally this @@ -100,6 +115,16 @@ int pazpar2_process(int debug, int flags, signal(SIGTERM, old_sigterm);/* restore */ write_pidfile(pidfile); + + if (pw) + { + if (setuid(pw->pw_uid) < 0) + { + yaz_log(YLOG_FATAL|YLOG_ERRNO, "setuid"); + exit(1); + } + } + work(data); exit(0); } @@ -153,9 +178,9 @@ int pazpar2_process(int debug, int flags, else if (status == 0) cont = 0; /* child exited normally */ else - { /* child exited in an abnormal way */ + { /* child exited with error */ yaz_log(YLOG_LOG, "Exit %d from child %ld", status, (long) p); - cont = 1; + cont = 0; } if (cont) /* respawn slower as we get more errors */ sleep(1 + run/5);