*) Eliminate the use of locks to assist in the clean shutdown of
[mod_fastcgi.git] / fcgi_config.c
blob510355c6f246a04a5c53fa6d97dd5110208f3306
1 /*
2 * $Id: fcgi_config.c,v 1.26 2001/03/26 15:35:39 robs Exp $
3 */
5 #include "fcgi.h"
7 #ifdef WIN32
8 #pragma warning( disable : 4100 4706 )
9 #endif
11 /*******************************************************************************
12 * Get the next configuration directive argument, & return an in_addr and port.
13 * The arg must be in the form "host:port" where host can be an IP or hostname.
14 * The pool arg should be persistant storage.
16 static const char *get_host_n_port(pool *p, const char **arg,
17 const char **host, u_short *port)
19 char *cvptr, *portStr;
20 long tmp;
22 *host = ap_getword_conf(p, arg);
23 if (**host == '\0')
24 return "\"\"";
26 portStr = strchr(*host, ':');
27 if (portStr == NULL)
28 return "missing port specification";
30 /* Split the host and port portions */
31 *portStr++ = '\0';
33 /* Convert port number */
34 tmp = (u_short) strtol(portStr, &cvptr, 10);
35 if (*cvptr != '\0' || tmp < 1 || tmp > USHRT_MAX)
36 return ap_pstrcat(p, "bad port number \"", portStr, "\"", NULL);
38 *port = (unsigned short) tmp;
40 return NULL;
43 /*******************************************************************************
44 * Get the next configuration directive argument, & return an u_short.
45 * The pool arg should be temporary storage.
47 static const char *get_u_short(pool *p, const char **arg,
48 u_short *num, u_short min)
50 char *ptr;
51 long tmp;
52 const char *txt = ap_getword_conf(p, arg);
54 if (*txt == '\0') {
55 return "\"\"";
58 tmp = strtol(txt, &ptr, 10);
60 if (*ptr != '\0') {
61 return ap_pstrcat(p, "\"", txt, "\" must be a positive integer", NULL);
64 if (tmp < min || tmp > USHRT_MAX) {
65 return ap_psprintf(p, "\"%u\" must be >= %u and < %u", *num, min, USHRT_MAX);
68 *num = (u_short) tmp;
70 return NULL;
73 static const char *get_int(pool *p, const char **arg, int *num, int min)
75 char *cp;
76 const char *val = ap_getword_conf(p, arg);
78 if (*val == '\0')
80 return "\"\"";
83 *num = (int) strtol(val, &cp, 10);
85 if (*cp != '\0')
87 return ap_pstrcat(p, "can't parse ", "\"", val, "\"", NULL);
89 else if (*num < min)
91 return ap_psprintf(p, "\"%d\" must be >= %d", *num, min);
94 return NULL;
97 /*******************************************************************************
98 * Get the next configuration directive argument, & return an u_int.
99 * The pool arg should be temporary storage.
101 static const char *get_u_int(pool *p, const char **arg,
102 u_int *num, u_int min)
104 char *ptr;
105 const char *val = ap_getword_conf(p, arg);
107 if (*val == '\0')
108 return "\"\"";
109 *num = (u_int)strtol(val, &ptr, 10);
111 if (*ptr != '\0')
112 return ap_pstrcat(p, "\"", val, "\" must be a positive integer", NULL);
113 else if (*num < min)
114 return ap_psprintf(p, "\"%u\" must be >= %u", *num, min);
115 return NULL;
118 /*******************************************************************************
119 * Get the next configuration directive argument, & return a float.
120 * The pool arg should be temporary storage.
122 static const char *get_float(pool *p, const char **arg,
123 float *num, float min, float max)
125 char *ptr;
126 const char *val = ap_getword_conf(p, arg);
128 if (*val == '\0')
129 return "\"\"";
130 *num = (float) strtod(val, &ptr);
132 if (*ptr != '\0')
133 return ap_pstrcat(p, "\"", val, "\" is not a floating point number", NULL);
134 if (*num < min || *num > max)
135 return ap_psprintf(p, "\"%f\" is not between %f and %f", *num, min, max);
136 return NULL;
139 const char *fcgi_config_set_env_var(pool *p, char **envp, unsigned int *envc, char * var)
141 if (*envc >= MAX_INIT_ENV_VARS) {
142 return "too many variables, must be <= MAX_INIT_ENV_VARS";
145 if (strchr(var, '=') == NULL) {
146 *(envp + *envc) = ap_pstrcat(p, var, "=", getenv(var), NULL);
148 else {
149 *(envp + *envc) = var;
152 (*envc)++;
154 return NULL;
157 /*******************************************************************************
158 * Get the next configuration directive argument, & add it to an env array.
159 * The pool arg should be permanent storage.
161 static const char *get_env_var(pool *p, const char **arg, char **envp, unsigned int *envc)
163 char * const val = ap_getword_conf(p, arg);
165 if (*val == '\0') {
166 return "\"\"";
169 return fcgi_config_set_env_var(p, envp, envc, val);
172 static const char *get_pass_header(pool *p, const char **arg, array_header **array)
174 const char **header;
176 if (!*array) {
177 *array = ap_make_array(p, 10, sizeof(char*));
180 header = (const char **)ap_push_array(*array);
181 *header = ap_getword_conf(p, arg);
183 return header ? NULL : "\"\"";
186 /*******************************************************************************
187 * Return a "standard" message for common configuration errors.
189 static const char *invalid_value(pool *p, const char *cmd, const char *id,
190 const char *opt, const char *err)
192 return ap_psprintf(p, "%s%s%s: invalid value for %s: %s",
193 cmd, id ? " " : "", id ? id : "", opt, err);
196 /*******************************************************************************
197 * Set/Reset the uid/gid that Apache and the PM will run as. This is ap_user_id
198 * and ap_group_id if we're started as root, and euid/egid otherwise. Also try
199 * to check that the config files don't set the User/Group after a FastCGI
200 * directive is used that depends on it.
202 /*@@@ To be complete, we should save a handle to the server each AppClass is
203 * configured in and at init() check that the user/group is still what we
204 * thought it was. Also the other directives should only be allowed in the
205 * parent Apache server.
207 const char *fcgi_config_set_fcgi_uid_n_gid(int set)
209 static int isSet = 0;
210 #ifndef WIN32
211 uid_t uid = geteuid();
212 gid_t gid = getegid();
214 if (set == 0) {
215 isSet = 0;
216 fcgi_user_id = (uid_t)-1;
217 fcgi_group_id = (gid_t)-1;
218 return NULL;
221 uid = uid ? uid : ap_user_id;
222 gid = gid ? gid : ap_group_id;
224 if (isSet && (uid != fcgi_user_id || gid != fcgi_group_id)) {
225 return "User/Group commands must preceed FastCGI server definitions";
228 isSet = 1;
229 fcgi_user_id = uid;
230 fcgi_group_id = gid;
231 #endif
232 return NULL;
235 void fcgi_config_reset_globals(void* dummy)
237 fcgi_config_pool = NULL;
238 fcgi_servers = NULL;
239 fcgi_config_set_fcgi_uid_n_gid(0);
240 fcgi_wrapper = NULL;
241 fcgi_socket_dir = DEFAULT_SOCK_DIR;
243 fcgi_dynamic_total_proc_count = 0;
244 fcgi_dynamic_epoch = 0;
245 fcgi_dynamic_last_analyzed = 0;
247 dynamicMaxProcs = FCGI_DEFAULT_MAX_PROCS;
248 dynamicMinProcs = FCGI_DEFAULT_MIN_PROCS;
249 dynamicMaxClassProcs = FCGI_DEFAULT_MAX_CLASS_PROCS;
250 dynamicKillInterval = FCGI_DEFAULT_KILL_INTERVAL;
251 dynamicUpdateInterval = FCGI_DEFAULT_UPDATE_INTERVAL;
252 dynamicGain = FCGI_DEFAULT_GAIN;
253 dynamicThreshold1 = FCGI_DEFAULT_THRESHOLD_1;
254 dynamicThresholdN = FCGI_DEFAULT_THRESHOLD_N;
255 dynamicPleaseStartDelay = FCGI_DEFAULT_START_PROCESS_DELAY;
256 dynamicAppConnectTimeout = FCGI_DEFAULT_APP_CONN_TIMEOUT;
257 dynamicEnvp = &fcgi_empty_env;
258 dynamicProcessSlack = FCGI_DEFAULT_PROCESS_SLACK;
259 dynamicAutoRestart = FCGI_DEFAULT_RESTART_DYNAMIC;
260 dynamicAutoUpdate = FCGI_DEFAULT_AUTOUPDATE;
261 dynamicListenQueueDepth = FCGI_DEFAULT_LISTEN_Q;
262 dynamicInitStartDelay = DEFAULT_INIT_START_DELAY;
263 dynamicRestartDelay = FCGI_DEFAULT_RESTART_DELAY;
264 dynamic_pass_headers = NULL;
265 dynamic_idle_timeout = FCGI_DEFAULT_IDLE_TIMEOUT;
268 /*******************************************************************************
269 * Create a directory to hold Unix/Domain sockets.
271 const char *fcgi_config_make_dir(pool *tp, char *path)
273 struct stat finfo;
274 const char *err = NULL;
276 /* Is the directory spec'd correctly */
277 if (*path != '/') {
278 return "path is not absolute (it must start with a \"/\")";
280 else {
281 int i = strlen(path) - 1;
283 /* Strip trailing "/"s */
284 while(i > 0 && path[i] == '/') path[i--] = '\0';
287 /* Does it exist? */
288 if (stat(path, &finfo) != 0) {
289 /* No, but maybe we can create it */
290 #ifdef WIN32
291 if (mkdir(path) != 0)
292 #else
293 if (mkdir(path, S_IRWXU) != 0)
294 #endif
296 return ap_psprintf(tp,
297 "doesn't exist and can't be created: %s",
298 strerror(errno));
301 #ifndef WIN32
302 /* If we're root, we're gonna setuid/setgid so we need to chown */
303 if (geteuid() == 0 && chown(path, ap_user_id, ap_group_id) != 0) {
304 return ap_psprintf(tp,
305 "can't chown() to the server (uid %ld, gid %ld): %s",
306 (long)ap_user_id, (long)ap_group_id, strerror(errno));
308 #endif
310 else {
311 /* Yes, is it a directory? */
312 if (!S_ISDIR(finfo.st_mode))
313 return "isn't a directory!";
315 /* Can we RWX in there? */
316 #ifdef WIN32
317 err = fcgi_util_check_access(tp, NULL, &finfo, _S_IREAD | _S_IWRITE | _S_IEXEC, fcgi_user_id, fcgi_group_id);
318 #else
319 err = fcgi_util_check_access(tp, NULL, &finfo, R_OK | W_OK | X_OK,
320 fcgi_user_id, fcgi_group_id);
321 #endif
322 if (err != NULL) {
323 return ap_psprintf(tp,
324 "access for server (uid %ld, gid %ld) failed: %s",
325 (long)fcgi_user_id, (long)fcgi_group_id, err);
328 return NULL;
331 /*******************************************************************************
332 * Create a "dynamic" subdirectory. If the directory
333 * already exists we don't mess with it unless 'wax' is set.
335 const char *fcgi_config_make_dynamic_dir(pool *p, const int wax)
337 #ifndef WIN32
338 DIR *dp = NULL;
339 struct dirent *dirp = NULL;
340 const char *err;
341 pool *tp;
343 fcgi_dynamic_dir = ap_pstrcat(p, fcgi_socket_dir, "/dynamic", NULL);
345 if ((err = fcgi_config_make_dir(p, fcgi_dynamic_dir)))
346 return ap_psprintf(p, "can't create dynamic directory \"%s\": %s", fcgi_dynamic_dir, err);
348 /* Don't step on a running server unless its OK. */
349 if (!wax)
350 return NULL;
352 /* Create a subpool for the directory operations */
353 tp = ap_make_sub_pool(p);
355 dp = ap_popendir(tp, fcgi_dynamic_dir);
356 if (dp == NULL) {
357 ap_destroy_pool(tp);
358 return ap_psprintf(p, "can't open dynamic directory \"%s\": %s",
359 fcgi_dynamic_dir, strerror(errno));
362 /* Delete everything in the directory, its all FCGI specific */
363 while ((dirp = readdir(dp)) != NULL) {
364 if (strcmp(dirp->d_name, ".") == 0
365 || strcmp(dirp->d_name, "..") == 0) {
366 continue;
369 unlink(ap_pstrcat(tp, fcgi_dynamic_dir, "/", dirp->d_name, NULL));
372 ap_destroy_pool(tp);
374 #else
375 fcgi_dynamic_dir = ap_pstrcat(p, fcgi_socket_dir, "dynamic", NULL);
376 #endif
377 return NULL;
381 /*******************************************************************************
382 * Change the directory used for the Unix/Domain sockets from the default.
383 * Create the directory and the "dynamic" subdirectory.
385 const char *fcgi_config_set_socket_dir(cmd_parms *cmd, void *dummy, char *arg)
387 pool * const tp = cmd->temp_pool;
388 const char * const name = cmd->cmd->name;
389 const char *err;
391 if (strcmp(fcgi_socket_dir, DEFAULT_SOCK_DIR) != 0) {
392 return ap_psprintf(tp, "%s %s: already defined as \"%s\"",
393 name, arg, fcgi_socket_dir);
396 err = fcgi_config_set_fcgi_uid_n_gid(1);
397 if (err != NULL)
398 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
400 if (fcgi_servers != NULL) {
401 return ap_psprintf(tp,
402 "The %s command must preceed static FastCGI server definitions",
403 name);
406 #ifndef WIN32
407 arg = ap_os_canonical_filename(cmd->pool, arg);
408 arg = ap_server_root_relative(cmd->pool, arg);
409 #else
410 if (strncmp(arg, "\\\\.\\pipe\\", 9) != 0)
411 return ap_psprintf(tp, "%s %s is invalid format",name, arg);
412 #endif
414 fcgi_socket_dir = arg;
416 #ifndef WIN32
417 err = fcgi_config_make_dir(tp, fcgi_socket_dir);
418 if (err != NULL)
419 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
421 err = fcgi_config_make_dynamic_dir(cmd->pool, 0);
422 if (err != NULL)
423 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
424 #endif
426 return NULL;
429 /*******************************************************************************
430 * Enable, disable, or specify the path to a wrapper used to invoke all
431 * FastCGI applications.
433 const char *fcgi_config_set_wrapper(cmd_parms *cmd, void *dummy, const char *arg)
435 const char *err = NULL;
436 const char * const name = cmd->cmd->name;
437 pool * const tp = cmd->temp_pool;
438 char * wrapper;
440 if (!ap_suexec_enabled && strcasecmp(arg, "On") == 0) {
441 fprintf(stderr, "Warning: \"%s On\" requires SUEXEC be enabled in Apache", name);
442 return NULL;
445 err = fcgi_config_set_fcgi_uid_n_gid(1);
446 if (err != NULL)
447 return ap_psprintf(tp, "%s %s: %s", name, arg, err);
449 if (fcgi_servers != NULL) {
450 return ap_psprintf(tp,
451 "The %s command must preceed static FastCGI server definitions", name);
454 if (strcasecmp(arg, "On") == 0) {
455 fcgi_wrapper = SUEXEC_BIN;
457 else if (strcasecmp(arg, "Off") == 0) {
458 fcgi_wrapper = NULL;
460 else {
461 wrapper = (char *) ap_os_canonical_filename(cmd->pool, arg);
462 wrapper = ap_server_root_relative(cmd->pool, wrapper);
464 #ifdef WIN32
465 err = fcgi_util_check_access(tp, wrapper, NULL, _S_IEXEC, fcgi_user_id, fcgi_group_id);
466 #else
467 err = fcgi_util_check_access(tp, wrapper, NULL, X_OK, fcgi_user_id, fcgi_group_id);
468 #endif
470 if (err != NULL) {
471 return ap_psprintf(tp,
472 "%s: \"%s\" access for server (uid %ld, gid %ld) failed: %s",
473 name, wrapper, (long)fcgi_user_id, (long)fcgi_group_id, err);
476 fcgi_wrapper = wrapper;
478 return NULL;
481 /*******************************************************************************
482 * Configure a static FastCGI server.
484 const char *fcgi_config_new_static_server(cmd_parms *cmd, void *dummy, const char *arg)
486 fcgi_server *s;
487 pool *p = cmd->pool, *tp = cmd->temp_pool;
488 const char *name = cmd->cmd->name;
489 char *fs_path = ap_getword_conf(p, &arg);
490 const char *option, *err;
492 /* Allocate temp storage for the array of initial environment variables */
493 char **envp = ap_pcalloc(tp, sizeof(char *) * (MAX_INIT_ENV_VARS + 3));
494 unsigned int envc = 0;
496 #ifdef WIN32
497 HANDLE mutex = ap_create_mutex(NULL);
499 SetHandleInformation(mutex, HANDLE_FLAG_INHERIT, TRUE);
500 #endif
502 if (*fs_path == '\0')
503 return "AppClass requires a pathname!?";
505 if ((err = fcgi_config_set_fcgi_uid_n_gid(1)) != NULL)
506 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
508 fs_path = ap_os_canonical_filename(p, fs_path);
509 fs_path = ap_server_root_relative(p, fs_path);
511 ap_getparents(fs_path);
512 ap_no2slash(fs_path);
514 /* See if we've already got one of these configured */
515 s = fcgi_util_fs_get_by_id(fs_path, cmd->server->server_uid,
516 cmd->server->server_gid);
517 if (s != NULL) {
518 if (fcgi_wrapper) {
519 return ap_psprintf(tp,
520 "%s: redefinition of a previously defined FastCGI server \"%s\" with uid=%ld and gid=%ld",
521 name, fs_path, (long)cmd->server->server_uid,
522 (long)cmd->server->server_gid);
524 else {
525 return ap_psprintf(tp,
526 "%s: redefinition of a previously defined FastCGI server \"%s\"",
527 name, fs_path);
531 err = fcgi_util_fs_is_path_ok(tp, fs_path, NULL);
532 if (err != NULL) {
533 return ap_psprintf(tp, "%s: \"%s\" %s", name, fs_path, err);
536 s = fcgi_util_fs_new(p);
537 s->fs_path = fs_path;
538 s->directive = APP_CLASS_STANDARD;
539 s->restartOnExit = TRUE;
540 s->numProcesses = 1;
542 #ifdef WIN32
543 // TCP FastCGI applications require SystemRoot be present in the environment
544 // Put it in both for consistency to the application
545 fcgi_config_set_env_var(tp, envp, &envc, "SystemRoot");
547 s->mutex_env_string = ap_psprintf(p, "_FCGI_MUTEX_=%ld", mutex);;
548 #else
549 if (fcgi_wrapper) {
550 struct passwd *pw;
551 struct group *gr;
553 s->uid = cmd->server->server_uid;
554 pw = getpwuid(s->uid);
555 if (pw == NULL) {
556 return ap_psprintf(tp, "mod_fastcgi: "
557 "getpwuid() couldn't determine the username for uid '%ld', "
558 "you probably need to modify the User directive: %s",
559 (long)s->uid, strerror(errno));
561 s->user = ap_pstrdup(p, pw->pw_name);
562 s->username = s->user;
564 s->gid = cmd->server->server_gid;
565 gr = getgrgid(s->gid);
566 if (gr == NULL) {
567 return ap_psprintf(tp, "mod_fastcgi: "
568 "getgrgid() couldn't determine the group name for gid '%ld', "
569 "you probably need to modify the Group directive: %s\n",
570 (long)s->gid, strerror(errno));
572 s->group = ap_pstrdup(p, gr->gr_name);
574 #endif
576 /* Parse directive arguments */
577 while (*arg) {
578 option = ap_getword_conf(tp, &arg);
580 if (strcasecmp(option, "-processes") == 0) {
581 if ((err = get_u_int(tp, &arg, &s->numProcesses, 1)))
582 return invalid_value(tp, name, fs_path, option, err);
584 else if (strcasecmp(option, "-restart-delay") == 0) {
585 if ((err = get_u_int(tp, &arg, &s->restartDelay, 0)))
586 return invalid_value(tp, name, fs_path, option, err);
588 else if (strcasecmp(option, "-init-start-delay") == 0) {
589 if ((err = get_int(tp, &arg, &s->initStartDelay, 0)))
590 return invalid_value(tp, name, fs_path, option, err);
592 else if (strcasecmp(option, "-priority") == 0) {
593 if ((err = get_u_int(tp, &arg, &s->processPriority, 0)))
594 return invalid_value(tp, name, fs_path, option, err);
596 else if (strcasecmp(option, "-listen-queue-depth") == 0) {
597 if ((err = get_u_int(tp, &arg, &s->listenQueueDepth, 1)))
598 return invalid_value(tp, name, fs_path, option, err);
600 else if (strcasecmp(option, "-appConnTimeout") == 0) {
601 if ((err = get_u_int(tp, &arg, &s->appConnectTimeout, 0)))
602 return invalid_value(tp, name, fs_path, option, err);
604 else if (strcasecmp(option, "-idle-timeout") == 0) {
605 if ((err = get_u_int(tp, &arg, &s->idle_timeout, 1)))
606 return invalid_value(tp, name, fs_path, option, err);
608 else if (strcasecmp(option, "-port") == 0) {
609 if ((err = get_u_short(tp, &arg, &s->port, 1)))
610 return invalid_value(tp, name, fs_path, option, err);
612 else if (strcasecmp(option, "-socket") == 0) {
613 s->socket_path = ap_getword_conf(tp, &arg);
614 if (*s->socket_path == '\0')
615 return invalid_value(tp, name, fs_path, option, "\"\"");
617 else if (strcasecmp(option, "-initial-env") == 0) {
618 if ((err = get_env_var(p, &arg, envp, &envc)))
619 return invalid_value(tp, name, fs_path, option, err);
621 else if (strcasecmp(option, "-pass-header") == 0) {
622 if ((err = get_pass_header(p, &arg, &s->pass_headers)))
623 return invalid_value(tp, name, fs_path, option, err);
625 else if (strcasecmp(option, "-flush") == 0) {
626 s->flush = 1;
628 else {
629 return ap_psprintf(tp, "%s %s: invalid option: %s", name, fs_path, option);
631 } /* while */
633 if (s->socket_path != NULL && s->port != 0) {
634 return ap_psprintf(tp,
635 "%s %s: -port and -socket are mutually exclusive options",
636 name, fs_path);
639 /* Move env array to a surviving pool */
640 ++envc;
641 s->envp = (char **)ap_palloc(p, sizeof(char *) * ++envc);
642 memcpy(s->envp, envp, sizeof(char *) * envc);
644 /* Initialize process structs */
645 s->procs = fcgi_util_fs_create_procs(p, s->numProcesses);
647 /* Build the appropriate sockaddr structure */
648 if (s->port != 0) {
649 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->socket_addr,
650 &s->socket_addr_len, NULL, s->port);
651 if (err != NULL)
652 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
653 #ifdef WIN32
654 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->dest_addr,
655 &s->socket_addr_len, "localhost", s->port);
656 if (err != NULL)
657 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
658 #endif
659 } else {
660 if (s->socket_path == NULL)
661 s->socket_path = fcgi_util_socket_hash_filename(tp, fs_path, s->user, s->group);
662 s->socket_path = fcgi_util_socket_make_path_absolute(p, s->socket_path, 0);
663 #ifndef WIN32
664 err = fcgi_util_socket_make_domain_addr(p, (struct sockaddr_un **)&s->socket_addr,
665 &s->socket_addr_len, s->socket_path);
666 if (err != NULL)
667 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
668 #endif
671 /* Add it to the list of FastCGI servers */
672 fcgi_util_fs_add(s);
674 return NULL;
677 /*******************************************************************************
678 * Configure a static FastCGI server that is started/managed elsewhere.
680 const char *fcgi_config_new_external_server(cmd_parms *cmd, void *dummy, const char *arg)
682 fcgi_server *s;
683 pool * const p = cmd->pool, *tp = cmd->temp_pool;
684 const char * const name = cmd->cmd->name;
685 char *fs_path = ap_getword_conf(p, &arg);
686 const char *option, *err;
688 if (!*fs_path) {
689 return ap_pstrcat(tp, name, " requires a path and either a -socket or -host option", NULL);
692 fs_path = ap_os_canonical_filename(p, fs_path);
693 fs_path = ap_server_root_relative(p, fs_path);
695 ap_getparents(fs_path);
696 ap_no2slash(fs_path);
698 /* See if we've already got one of these bettys configured */
699 s = fcgi_util_fs_get_by_id(fs_path, cmd->server->server_uid,
700 cmd->server->server_gid);
701 if (s != NULL) {
702 if (fcgi_wrapper) {
703 return ap_psprintf(tp,
704 "%s: redefinition of a previously defined class \"%s\" with uid=%ld and gid=%ld",
705 name, fs_path, (long)cmd->server->server_uid,
706 (long)cmd->server->server_gid);
708 else {
709 return ap_psprintf(tp,
710 "%s: redefinition of previously defined class \"%s\"", name, fs_path);
714 s = fcgi_util_fs_new(p);
715 s->fs_path = fs_path;
716 s->directive = APP_CLASS_EXTERNAL;
718 err = fcgi_util_fs_set_uid_n_gid(p, s, cmd->server->server_uid, cmd->server->server_gid);
719 if (err != NULL)
720 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
722 /* Parse directive arguments */
723 while (*arg != '\0') {
724 option = ap_getword_conf(tp, &arg);
726 if (strcasecmp(option, "-host") == 0) {
727 if ((err = get_host_n_port(p, &arg, &s->host, &s->port)))
728 return invalid_value(tp, name, fs_path, option, err);
730 else if (strcasecmp(option, "-socket") == 0) {
731 s->socket_path = ap_getword_conf(tp, &arg);
732 if (*s->socket_path == '\0')
733 return invalid_value(tp, name, fs_path, option, "\"\"");
735 else if (strcasecmp(option, "-appConnTimeout") == 0) {
736 if ((err = get_u_int(tp, &arg, &s->appConnectTimeout, 0)))
737 return invalid_value(tp, name, fs_path, option, err);
739 else if (strcasecmp(option, "-idle-timeout") == 0) {
740 if ((err = get_u_int(tp, &arg, &s->idle_timeout, 1)))
741 return invalid_value(tp, name, fs_path, option, err);
743 else if (strcasecmp(option, "-pass-header") == 0) {
744 if ((err = get_pass_header(p, &arg, &s->pass_headers)))
745 return invalid_value(tp, name, fs_path, option, err);
747 else if (strcasecmp(option, "-flush") == 0) {
748 s->flush = 1;
750 else {
751 return ap_psprintf(tp, "%s %s: invalid option: %s", name, fs_path, option);
753 } /* while */
755 /* Require one of -socket or -host, but not both */
756 if (s->socket_path != NULL && s->port != 0) {
757 return ap_psprintf(tp,
758 "%s %s: -host and -socket are mutually exclusive options",
759 name, fs_path);
761 if (s->socket_path == NULL && s->port == 0) {
762 return ap_psprintf(tp,
763 "%s %s: -socket or -host option missing", name, fs_path);
766 /* Build the appropriate sockaddr structure */
767 if (s->port != 0) {
768 err = fcgi_util_socket_make_inet_addr(p, (struct sockaddr_in **)&s->socket_addr,
769 &s->socket_addr_len, s->host, s->port);
770 if (err != NULL)
771 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
772 } else {
773 s->socket_path = fcgi_util_socket_make_path_absolute(p, s->socket_path, 0);
774 #ifndef WIN32
775 err = fcgi_util_socket_make_domain_addr(p, (struct sockaddr_un **)&s->socket_addr,
776 &s->socket_addr_len, s->socket_path);
777 if (err != NULL)
778 return ap_psprintf(tp, "%s %s: %s", name, fs_path, err);
779 #endif
782 /* Add it to the list of FastCGI servers */
783 fcgi_util_fs_add(s);
785 return NULL;
789 *----------------------------------------------------------------------
791 * fcgi_config_set_config --
793 * Implements the FastCGI FCGIConfig configuration directive.
794 * This command adds routines to control the execution of the
795 * dynamic FastCGI processes.
798 *----------------------------------------------------------------------
800 const char *fcgi_config_set_config(cmd_parms *cmd, void *dummy, const char *arg)
802 pool * const p = cmd->pool;
803 pool * const tp = cmd->temp_pool;
804 const char *err, *option;
805 const char * const name = cmd->cmd->name;
807 /* Allocate temp storage for an initial environment */
808 unsigned int envc = 0;
809 char **envp = (char **)ap_pcalloc(tp, sizeof(char *) * (MAX_INIT_ENV_VARS + 3));
811 /* Parse the directive arguments */
812 while (*arg) {
813 option = ap_getword_conf(tp, &arg);
815 if (strcasecmp(option, "-maxProcesses") == 0) {
816 if ((err = get_u_int(tp, &arg, &dynamicMaxProcs, 1)))
817 return invalid_value(tp, name, NULL, option, err);
819 else if (strcasecmp(option, "-minProcesses") == 0) {
820 if ((err = get_int(tp, &arg, &dynamicMinProcs, 0)))
821 return invalid_value(tp, name, NULL, option, err);
823 else if (strcasecmp(option, "-maxClassProcesses") == 0) {
824 if ((err = get_int(tp, &arg, &dynamicMaxClassProcs, 1)))
825 return invalid_value(tp, name, NULL, option, err);
827 else if (strcasecmp(option, "-killInterval") == 0) {
828 if ((err = get_u_int(tp, &arg, &dynamicKillInterval, 1)))
829 return invalid_value(tp, name, NULL, option, err);
831 else if (strcasecmp(option, "-updateInterval") == 0) {
832 if ((err = get_u_int(tp, &arg, &dynamicUpdateInterval, 1)))
833 return invalid_value(tp, name, NULL, option, err);
835 else if (strcasecmp(option, "-gainValue") == 0) {
836 if ((err = get_float(tp, &arg, &dynamicGain, 0.0, 1.0)))
837 return invalid_value(tp, name, NULL, option, err);
839 else if ((strcasecmp(option, "-singleThreshold") == 0)
840 || (strcasecmp(option, "-singleThreshhold") == 0))
842 if ((err = get_int(tp, &arg, &dynamicThreshold1, 0)))
843 return invalid_value(tp, name, NULL, option, err);
845 else if ((strcasecmp(option, "-multiThreshold") == 0)
846 || (strcasecmp(option, "-multiThreshhold") == 0))
848 if ((err = get_int(tp, &arg, &dynamicThresholdN, 0)))
849 return invalid_value(tp, name, NULL, option, err);
851 else if (strcasecmp(option, "-startDelay") == 0) {
852 if ((err = get_u_int(tp, &arg, &dynamicPleaseStartDelay, 1)))
853 return invalid_value(tp, name, NULL, option, err);
855 else if (strcasecmp(option, "-initial-env") == 0) {
856 if ((err = get_env_var(p, &arg, envp, &envc)))
857 return invalid_value(tp, name, NULL, option, err);
859 else if (strcasecmp(option, "-pass-header") == 0) {
860 if ((err = get_pass_header(p, &arg, &dynamic_pass_headers)))
861 return invalid_value(tp, name, NULL, option, err);
863 else if (strcasecmp(option, "-appConnTimeout") == 0) {
864 if ((err = get_u_int(tp, &arg, &dynamicAppConnectTimeout, 0)))
865 return invalid_value(tp, name, NULL, option, err);
867 else if (strcasecmp(option, "-idle-timeout") == 0) {
868 if ((err = get_u_int(tp, &arg, &dynamic_idle_timeout, 1)))
869 return invalid_value(tp, name, NULL, option, err);
871 else if (strcasecmp(option, "-listen-queue-depth") == 0) {
872 if ((err = get_u_int(tp, &arg, &dynamicListenQueueDepth, 1)))
873 return invalid_value(tp, name, NULL, option, err);
875 else if (strcasecmp(option, "-restart-delay") == 0) {
876 if ((err = get_u_int(tp, &arg, &dynamicRestartDelay, 0)))
877 return invalid_value(tp, name, NULL, option, err);
879 else if (strcasecmp(option, "-init-start-delay") == 0) {
880 if ((err = get_u_int(tp, &arg, &dynamicInitStartDelay, 0)))
881 return invalid_value(tp, name, NULL, option, err);
883 else if (strcasecmp(option, "-processSlack") == 0) {
884 if ((err = get_u_int(tp, &arg, &dynamicProcessSlack, 1)))
885 return invalid_value(tp, name, NULL, option, err);
887 else if (strcasecmp(option, "-restart") == 0) {
888 dynamicAutoRestart = 1;
890 else if (strcasecmp(option, "-autoUpdate") == 0) {
891 dynamicAutoUpdate = 1;
893 else {
894 return ap_psprintf(tp, "%s: invalid option: %s", name, option);
896 } /* while */
898 /* Move env array to a surviving pool, leave an extra slot for WIN32 _FCGI_MUTEX_ */
899 ++envc;
900 dynamicEnvp = (char **)ap_palloc(p, sizeof(char *) * ++envc);
901 memcpy(dynamicEnvp, envp, sizeof(char *) * envc);
903 return NULL;
906 void *fcgi_config_create_dir_config(pool *p, char *dummy)
908 fcgi_dir_config *dir_config = ap_pcalloc(p, sizeof(fcgi_dir_config));
910 dir_config->authenticator_options = FCGI_AUTHORITATIVE;
911 dir_config->authorizer_options = FCGI_AUTHORITATIVE;
912 dir_config->access_checker_options = FCGI_AUTHORITATIVE;
914 return dir_config;
918 const char *fcgi_config_new_auth_server(cmd_parms * const cmd,
919 fcgi_dir_config *dir_config, const char *fs_path, const char * const compat)
921 pool * const tp = cmd->temp_pool;
922 const uid_t uid = cmd->server->server_uid;
923 const gid_t gid = cmd->server->server_gid;
924 char * auth_server;
926 auth_server = (char *) ap_os_canonical_filename(cmd->pool, fs_path);
927 auth_server = ap_server_root_relative(cmd->pool, auth_server);
929 /* Make sure its already configured or at least a candidate for dynamic */
930 if (fcgi_util_fs_get_by_id(auth_server, uid, gid) == NULL) {
931 const char *err = fcgi_util_fs_is_path_ok(tp, auth_server, NULL);
932 if (err)
933 return ap_psprintf(tp, "%s: \"%s\" %s", cmd->cmd->name, auth_server, err);
936 if (compat && strcasecmp(compat, "-compat"))
937 return ap_psprintf(cmd->temp_pool, "%s: unknown option: \"%s\"", cmd->cmd->name, compat);
939 switch((int)cmd->info) {
940 case FCGI_AUTH_TYPE_AUTHENTICATOR:
941 dir_config->authenticator = auth_server;
942 dir_config->authenticator_options |= (compat) ? FCGI_COMPAT : 0;
943 break;
944 case FCGI_AUTH_TYPE_AUTHORIZER:
945 dir_config->authorizer = auth_server;
946 dir_config->authorizer_options |= (compat) ? FCGI_COMPAT : 0;
947 break;
948 case FCGI_AUTH_TYPE_ACCESS_CHECKER:
949 dir_config->access_checker = auth_server;
950 dir_config->access_checker_options |= (compat) ? FCGI_COMPAT : 0;
951 break;
954 return NULL;
957 const char *fcgi_config_set_authoritative_slot(const cmd_parms * const cmd,
958 fcgi_dir_config * const dir_config, int arg)
960 int offset = (int)(long)cmd->info;
962 if (arg)
963 *(int *)(dir_config + offset) |= FCGI_AUTHORITATIVE;
964 else
965 *(int *)(dir_config + offset) &= ~FCGI_AUTHORITATIVE;
967 return NULL;