| 1 | /* |
|---|
| 2 | compile with: |
|---|
| 3 | |
|---|
| 4 | $ gcc -lfcgi -lpthread fcgi-stat-accel.c -o fcgi-stat-accel |
|---|
| 5 | |
|---|
| 6 | fcgi-stat-accel will use the PHP_FCGI_CHILDREN environment variable to set the thread count. |
|---|
| 7 | |
|---|
| 8 | The default value, if spawned from lighttpd, is 20. |
|---|
| 9 | */ |
|---|
| 10 | |
|---|
| 11 | #ifdef HAVE_CONFIG_H |
|---|
| 12 | #include "config.h" |
|---|
| 13 | #endif |
|---|
| 14 | |
|---|
| 15 | #include <sys/types.h> |
|---|
| 16 | #include <sys/stat.h> |
|---|
| 17 | |
|---|
| 18 | #ifdef HAVE_PTHREAD_H |
|---|
| 19 | #include <pthread.h> |
|---|
| 20 | #endif |
|---|
| 21 | #ifdef HAVE_UNISTD_H |
|---|
| 22 | #include <unistd.h> |
|---|
| 23 | #endif |
|---|
| 24 | #include <string.h> |
|---|
| 25 | #include <fcntl.h> |
|---|
| 26 | |
|---|
| 27 | #include <stdlib.h> |
|---|
| 28 | #ifdef HAVE_FASTCGI_FASTCGI_H |
|---|
| 29 | #include <fastcgi/fcgiapp.h> |
|---|
| 30 | #elif defined (HAVE_FASTCGI_H) |
|---|
| 31 | #include <fcgiapp.h> |
|---|
| 32 | #endif |
|---|
| 33 | |
|---|
| 34 | #define THREAD_COUNT 20 |
|---|
| 35 | |
|---|
| 36 | #define FORBIDDEN(stream) \ |
|---|
| 37 | FCGX_FPrintF(stream, "Status: 403 Forbidden\r\nContent-Type: text/html\r\n\r\n<h1>403 Forbidden</h1>\n"); |
|---|
| 38 | #define NOTFOUND(stream, filename) \ |
|---|
| 39 | FCGX_FPrintF(stream, "Status: 404 Not Found\r\nContent-Type: text/html\r\n\r\n<h1>404 Not Found</h1>\r\n%s", filename); |
|---|
| 40 | #define SENDFILE(stream, filename) \ |
|---|
| 41 | FCGX_FPrintF(stream, "X-LIGHTTPD-send-file: %s\r\n\r\n", filename); |
|---|
| 42 | |
|---|
| 43 | #define UNUSED(x) ( (void)(x) ) |
|---|
| 44 | |
|---|
| 45 | static void *doit(void *a){ |
|---|
| 46 | FCGX_Request request; |
|---|
| 47 | int rc; |
|---|
| 48 | char *filename; |
|---|
| 49 | UNUSED(a); |
|---|
| 50 | |
|---|
| 51 | FCGX_InitRequest(&request, 0, /* FCGI_FAIL_ACCEPT_ON_INTR */ 0); |
|---|
| 52 | |
|---|
| 53 | while(1){ |
|---|
| 54 | int fd; |
|---|
| 55 | //Some platforms require accept() serialization, some don't. The documentation claims it to be thread safe |
|---|
| 56 | // static pthread_mutex_t accept_mutex = PTHREAD_MUTEX_INITIALIZER; |
|---|
| 57 | // pthread_mutex_lock(&accept_mutex); |
|---|
| 58 | rc = FCGX_Accept_r(&request); |
|---|
| 59 | // pthread_mutex_unlock(&accept_mutex); |
|---|
| 60 | |
|---|
| 61 | if(rc < 0) |
|---|
| 62 | break; |
|---|
| 63 | |
|---|
| 64 | //get the filename |
|---|
| 65 | if((filename = FCGX_GetParam("SCRIPT_FILENAME", request.envp)) == NULL){ |
|---|
| 66 | FORBIDDEN(request.out); |
|---|
| 67 | //don't try to open directories |
|---|
| 68 | }else if(filename[strlen(filename)-1] == '/'){ |
|---|
| 69 | FORBIDDEN(request.out); |
|---|
| 70 | //open the file |
|---|
| 71 | }else if((fd = open(filename, O_RDONLY)) == -1){ |
|---|
| 72 | NOTFOUND(request.out, filename); |
|---|
| 73 | //no error, serve it |
|---|
| 74 | }else{ |
|---|
| 75 | SENDFILE(request.out, filename); |
|---|
| 76 | |
|---|
| 77 | close(fd); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | FCGX_Finish_r(&request); |
|---|
| 81 | } |
|---|
| 82 | return NULL; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | int main(void){ |
|---|
| 86 | int i,j,thread_count; |
|---|
| 87 | pthread_t* id; |
|---|
| 88 | char* env_val; |
|---|
| 89 | |
|---|
| 90 | FCGX_Init(); |
|---|
| 91 | |
|---|
| 92 | thread_count = THREAD_COUNT; |
|---|
| 93 | env_val = getenv("PHP_FCGI_CHILDREN"); |
|---|
| 94 | if (env_val != NULL) { |
|---|
| 95 | j = atoi(env_val); |
|---|
| 96 | if (j != 0) { |
|---|
| 97 | thread_count = j; |
|---|
| 98 | }; |
|---|
| 99 | }; |
|---|
| 100 | |
|---|
| 101 | id = malloc(sizeof(*id) * thread_count); |
|---|
| 102 | |
|---|
| 103 | for (i = 0; i < thread_count; i++) { |
|---|
| 104 | pthread_create(&id[i], NULL, doit, NULL); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /* block the current thread by executing one */ |
|---|
| 108 | doit(NULL); |
|---|
| 109 | |
|---|
| 110 | for (i = 0; i < thread_count; i++) { |
|---|
| 111 | pthread_join(id[i], NULL); |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | free(id); |
|---|
| 115 | |
|---|
| 116 | return 0; |
|---|
| 117 | } |
|---|