This repository has been archived by the owner on Sep 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
190 lines (155 loc) · 5.31 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/* A small FastCGI server that generates image thumbnails on the fly. Originally
* written for soup.io.
*
* Copyright © 2012 euphoria GmbH
* Author: Lukas Martini <lutoma@phoria.eu>
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <getopt.h>
#include <fcgiapp.h>
#include <sys/stat.h>
#include <syslog.h>
#include <pwd.h>
#include <grp.h>
#include <signal.h>
#include <wand/MagickWand.h>
#include "global.h"
#include "request.h"
int worker_id = -1;
pid_t* worker_pids = NULL;
int num_workers = -1;
void usage(char* argv[])
{
fprintf(stderr, "Usage: %s [root] [thumbnail_root] [listen_addr] [user] [group] [num_workers]\n", argv[0]);
syslog(LOG_ERR, "Invalid command line arguments\n");
exit(EXIT_FAILURE);
}
void exit_signal()
{
syslog(LOG_INFO, "Catched one of SIG{TERM,INT, HUP}, killing workers!\n");
for(int i = 0; i <= num_workers; i++)
kill(worker_pids[i], SIGTERM);
exit(EXIT_SUCCESS);
}
void worker_died()
{
syslog(LOG_CRIT, "One of our workers crashed. This is seriously BAD.\n");
}
int main(int argc, char* argv[], char* envp[])
{
if(argc < 7)
usage(argv);
char* root = argv[1];
char* thumbnail_root = argv[2];
char* listen_addr = argv[3];
num_workers = atoi(argv[6]);
if(num_workers < 1)
usage(argv);
struct passwd* pwd = getpwnam(argv[4]);
if (pwd == NULL)
error_errno("getpwnam_r failed", EXIT_FAILURE);
struct group* grp = getgrnam(argv[5]);
if (grp == NULL)
error_errno("getgrnam_r failed", EXIT_FAILURE);
int userid = pwd->pw_uid;
int groupid = grp->gr_gid;
if(root[strlen(root) - 1] != '/' || thumbnail_root[strlen(thumbnail_root) - 1] != '/')
error("Did you forget the ending slash in the (thumbnail) root directory path?", EXIT_FAILURE);
// TODO Some more error checking
// Initialize syslog
syslog(LOG_INFO, "Starting up\n");
#if defined(DEBUG)
setlogmask(LOG_UPTO(LOG_DEBUG));
openlog("fastresize", LOG_CONS | LOG_NDELAY | LOG_PERROR | LOG_PID, LOG_USER);
#else
setlogmask(LOG_UPTO(LOG_INFO));
openlog("fastresize", LOG_CONS, LOG_USER);
#endif
// Change the working directory
if((chdir(root)) < 0)
error("Couldn't change working directory\n", EXIT_FAILURE);
// Initialize FastCGI
syslog(LOG_INFO, "Initializing FastCGI\n");
if(FCGX_Init())
error("Could not initialize FastCGI (during FCGX_Init())", EXIT_FAILURE);
int listen_socket = FCGX_OpenSocket(listen_addr, 400);
if(listen_socket < 0)
error("Couldn't bind to FastCGI socket", EXIT_FAILURE);
// Now that we've got our socket, drop root privileges
if (getuid() == 0) {
if (setgid(groupid) != 0)
error_errno("setgid: Unable to drop group privileges", EXIT_FAILURE);
if (setuid(userid) != 0)
error_errno("setuid: Unable to drop user privileges", EXIT_FAILURE);
}
FCGX_Request request;
if(FCGX_InitRequest(&request, listen_socket, 0))
error("Couldn't initialize FastCGI request handler", EXIT_FAILURE);
// Initialize ImageMagick
syslog(LOG_INFO, "Initializing ImageMagick\n");
MagickWandGenesis();
atexit(MagickWandTerminus);
/* Fork a new master process to daemonize and exit the old one. We use
* _Exit here to not trigger the atexit that terminates ImageMagick.
*/
if(fork())
_Exit(EXIT_SUCCESS);
// Fork worker processes
syslog(LOG_INFO, "Forking workers\n");
worker_pids = calloc(num_workers, sizeof(pid_t));
if(!worker_pids)
error_errno("worker_pids: Could not allocate", 1);
for(worker_id = 0; worker_id <= num_workers; worker_id++)
{
worker_pids[worker_id] = fork();
// Exit the loop if we're the forked process
if(worker_pids[worker_id] == 0)
break;
syslog(LOG_INFO, "Forked worker with PID %d\n", worker_pids[worker_id]);
}
// The following code is only executed in the master process.
if(worker_id > num_workers)
{
syslog(LOG_INFO, "master (PID %d): Sleeping until I receive a signal.\n", getpid());
/* Sleep a little until we get a SIG{TERM,HUP,INT} or one of our
* workers died (not cool).
*/
struct sigaction exit_action;
exit_action.sa_handler = &exit_signal;
exit_action.sa_sigaction = &exit_signal;
struct sigaction worker_died_action;
worker_died_action.sa_handler = &worker_died;
worker_died_action.sa_sigaction = &worker_died;
sigaction(SIGTERM, &exit_action, NULL);
sigaction(SIGHUP, &exit_action, NULL);
sigaction(SIGINT, &exit_action, NULL);
sigaction(SIGCHLD, &worker_died_action, NULL);
pause();
// This should never™ be reached unless something funny happens.
exit(EXIT_FAILURE);
}
syslog(LOG_INFO, "Worker #%d is now listening for requests on 127.0.0.1:9000\n", worker_id);
while(FCGX_Accept_r(&request) == 0)
handle_request(&request, root, thumbnail_root);
// Exit
exit(EXIT_SUCCESS);
}