123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198 |
- #include "conf.h"
-
- void usage()
- {
- static const struct option opts[] = PYFCGI_LONG_OPT;
- static const char *help[][2] = PYFCGI_OPT_HELP;
- size_t i;
- dprintf(2, "Usage : %s -e PYMODULE [-E PYFUN] [OPTIONS]\n", PYFCGI_NAME);
- dprintf(2, "\nOptions list :\n");
- i=0;
- while(opts[i].name)
- {
- dprintf(2, "\t-%c, --%s", opts[i].val, opts[i].name);
- switch(opts[i].has_arg)
- {
- case required_argument:
- dprintf(2, "=%s\n",
- help[i][1]?help[i][1]:"ARG");
- break;
- case optional_argument:
- dprintf(2, "[=%s]\n",
- help[i][1]?help[i][1]:"ARG");
- break;
- default: //no_argument
- dprintf(2, "\n");
- }
- printf("\t\t%s\n\n", help[i][0]);
- i++;
- }
- dprintf(2, "%s", PYFCGI_HELP_TEXT);
- }
-
- void print_version(int fd)
- {
- char version[16];
- pyfcgi_python_version(version);
- dprintf(fd, "%s\nPython %s\n", PACKAGE_STRING, version);
- }
-
- void default_conf()
- {
- memset(&PyFCGI_conf, 0, sizeof(pyfcgi_conf_t));
- PyFCGI_conf.context.pid = getpid();
- PyFCGI_conf.min_wrk = 1;
- PyFCGI_conf.max_wrk = 5;
- PyFCGI_conf.max_reqs = 1000;
- PyFCGI_conf.pep333 = 1;
- }
-
- int parse_args(int argc, char *argv[])
- {
- static const struct option long_options[] = PYFCGI_LONG_OPT;
- char ident[] = "pyfcgi[XXXXXXXX]";
- int c, opt_i;
-
- while(1)
- {
- c = getopt_long(argc, argv, PYFCGI_SHORT_OPT, long_options,
- &opt_i);
- if(c == -1) { break; }
- switch(c)
- {
- case 'v':
- print_version(1);
- exit(0);
- case 'C':
- dprintf(2, "Config parser not yet implemented :'(\n");
- exit(1);
- case 'e':
- PyFCGI_conf.py_entrymod = strdup(optarg);
- break;
- case 'E':
- PyFCGI_conf.py_entryfun = strdup(optarg);
- break;
- case 'A':
- PyFCGI_conf.pep333 = 0;
- break;
- case 'w':
- PyFCGI_conf.min_wrk = atoi(optarg);
- break;
- case 'W':
- PyFCGI_conf.max_wrk = atoi(optarg);
- break;
- case 'm':
- PyFCGI_conf.max_reqs = atoi(optarg);
- if(PyFCGI_conf.max_reqs < 0)
- {
- PyFCGI_conf.max_reqs = 0;
- }
- break;
- case 'L':
- if(parse_optlog(optarg))
- {
- exit(1);
- }
- break;
- case 'S':
- snprintf(ident+7, 8, "%4d]", PyFCGI_conf.context.pid);
- pyfcgi_logger_enable_syslog(ident);
- break;
- case 'P':
- PyFCGI_conf.pidfile = strdup(optarg);
- /**@todo create pidfile and put master pid in it */
- break;
- case 'h':
- usage();
- exit(0);
- default:
- usage();
- exit(1);
- }
- }
- if(optind < argc)
- {
- for(opt_i=optind; opt_i<argc; opt_i++)
- {
- dprintf(2, "Unkown argument '%s'\n", argv[opt_i]);
- }
- usage();
- exit(1);
- }
- if(!PyFCGI_conf.py_entrymod)
- {
- dprintf(2, "No python entry module given... exiting\n");
- usage();
- exit(2);
- }
- if(!PyFCGI_conf.py_entryfun)
- {
- PyFCGI_conf.py_entryfun = PYENTRY_DEFAULT_FUN;
- }
- if(check_entrypoint_import())
- {
- usage();
- exit(3);
- }
- return 0;
- }
-
- int check_entrypoint_import()
- {
- pid_t pid;
- int status;
- void *ret;
-
- pid = fork();
- if(!pid)
- {
- pyinit();
- ret = (void*)import_entrypoint();
- if(!ret)
- {
- dprintf(2, "Unable to import entrypoint...\n");
- exit(1);
- }
- pyfcgi_log(LOG_DEBUG, "Entrypoint import [OK]");
- Py_Exit(0);
- }
- waitpid(pid, &status, 0);
- return WEXITSTATUS(status);
- }
-
- int parse_optlog(const char* logspec)
- {
- char *filename, *filter, *fmt;
- int filt;
-
- filename = strdup(logspec); /**@todo check error */
- filter = filename;
- while(*filter && *filter != ';') { filter++; }
- if(*filter)
- {
- *filter = '\0';
- filter++;
- }
- fmt = filter;
- while(*fmt && *fmt != ';') { fmt++; }
- if(*fmt)
- {
- *fmt = '\0';
- fmt++;
- }
- if(!strlen(filter))
- {
- filt = 0xFF;
- }
- else
- {
- filt = strtol(filter, NULL, !strncmp(filter, "0x", 2)?16:10);
- }
- fmt = strlen(fmt)?fmt:NULL;
- if(pyfcgi_logger_add(filename, filt, filt, fmt))
- {
- return 1;
- }
- return 0;
- }
|