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
142 lines (117 loc) · 3.91 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
/* 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 <wand/MagickWand.h>
#include "global.h"
#include "request.h"
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);
}
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];
int 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", 1);
if (setuid(userid) != 0)
error_errno("setuid: Unable to drop user privileges", 1);
}
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();
// Fork worker processes and exit main process (to daemonize)
bool worker = false;
syslog(LOG_INFO, "Forking workers\n");
for(int i = 1; i <= num_workers; i++)
if((worker = fork() == 0)) break;
if(!worker)
{
syslog(LOG_INFO, "Now listening for requests on 127.0.0.1:9000\n");
printf("Successfull startup, see syslog for details\n");
// Change the filemode mask
umask(0);
// Close stdin, -out, -err
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
exit(EXIT_SUCCESS);
}
while(FCGX_Accept_r(&request) == 0)
handle_request(&request, root, thumbnail_root);
// Cleanup & exit
MagickWandTerminus();
exit(EXIT_SUCCESS);
}