fix top_dir instructions
[mod_fastcgi.git] / fcgi_config.c
blob1bcb01368eef2c343b94970dd9d092ebdd21f73e
1 /*
2 * $Id: fcgi_config.c,v 1.36 2002/08/20 02:36:15 robs Exp $
3 */
5 #include "fcgi.h"
7 #ifdef APACHE2
8 #include <limits.h>
9 #ifdef WIN32
10 #include <direct.h>
11 #else
12 #include <unistd.h>
13 #endif
14 #endif
16 #ifdef WIN32
17 /* warning C4100: unreferenced formal parameter */
18 /* warning C4706: assignment within conditional expression */
19 #pragma warning( disable : 4100 4706 )
20 #endif
22 /*******************************************************************************
23 * Get the next configuration directive argument, & return an in_addr and port.
24 * The arg must be in the form "host:port" where host can be an IP or hostname.
25 * The pool arg should be persistant storage.
27 static const char *get_host_n_port(pool *p, const char **arg,
28 const char **host, u_short *port)
30 char *cvptr, *portStr;
31 long tmp;
33 *host = ap_getword_conf(p, arg);
34 if (**host == '\0')
35 return "\"\"";
37 portStr = strchr(*host, ':');
38 if (portStr == NULL)
39 return "missing port specification";
41 /* Split the host and port portions */
42 *portStr++ = '\0';
44 /* Convert port number */
45 tmp = (u_short) strtol(portStr, &cvptr, 10);
46 if (*cvptr != '\0' || tmp < 1 || tmp > USHRT_MAX)
47 return ap_pstrcat(p, "bad port number \"", portStr, "\"", NULL);
49 *port = (unsigned short) tmp;
51 return NULL;
54 /*******************************************************************************
55 * Get the next configuration directive argument, & return an u_short.
56 * The pool arg should be temporary storage.
58 static const char *get_u_short(pool *p, const char **arg,
59 u_short *num, u_short min)
61 char *ptr;
62 long tmp;
63 const char *txt = ap_getword_conf(p, arg);
65 if (*txt == '\0') {
66 return "\"\"";
69 tmp = strtol(txt, &ptr, 10);
71 if (*ptr != '\0') {
72 return ap_pstrcat(p, "\"", txt, "\" must be a positive integer", NULL);
75 if (tmp < min || tmp > USHRT_MAX) {
76 return ap_psprintf(p, "\"%u\" must be >= %u and < %u", *num, min, USHRT_MAX);
79 *num = (u_short) tmp;
81 return NULL;
84 static const char *get_int(pool *p, const char **arg, int *num, int min)
86 char *cp;
87 const char *val = ap_getword_conf(p, arg);
89 if (*val == '\0')
91 return "\"\"";
94 *num = (int) strtol(val, &cp, 10);
96 if (*cp != '\0')
98 return ap_pstrcat(p, "can't parse ", "\"", val, "\"", NULL);
100 else if (*num < min)
102 return ap_psprintf(p, "\"%d\" must be >= %d", *num, min);
105 return NULL;
108 /*******************************************************************************
109 * Get the next configuration directive argument, & return an u_int.
110 * The pool arg should be temporary storage.
112 static const char *get_u_int(pool *p, const char **arg,
113 u_int *num, u_int min)
115 char *ptr;
116 const char *val = ap_getword_conf(p, arg);
118 if (*val == '\0')
119 return "\"\"";
120 *num = (u_int)strtol(val, &ptr, 10);
122 if (*ptr != '\0')
123 return ap_pstrcat(p, "\"", val, "\" must be a positive integer", NULL);
124 else if (*num < min)
125 return ap_psprintf(p, "\"%u\" must be >= %u", *num, min);
126 return NULL;
129 /*******************************************************************************
130 * Get the next configuration directive argument, & return a float.
131 * The pool arg should be temporary storage.
133 static const char *get_float(pool *p, const char **arg,
134 float *num, float min, float max)
136 char *ptr;
137 const char *val = ap_getword_conf(p, arg);
139 if (*val == '\0')
140 return "\"\"";
141 *num = (float) strtod(val, &ptr);
143 if (*ptr != '\0')
144 return ap_pstrcat(p, "\"", val, "\" is not a floating point number", NULL);
145 if (*num < min || *num > max)
146 return ap_psprintf(p, "\"%f\" is not between %f and %f", *num, min, max);
147 return NULL;
150 const char *fcgi_config_set_env_var(pool *p, char **envp, unsigned int *envc, char * var)
152 if (*envc >= MAX_INIT_ENV_VARS) {
153 return "too many variables, must be <= MAX_INIT_ENV_VARS";
156 if (strchr(var, '=') == NULL) {
157 *(envp + *envc) = ap_pstrcat(p, var, "=", getenv(var), NULL);
159 else {
160 *(envp + *envc) = var;
163 (*envc)++;
165 return NULL;
168 /*******************************************************************************
169 * Get the next configuration directive argument, & add it to an env array.
170 * The pool arg should be permanent storage.
172 static const char *get_env_var(pool *p, const char **arg, char **envp, unsigned int *envc)
174 char * const val = ap_getword_conf(p, arg);
176 if (*val == '\0') {
177 return "\"\"";
180 return fcgi_config_set_env_var(p, envp, envc, val);
183 static const char *get_pass_header(pool *p, const char **arg, array_header **array)
185 const char **header;
187 if (!*array) {
188 *array = ap_make_array(p, 10, sizeof(char*));
191 header = (const char **)ap_push_array(*array);
192 *header = ap_getword_conf(p, arg);
194 return header ? NULL : "\"\"";
197 /*******************************************************************************
198 * Return a "standard" message for common configuration errors.
200 static const char *invalid_value(pool *p, const char *cmd, const char *id,
201 const char *opt, const char *err)
203 return ap_psprintf(p, "%s%s%s: invalid value for %s: %s",
204 cmd, id ? " " : "", id ? id : "", opt, err);
207 /*******************************************************************************
208 * Set/Reset the uid/gid that Apache and the PM will run as. This is ap_user_id
209 * and ap_group_id if we're started as root, and euid/egid otherwise. Also try
210 * to check that the config files don't set the User/Group after a FastCGI
211 * directive is used that depends on it.
213 /*@@@ To be complete, we should save a handle to the server each AppClass is
214 * configured in and at init() check that the user/group is still what we
215 * thought it was. Also the other directives should only be allowed in the
216 * parent Apache server.
218 const char *fcgi_config_set_fcgi_uid_n_gid(int set)
220 static int isSet = 0;
222 #ifndef WIN32
224 uid_t uid = geteuid();
225 gid_t gid = getegid();
227 if (set == 0) {
228 isSet = 0;
229 fcgi_user_id = (uid_t)-1;
230 fcgi_group_id = (gid_t)-1;
231 return NULL;
234 if (uid == 0) {
235 uid = ap_user_id;
238 if (gid == 0) {
239 gid = ap_group_id;
242 if (isSet && (uid != fcgi_user_id || gid != fcgi_group_id)) {
243 return "User/Group commands must preceed FastCGI server definitions";
246 isSet = 1;
247 fcgi_user_id = uid;
248 fcgi_group_id = gid;
250 #endif /* !WIN32 */
252 return NULL;
255 apcb_t fcgi_config_reset_globals(void* dummy)
257 fcgi_config_pool = NULL;
258 fcgi_servers = NULL;
259 fcgi_config_set_fcgi_uid_n_gid(0);
260 fcgi_wrapper = NULL;
261 fcgi_socket_dir = DEFAULT_SOCK_DIR;
263 fcgi_dynamic_total_proc_count = 0;
264 fcgi_dynamic_epoch = 0;
265 fcgi_dynamic_last_analyzed = 0;
267 dynamicMaxProcs = FCGI_DEFAULT_MAX_PROCS;
268 dynamicMinProcs = FCGI_DEFAULT_MIN_PROCS;
269 dynamicMaxClassProcs = FCGI_DEFAULT_MAX_CLASS_PROCS;
270 dynamicKillInterval = FCGI_DEFAULT_KILL_INTERVAL;
271 dynamicUpdateInterval = FCGI_DEFAULT_UPDATE_INTERVAL;
272 dynamicGain = FCGI_DEFAULT_GAIN;
273 dynamicThreshold1 = FCGI_DEFAULT_THRESHOLD_1;
274 dynamicThresholdN = FCGI_DEFAULT_THRESHOLD_N;
275 dynamicPleaseStartDelay = FCGI_DEFAULT_START_PROCESS_DELAY;
276 dynamicAppConnectTimeout = FCGI_DEFAULT_APP_CONN_TIMEOUT;
277 dynamicEnvp = &fcgi_empty_env;
278 dynamicProcessSlack = FCGI_DEFAULT_PROCESS_SLACK;
279 dynamicAutoRestart = FCGI_DEFAULT_RESTART_DYNAMIC;
280 dynamicAutoUpdate = FCGI_DEFAULT_AUTOUPDATE;
281 dynamicListenQueueDepth = FCGI_DEFAULT_LISTEN_Q;
282 dynamicInitStartDelay = DEFAULT_INIT_START_DELAY;
283 dynamicRestartDelay = FCGI_DEFAULT_RESTART_DELAY;
284 dynamic_pass_headers = NULL;
285 dynamic_idle_timeout = FCGI_DEFAULT_IDLE_TIMEOUT;
286 dynamicFlush = FCGI_FLUSH;
288 #ifndef WIN32
289 /* Close any old pipe (HUP/USR1) */
290 if (fcgi_pm_pipe[0] != -1) {
291 close(fcgi_pm_pipe[0]);
292 fcgi_pm_pipe[0] = -1;
294 if (fcgi_pm_pipe[1] != -1) {
295 close(fcgi_pm_pipe[1]);
296 fcgi_pm_pipe[1] = -1;
298 #endif
300 return APCB_OK;
303 /*******************************************************************************
304 * Create a directory to hold Unix/Domain sockets.
306 const char *fcgi_config_make_dir(pool *tp, char *path)
308 struct stat finfo;
309 const char *err = NULL;
311 /* Is the directory spec'd correctly */
312 if (*path != '/') {
313 return "path is not absolute (it must start with a \"/\")";
315 else {
316 int i = strlen(path) - 1;
318 /* Strip trailing "/"s */
319 while(i > 0 && path[i] == '/') path[i--] = '\0';
322 /* Does it exist? */
323 if (stat(path, &finfo) != 0) {
324 /* No, but maybe we can create it */
325 #ifdef WIN32
326 if (mkdir(path) != 0)
327 #else
328 if (mkdir(path, S_IRWXU) != 0)
329 #endif
331 return ap_psprintf(tp,
332 "doesn't exist and can't be created: %s",
333 strerror(errno));
336 #ifndef WIN32
337 /* If we're root, we're gonna setuid/setgid so we need to chown */
338 if (geteuid() == 0 && chown(path, ap_user_id, ap_group_id) != 0) {
339 return ap_psprintf(tp,
340 "can't chown() to the server (uid %ld, gid %ld): %s",
341 (long)ap_user_id, (long)ap_group_id, strerror(errno));
343 #endif
345 else {
346 /* Yes, is it a directory? */
347 if (!S_ISDIR(finfo.st_mode))
348 return "isn't a directory!";
350 /* Can we RWX in there? */
351 #ifdef WIN32
352 err = fcgi_util_check_access(tp, NULL, &finfo, _S_IREAD | _S_IWRITE | _S_IEXEC, fcgi_user_id, fcgi_group_id);
353 #else
354 err = fcgi_util_check_access(tp, NULL, &finfo, R_OK | W_OK | X_OK,
355 fcgi_user_id, fcgi_group_id);
356 #endif
357 if (err != NULL) {
358 return ap_psprintf(tp,
359 "access for server (uid %ld, gid %ld) failed: %s",
360 (long)fcgi_user_id, (long)fcgi_group_id, err);
363 return NULL;
366 /*******************************************************************************
367 * Create a "dynamic" subdirectory. If the directory
368 * already exists we don't mess with it unless 'wax' is set.
370 const char *fcgi_config_make_dynamic_dir(pool *p, const int wax)
372 #ifndef WIN32
373 const char *err;
374 pool *tp;
376 fcgi_dynamic_dir = ap_pstrcat(p, fcgi_socket_dir, "/dynamic", NULL);
378 if ((err = fcgi_config_make_dir(p, fcgi_dynamic_dir)))
379 return ap_psprintf(p, "can't create dynamic directory \"%s\": %s", fcgi_dynamic_dir, err);
381 /* Don't step on a running server unless its OK. */
382 if (!wax)
383 return NULL;
385 #ifdef APACHE2
387 apr_dir_t * dir;
388 apr_finfo_t finfo;
390 if (apr_pool_create(&tp, p))
391 return "apr_pool_create() failed";
393 if (apr_dir_open(&dir, fcgi_dynamic_dir, tp))
394 return "apr_dir_open() failed";
396 /* delete the contents */
398 while (apr_dir_read(&finfo, APR_FINFO_NAME, dir) == APR_SUCCESS)
400 if (strcmp(finfo.name, ".") == 0 || strcmp(finfo.name, "..") == 0)
401 continue;
403 apr_file_remove(finfo.name, tp);
407 #else /* !APACHE2 */
409 DIR *dp;
410 struct dirent *dirp = NULL;
412 tp = ap_make_sub_pool(p);
414 dp = ap_popendir(tp, fcgi_dynamic_dir);
415 if (dp == NULL) {
416 ap_destroy_pool(tp);
417 return ap_psprintf(p, "can't open dynamic directory \"%s\": %s",
418 fcgi_dynamic_dir, strerror(errno));
421 /* delete the contents */
423 while ((dirp = readdir(dp)) != NULL)
425 if (strcmp(dirp->d_name, ".") == 0 || strcmp(dirp->d_name, "..") == 0)
426 continue;
428 unlink(ap_pstrcat(tp, fcgi_dynamic_dir, "/", dirp->d_name, NULL));
432 #endif /* !APACHE2 */
434 ap_destroy_pool(tp);
436 #else
437 fcgi_dynamic_dir = ap_pstrcat(p, fcgi_socket_dir, "dynamic", NULL);
438 #endif
439 return NULL;
443 /*******************************************************************************
444 * Change the directory used for the Unix/Domain sockets from the default.
445 * Create the directory and the "dynamic" subdirectory.
447 const char *fcgi_config_set_socket_dir(cmd_parms *cmd, void *dummy, const char *arg)
449 pool * const tp = cmd->temp_pool;
450 const char * const name = cmd->cmd->name;
451 const char *err;
452 char * arg_nc;
454 if (strcmp(fcgi_socket_dir, DEFAULT_SOCK_DIR) != 0) {
455 return ap_psprintf(tp, "%s %s: already defined as \"%s\"",
456 name, arg, fcgi_socket_dir);
459 err = fcgi_config_set_fcgi_uid_n_gid(1);
460 if (err != NULL)
461 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
463 if (fcgi_servers != NULL) {
464 return ap_psprintf(tp,
465 "The %s command must preceed static FastCGI server definitions",
466 name);
469 arg_nc = ap_pstrdup(cmd->pool, arg);
471 #ifndef WIN32
473 #ifdef APACHE2
474 if (apr_filepath_merge(&arg_nc, "", arg, 0, cmd->pool))
475 return ap_psprintf(tp, "%s %s: invalid filepath", name, arg);
476 #else
477 arg_nc = ap_os_canonical_filename(cmd->pool, arg_nc);
478 #endif
480 arg_nc = ap_server_root_relative(cmd->pool, arg_nc);
482 #else /* WIN32 */
484 if (strncmp(arg_nc, "\\\\.\\pipe\\", 9) != 0)
485 return ap_psprintf(tp, "%s %s is invalid format",name, arg_nc);
487 #endif
489 fcgi_socket_dir = arg_nc;
491 #ifndef WIN32
492 err = fcgi_config_make_dir(tp, fcgi_socket_dir);
493 if (err != NULL)
494 return ap_psprintf(tp, "%s %s: %s", name, arg_nc, err);
496 err = fcgi_config_make_dynamic_dir(cmd->pool, 0);
497 if (err != NULL)
498 return ap_psprintf(tp, "%s %s: %s", name, arg_nc, err);
499 #endif
501 return NULL;
504 /*******************************************************************************
505 * Enable, disable, or specify the path to a wrapper used to invoke all
506 * FastCGI applications.
508 const char *fcgi_config_set_wrapper(cmd_parms *cmd, void *dummy, const char *arg)
510 #ifdef APACHE2
512 * AP2TODO use ap_run_get_suexec_identity() (this will be hard as it
513 * takes a request_rec) and/or mod_userdir_user note
515 return ap_psprintf(cmd->temp_pool, "%s isn't supported yet under Apache2", cmd->cmd->name);
516 #else
517 const char *err = NULL;
518 const char * const name = cmd->cmd->name;
519 pool * const tp = cmd->temp_pool;
520 char * wrapper;
522 if (!ap_suexec_enabled && strcasecmp(arg, "On") == 0) {
523 fprintf(stderr, "Warning: \"%s On\" requires SUEXEC be enabled in Apache", name);
524 return NULL;
527 err = fcgi_config_set_fcgi_uid_n_gid(1);
528 if (err != NULL)
529 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
531 if (fcgi_servers != NULL) {
532 return ap_psprintf(tp,
533 "The %s command must preceed static FastCGI server definitions", name);
536 if (strcasecmp(arg, "On") == 0) {
537 fcgi_wrapper = SUEXEC_BIN;
539 else if (strcasecmp(arg, "Off") == 0) {
540 fcgi_wrapper = NULL;
542 else {
543 wrapper = (char *) ap_os_canonical_filename(cmd->pool, arg);
544 wrapper = ap_server_root_relative(cmd->pool, wrapper);
546 #ifdef WIN32
547 err = fcgi_util_check_access(tp, wrapper, NULL, _S_IEXEC, fcgi_user_id, fcgi_group_id);
548 #else
549 err = fcgi_util_check_access(tp, wrapper, NULL, X_OK, fcgi_user_id, fcgi_group_id);
550 #endif
552 if (err != NULL) {
553 return ap_psprintf(tp,
554 "%s: \"%s\" access for server (uid %ld, gid %ld) failed: %s",
555 name, wrapper, (long)fcgi_user_id, (long)fcgi_group_id, err);
558 fcgi_wrapper = wrapper;
560 return NULL;
561 #endif /* !APACHE2 */
564 /*******************************************************************************
565 * Configure a static FastCGI server.
567 const char *fcgi_config_new_static_server(cmd_parms *cmd, void *dummy, const char *arg)
569 fcgi_server *s;
570 pool *p = cmd->pool, *tp = cmd->temp_pool;
571 const char *name = cmd->cmd->name;
572 char *fs_path = ap_getword_conf(p, &arg);
573 const char *option, *err;
575 /* Allocate temp storage for the array of initial environment variables */
576 char **envp = ap_pcalloc(tp, sizeof(char *) * (MAX_INIT_ENV_VARS + 3));
577 unsigned int envc = 0;
579 #ifdef WIN32
580 HANDLE mutex;
581 #endif
583 if (*fs_path == '\0')
584 return "AppClass requires a pathname!?";
586 if ((err = fcgi_config_set_fcgi_uid_n_gid(1)) != NULL)
587 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
589 #ifdef APACHE2
590 if (apr_filepath_merge(&fs_path, "", fs_path, 0, p))
591 return ap_psprintf(tp, "%s %s: invalid filepath", name, fs_path);
592 #else
593 fs_path = ap_os_canonical_filename(p, fs_path);
594 #endif
595 fs_path = ap_server_root_relative(p, fs_path);
597 ap_getparents(fs_path);
598 ap_no2slash(fs_path);
600 /* See if we've already got one of these configured */
601 s = fcgi_util_fs_get_by_id(fs_path, fcgi_util_get_server_uid(cmd->server),
602 fcgi_util_get_server_gid(cmd->server));
603 if (s != NULL) {
604 if (fcgi_wrapper) {
605 return ap_psprintf(tp,
606 "%s: redefinition of a previously defined FastCGI "
607 "server \"%s\" with uid=%ld and gid=%ld",
608 name, fs_path, (long) fcgi_util_get_server_uid(cmd->server),
609 (long) fcgi_util_get_server_gid(cmd->server));
611 else {
612 return ap_psprintf(tp,
613 "%s: redefinition of a previously defined FastCGI server \"%s\"",
614 name, fs_path);
618 err = fcgi_util_fs_is_path_ok(tp, fs_path, NULL);
619 if (err != NULL) {
620 return ap_psprintf(tp, "%s: \"%s\" %s", name, fs_path, err);
623 s = fcgi_util_fs_new(p);
624 s->fs_path = fs_path;
625 s->directive = APP_CLASS_STANDARD;
626 s->restartOnExit = TRUE;
627 s->numProcesses = 1;
629 #ifdef WIN32
631 // TCP FastCGI applications require SystemRoot be present in the environment
632 // Put it in both for consistency to the application
633 fcgi_config_set_env_var(tp, envp, &envc, "SystemRoot");
635 mutex = CreateMutex(NULL, FALSE, fs_path);
637 if (mutex == NULL)
639 ap_log_error(FCGI_LOG_ALERT, fcgi_apache_main_server,
640 "FastCGI: CreateMutex() failed");
641 return "failed to create FastCGI application accept mutex";
644 SetHandleInformation(mutex, HANDLE_FLAG_INHERIT, TRUE);
646 s->mutex_env_string = ap_psprintf(p, "_FCGI_MUTEX_=%ld", mutex);
648 #elif !defined(APACHE2)
650 if (fcgi_wrapper) {
651 struct passwd *pw;
652 struct group *gr;
654 s->uid = cmd->server->server_uid;
655 pw = getpwuid(s->uid);
656 if (pw == NULL) {
657 return ap_psprintf(tp, "mod_fastcgi: "
658 "getpwuid() couldn't determine the username for uid '%ld', "
659 "you probably need to modify the User directive: %s",
660 (long)s->uid, strerror(errno));
662 s->user = ap_pstrdup(p, pw->pw_name);
663 s->username = s->user;
665 s->gid = cmd->server->server_gid;
666 gr = getgrgid(s->gid);
667 if (gr == NULL) {
668 return ap_psprintf(tp, "mod_fastcgi: "
669 "getgrgid() couldn't determine the group name for gid '%ld', "
670 "you probably need to modify the Group directive: %s\n",
671 (long)s->gid, strerror(errno));
673 s->group = ap_pstrdup(p, gr->gr_name);
675 #endif
677 /* Parse directive arguments */
678 while (*arg) {
679 option = ap_getword_conf(tp, &arg);
681 if (strcasecmp(option, "-processes") == 0) {
682 if ((err = get_u_int(tp, &arg, &s->numProcesses, 1)))
683 return invalid_value(tp, name, fs_path, option, err);
685 else if (strcasecmp(option, "-restart-delay") == 0) {
686 if ((err = get_u_int(tp, &arg, &s->restartDelay, 0)))
687 return invalid_value(tp, name, fs_path, option, err);
689 else if (strcasecmp(option, "-init-start-delay") == 0) {
690 if ((err = get_int(tp, &arg, &s->initStartDelay, 0)))
691 return invalid_value(tp, name, fs_path, option, err);
693 else if (strcasecmp(option, "-priority") == 0) {
694 if ((err = get_u_int(tp, &arg, &s->processPriority, 0)))
695 return invalid_value(tp, name, fs_path, option, err);
697 else if (strcasecmp(option, "-listen-queue-depth") == 0) {
698 if ((err = get_u_int(tp, &arg, &s->listenQueueDepth, 1)))
699 return invalid_value(tp, name, fs_path, option, err);
701 else if (strcasecmp(option, "-appConnTimeout") == 0) {
702 if ((err = get_u_int(tp, &arg, &s->appConnectTimeout, 0)))
703 return invalid_value(tp, name, fs_path, option, err);
705 else if (strcasecmp(option, "-idle-timeout") == 0) {
706 if ((err = get_u_int(tp, &arg, &s->idle_timeout, 1)))
707 return invalid_value(tp, name, fs_path, option, err);
709 else if (strcasecmp(option, "-port") == 0) {
710 if ((err = get_u_short(tp, &arg, &s->port, 1)))
711 return invalid_value(tp, name, fs_path, option, err);
713 else if (strcasecmp(option, "-socket") == 0) {
714 s->socket_path = ap_getword_conf(tp, &arg);
715 if (*s->socket_path == '\0')
716 return invalid_value(tp, name, fs_path, option, "\"\"");
718 else if (strcasecmp(option, "-initial-env") == 0) {
719 if ((err = get_env_var(p, &arg, envp, &envc)))
720 return invalid_value(tp, name, fs_path, option, err);
722 else if (strcasecmp(option, "-pass-header") == 0) {
723 if ((err = get_pass_header(p, &arg, &s->pass_headers)))
724 return invalid_value(tp, name, fs_path, option, err);
726 else if (strcasecmp(option, "-flush") == 0) {
727 s->flush = 1;
729 else {
730 return ap_psprintf(tp, "%s %s: invalid option: %s", name, fs_path, option);
732 } /* while */
734 if (s->socket_path != NULL && s->port != 0) {
735 return ap_psprintf(tp,
736 "%s %s: -port and -socket are mutually exclusive options",
737 name, fs_path);
740 /* Move env array to a surviving pool */
741 s->envp = (char **)ap_pcalloc(p, sizeof(char *) * (envc + 4));
742 memcpy(s->envp, envp, sizeof(char *) * envc);
744 /* Initialize process structs */
745 s->procs = fcgi_util_fs_create_procs(p, s->numProcesses);
747 /* Build the appropriate sockaddr structure */
748 if (s->port != 0) {
749 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->socket_addr,
750 &s->socket_addr_len, NULL, s->port);
751 if (err != NULL)
752 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
753 #ifdef WIN32
754 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->dest_addr,
755 &s->socket_addr_len, "localhost", s->port);
756 if (err != NULL)
757 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
758 #endif
759 } else {
760 if (s->socket_path == NULL)
761 s->socket_path = fcgi_util_socket_hash_filename(tp, fs_path, s->user, s->group);
762 s->socket_path = fcgi_util_socket_make_path_absolute(p, s->socket_path, 0);
763 #ifndef WIN32
764 err = fcgi_util_socket_make_domain_addr(p, (struct sockaddr_un **)&s->socket_addr,
765 &s->socket_addr_len, s->socket_path);
766 if (err != NULL)
767 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
768 #endif
771 /* Add it to the list of FastCGI servers */
772 fcgi_util_fs_add(s);
774 return NULL;
777 /*******************************************************************************
778 * Configure a static FastCGI server that is started/managed elsewhere.
780 const char *fcgi_config_new_external_server(cmd_parms *cmd, void *dummy, const char *arg)
782 fcgi_server *s;
783 pool * const p = cmd->pool, *tp = cmd->temp_pool;
784 const char * const name = cmd->cmd->name;
785 char *fs_path = ap_getword_conf(p, &arg);
786 const char *option, *err;
788 if (!*fs_path) {
789 return ap_pstrcat(tp, name, " requires a path and either a -socket or -host option", NULL);
792 #ifdef APACHE2
793 if (apr_filepath_merge(&fs_path, "", fs_path, 0, p))
794 return ap_psprintf(tp, "%s %s: invalid filepath", name, fs_path);
795 #else
796 fs_path = ap_os_canonical_filename(p, fs_path);
797 #endif
799 fs_path = ap_server_root_relative(p, fs_path);
801 ap_getparents(fs_path);
802 ap_no2slash(fs_path);
804 /* See if we've already got one of these bettys configured */
805 s = fcgi_util_fs_get_by_id(fs_path, fcgi_util_get_server_uid(cmd->server),
806 fcgi_util_get_server_gid(cmd->server));
807 if (s != NULL) {
808 if (fcgi_wrapper) {
809 return ap_psprintf(tp,
810 "%s: redefinition of a previously defined class \"%s\" "
811 "with uid=%ld and gid=%ld",
812 name, fs_path, (long) fcgi_util_get_server_uid(cmd->server),
813 (long) fcgi_util_get_server_gid(cmd->server));
815 else
817 return ap_psprintf(tp,
818 "%s: redefinition of previously defined class \"%s\"", name, fs_path);
822 s = fcgi_util_fs_new(p);
823 s->fs_path = fs_path;
824 s->directive = APP_CLASS_EXTERNAL;
826 err = fcgi_util_fs_set_uid_n_gid(p, s, fcgi_util_get_server_uid(cmd->server),
827 fcgi_util_get_server_gid(cmd->server));
828 if (err != NULL)
829 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
831 /* Parse directive arguments */
832 while (*arg != '\0') {
833 option = ap_getword_conf(tp, &arg);
835 if (strcasecmp(option, "-host") == 0) {
836 if ((err = get_host_n_port(p, &arg, &s->host, &s->port)))
837 return invalid_value(tp, name, fs_path, option, err);
839 else if (strcasecmp(option, "-socket") == 0) {
840 s->socket_path = ap_getword_conf(tp, &arg);
841 if (*s->socket_path == '\0')
842 return invalid_value(tp, name, fs_path, option, "\"\"");
844 else if (strcasecmp(option, "-appConnTimeout") == 0) {
845 if ((err = get_u_int(tp, &arg, &s->appConnectTimeout, 0)))
846 return invalid_value(tp, name, fs_path, option, err);
848 else if (strcasecmp(option, "-idle-timeout") == 0) {
849 if ((err = get_u_int(tp, &arg, &s->idle_timeout, 1)))
850 return invalid_value(tp, name, fs_path, option, err);
852 else if (strcasecmp(option, "-pass-header") == 0) {
853 if ((err = get_pass_header(p, &arg, &s->pass_headers)))
854 return invalid_value(tp, name, fs_path, option, err);
856 else if (strcasecmp(option, "-flush") == 0) {
857 s->flush = 1;
859 else {
860 return ap_psprintf(tp, "%s %s: invalid option: %s", name, fs_path, option);
862 } /* while */
864 /* Require one of -socket or -host, but not both */
865 if (s->socket_path != NULL && s->port != 0) {
866 return ap_psprintf(tp,
867 "%s %s: -host and -socket are mutually exclusive options",
868 name, fs_path);
870 if (s->socket_path == NULL && s->port == 0) {
871 return ap_psprintf(tp,
872 "%s %s: -socket or -host option missing", name, fs_path);
875 /* Build the appropriate sockaddr structure */
876 if (s->port != 0) {
877 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->socket_addr,
878 &s->socket_addr_len, s->host, s->port);
879 if (err != NULL)
880 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
881 } else {
882 s->socket_path = fcgi_util_socket_make_path_absolute(p, s->socket_path, 0);
883 #ifndef WIN32
884 err = fcgi_util_socket_make_domain_addr(p, (struct sockaddr_un **)&s->socket_addr,
885 &s->socket_addr_len, s->socket_path);
886 if (err != NULL)
887 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
888 #endif
891 /* Add it to the list of FastCGI servers */
892 fcgi_util_fs_add(s);
894 return NULL;
898 *----------------------------------------------------------------------
900 * fcgi_config_set_config --
902 * Implements the FastCGI FCGIConfig configuration directive.
903 * This command adds routines to control the execution of the
904 * dynamic FastCGI processes.
907 *----------------------------------------------------------------------
909 const char *fcgi_config_set_config(cmd_parms *cmd, void *dummy, const char *arg)
911 pool * const p = cmd->pool;
912 pool * const tp = cmd->temp_pool;
913 const char *err, *option;
914 const char * const name = cmd->cmd->name;
916 /* Allocate temp storage for an initial environment */
917 unsigned int envc = 0;
918 char **envp = (char **)ap_pcalloc(tp, sizeof(char *) * (MAX_INIT_ENV_VARS + 3));
920 /* Parse the directive arguments */
921 while (*arg) {
922 option = ap_getword_conf(tp, &arg);
924 if (strcasecmp(option, "-maxProcesses") == 0) {
925 if ((err = get_u_int(tp, &arg, &dynamicMaxProcs, 1)))
926 return invalid_value(tp, name, NULL, option, err);
928 else if (strcasecmp(option, "-minProcesses") == 0) {
929 if ((err = get_int(tp, &arg, &dynamicMinProcs, 0)))
930 return invalid_value(tp, name, NULL, option, err);
932 else if (strcasecmp(option, "-maxClassProcesses") == 0) {
933 if ((err = get_int(tp, &arg, &dynamicMaxClassProcs, 1)))
934 return invalid_value(tp, name, NULL, option, err);
936 else if (strcasecmp(option, "-killInterval") == 0) {
937 if ((err = get_u_int(tp, &arg, &dynamicKillInterval, 1)))
938 return invalid_value(tp, name, NULL, option, err);
940 else if (strcasecmp(option, "-updateInterval") == 0) {
941 if ((err = get_u_int(tp, &arg, &dynamicUpdateInterval, 1)))
942 return invalid_value(tp, name, NULL, option, err);
944 else if (strcasecmp(option, "-gainValue") == 0) {
945 if ((err = get_float(tp, &arg, &dynamicGain, 0.0, 1.0)))
946 return invalid_value(tp, name, NULL, option, err);
948 else if ((strcasecmp(option, "-singleThreshold") == 0)
949 || (strcasecmp(option, "-singleThreshhold") == 0))
951 if ((err = get_int(tp, &arg, &dynamicThreshold1, 0)))
952 return invalid_value(tp, name, NULL, option, err);
954 else if ((strcasecmp(option, "-multiThreshold") == 0)
955 || (strcasecmp(option, "-multiThreshhold") == 0))
957 if ((err = get_int(tp, &arg, &dynamicThresholdN, 0)))
958 return invalid_value(tp, name, NULL, option, err);
960 else if (strcasecmp(option, "-startDelay") == 0) {
961 if ((err = get_u_int(tp, &arg, &dynamicPleaseStartDelay, 1)))
962 return invalid_value(tp, name, NULL, option, err);
964 else if (strcasecmp(option, "-initial-env") == 0) {
965 if ((err = get_env_var(p, &arg, envp, &envc)))
966 return invalid_value(tp, name, NULL, option, err);
968 else if (strcasecmp(option, "-pass-header") == 0) {
969 if ((err = get_pass_header(p, &arg, &dynamic_pass_headers)))
970 return invalid_value(tp, name, NULL, option, err);
972 else if (strcasecmp(option, "-appConnTimeout") == 0) {
973 if ((err = get_u_int(tp, &arg, &dynamicAppConnectTimeout, 0)))
974 return invalid_value(tp, name, NULL, option, err);
976 else if (strcasecmp(option, "-idle-timeout") == 0) {
977 if ((err = get_u_int(tp, &arg, &dynamic_idle_timeout, 1)))
978 return invalid_value(tp, name, NULL, option, err);
980 else if (strcasecmp(option, "-listen-queue-depth") == 0) {
981 if ((err = get_u_int(tp, &arg, &dynamicListenQueueDepth, 1)))
982 return invalid_value(tp, name, NULL, option, err);
984 else if (strcasecmp(option, "-restart-delay") == 0) {
985 if ((err = get_u_int(tp, &arg, &dynamicRestartDelay, 0)))
986 return invalid_value(tp, name, NULL, option, err);
988 else if (strcasecmp(option, "-init-start-delay") == 0) {
989 if ((err = get_u_int(tp, &arg, &dynamicInitStartDelay, 0)))
990 return invalid_value(tp, name, NULL, option, err);
992 else if (strcasecmp(option, "-processSlack") == 0) {
993 if ((err = get_u_int(tp, &arg, &dynamicProcessSlack, 1)))
994 return invalid_value(tp, name, NULL, option, err);
996 else if (strcasecmp(option, "-restart") == 0) {
997 dynamicAutoRestart = 1;
999 else if (strcasecmp(option, "-autoUpdate") == 0) {
1000 dynamicAutoUpdate = 1;
1002 else if (strcasecmp(option, "-flush") == 0) {
1003 dynamicFlush = TRUE;
1005 else {
1006 return ap_psprintf(tp, "%s: invalid option: %s", name, option);
1008 } /* while */
1010 if (dynamicProcessSlack >= dynamicMaxProcs + 1) {
1011 /* the kill policy would work unexpectedly */
1012 return ap_psprintf(tp,
1013 "%s: processSlack (%u) must be less than maxProcesses (%u) + 1",
1014 name, dynamicProcessSlack, dynamicMaxProcs);
1017 /* Move env array to a surviving pool, leave 2 extra slots for
1018 * WIN32 _FCGI_MUTEX_ and _FCGI_SHUTDOWN_EVENT_ */
1019 dynamicEnvp = (char **)ap_pcalloc(p, sizeof(char *) * (envc + 4));
1020 memcpy(dynamicEnvp, envp, sizeof(char *) * envc);
1022 return NULL;
1025 void *fcgi_config_create_dir_config(pool *p, char *dummy)
1027 fcgi_dir_config *dir_config = ap_pcalloc(p, sizeof(fcgi_dir_config));
1029 dir_config->authenticator_options = FCGI_AUTHORITATIVE;
1030 dir_config->authorizer_options = FCGI_AUTHORITATIVE;
1031 dir_config->access_checker_options = FCGI_AUTHORITATIVE;
1033 return dir_config;
1037 const char *fcgi_config_new_auth_server(cmd_parms * cmd,
1038 void * dircfg, const char *fs_path, const char * compat)
1040 fcgi_dir_config * dir_config = (fcgi_dir_config *) dircfg;
1041 pool * const tp = cmd->temp_pool;
1042 char * auth_server;
1044 #ifdef APACHE2
1045 if (apr_filepath_merge(&auth_server, "", fs_path, 0, cmd->pool))
1046 return ap_psprintf(tp, "%s %s: invalid filepath", cmd->cmd->name, fs_path);
1047 #else
1048 auth_server = (char *) ap_os_canonical_filename(cmd->pool, fs_path);
1049 #endif
1051 auth_server = ap_server_root_relative(cmd->pool, auth_server);
1053 /* Make sure its already configured or at least a candidate for dynamic */
1054 if (fcgi_util_fs_get_by_id(auth_server, fcgi_util_get_server_uid(cmd->server),
1055 fcgi_util_get_server_gid(cmd->server)) == NULL)
1057 const char *err = fcgi_util_fs_is_path_ok(tp, auth_server, NULL);
1058 if (err)
1059 return ap_psprintf(tp, "%s: \"%s\" %s", cmd->cmd->name, auth_server, err);
1062 if (compat && strcasecmp(compat, "-compat"))
1063 return ap_psprintf(cmd->temp_pool, "%s: unknown option: \"%s\"", cmd->cmd->name, compat);
1065 switch((int)cmd->info) {
1066 case FCGI_AUTH_TYPE_AUTHENTICATOR:
1067 dir_config->authenticator = auth_server;
1068 dir_config->authenticator_options |= (compat) ? FCGI_COMPAT : 0;
1069 break;
1070 case FCGI_AUTH_TYPE_AUTHORIZER:
1071 dir_config->authorizer = auth_server;
1072 dir_config->authorizer_options |= (compat) ? FCGI_COMPAT : 0;
1073 break;
1074 case FCGI_AUTH_TYPE_ACCESS_CHECKER:
1075 dir_config->access_checker = auth_server;
1076 dir_config->access_checker_options |= (compat) ? FCGI_COMPAT : 0;
1077 break;
1080 return NULL;
1083 const char *fcgi_config_set_authoritative_slot(cmd_parms * cmd,
1084 void * dir_config, int arg)
1086 int offset = (int)(long)cmd->info;
1088 if (arg)
1089 *((u_char *)dir_config + offset) |= FCGI_AUTHORITATIVE;
1090 else
1091 *((u_char *)dir_config + offset) &= ~FCGI_AUTHORITATIVE;
1093 return NULL;