1 /* Copyright 2006 The Apache Software Foundation or its licensors, as
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include <apr_pools.h>
19 #include <apr_network_io.h>
20 #include <apr_thread_proc.h>
21 #include <apr_getopt.h>
22 #include <apr_portable.h>
25 #include <stdlib.h> /* For EXIT_SUCCESS, EXIT_FAILURE */
29 #include <unistd.h> /* For execl */
32 static const char *usage_message =
33 "usage: fcgistarter -c <command> -p <port> [-i <interface> -N <num>]\n"
35 "If an interface is not specified, any available will be used.\n";
39 fprintf(stderr, "%s", usage_message);
44 static void exit_error(apr_status_t rv, const char *func)
51 apr_strerror(rv, buffer, sizeof(buffer)));
56 int main(int argc, const char *argv[])
58 apr_file_t *infd, *skwrapper;
59 apr_sockaddr_t *skaddr;
60 apr_procattr_t *pattr;
68 /* Command line arguments */
69 int num_to_start = 1, port = 0;
70 const char *interface = NULL;
71 const char *command = NULL;
75 atexit(apr_terminate);
77 apr_pool_create(&pool, NULL);
79 rv = apr_getopt_init(&gopt, pool, argc, argv);
88 rv = apr_getopt(gopt, "c:p:i:N:", &opt, &arg);
89 if (APR_STATUS_IS_EOF(rv)) {
111 num_to_start = atoi(arg);
112 if (! num_to_start) {
123 if (! command || ! port) {
127 rv = apr_socket_create(&skt, APR_INET, SOCK_STREAM, APR_PROTO_TCP, pool);
129 exit_error(rv, "apr_socket_create");
132 rv = apr_sockaddr_info_get(&skaddr, interface, APR_UNSPEC, port, 0, pool);
134 exit_error(rv, "apr_sockaddr_info_get");
137 rv = apr_socket_bind(skt, skaddr);
139 exit_error(rv, "apr_socket_bind");
142 rv = apr_socket_listen(skt, 1024);
144 exit_error(rv, "apr_socket_listen");
147 while (--num_to_start >= 0) {
148 rv = apr_proc_fork(&proc, pool);
149 if (rv == APR_INCHILD) {
150 apr_os_file_t oft = 0;
153 rv = apr_proc_detach(APR_PROC_DETACH_DAEMONIZE);
155 exit_error(rv, "apr_proc_detach");
158 #if defined(WIN32) || defined(OS2) || defined(NETWARE)
159 #error "Please implement me."
162 /* Ok, so we need a file that has file descriptor 0 (which
163 * FastCGI wants), but points to our socket. This isn't really
164 * possible in APR, so we cheat a bit. I have no idea how to
165 * do this on a non-unix platform, so for now this is platform
168 * Note that this has to happen post-detach, otherwise fd 0
169 * gets closed during apr_proc_detach and it's all for nothing.
171 * Unfortunately, doing this post detach means we have no way
172 * to let anyone know if there's a problem at this point :( */
174 rv = apr_os_file_put(&infd, &oft, APR_READ | APR_WRITE, pool);
179 rv = apr_os_sock_get(&oskt, skt);
184 rv = apr_os_file_put(&skwrapper, &oskt, APR_READ | APR_WRITE,
190 rv = apr_file_dup2(infd, skwrapper, pool);
195 /* XXX Can't use apr_proc_create because there's no way to get
196 * infd into the procattr without going through another dup2,
197 * which means by the time it gets to the fastcgi process it
198 * is no longer fd 0, so it doesn't work. Sigh. */
200 execl(command, NULL);
202 } else if (rv == APR_INPARENT) {
203 if (num_to_start == 0) {
204 apr_socket_close(skt);
207 exit_error(rv, "apr_proc_fork");