-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
323 lines (284 loc) · 6.48 KB
/
helpers.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
#include "helpers.h"
char *mb_reverse(const char *str)
{
if (g_utf8_validate(str, g_utf8_strlen(str, -1), NULL) != TRUE)
{
return NULL;
}
char *temp = g_utf8_strreverse(str, g_utf8_strlen(str, -1));
return temp;
}
char *str_toup(const char *str)
{
char *temp = g_utf8_strup(str, strlen(str));
return temp;
}
char *str_tolow(const char *str)
{
char *temp = g_utf8_strdown(str, strlen(str));
return temp;
}
void createpidf(const char *pid)
{
errno = 0;
FILE *pidfile = fopen(pid, "w");
if (pidfile < 0 || errno)
{
log_msg(0, "Opening pidfile error");
log_msg(0, strerror(errno));
exit(1);
}
int p_pid = getpid();
fprintf(pidfile, "%d", p_pid);
fflush(pidfile);
fclose(pidfile);
}
void daemon_mode()
{
errno = 0;
int master_pid = getpid();
pid_t new_pid = fork();
if (new_pid < 0)
{
log_msg(0, "daemon fork error");
if (errno) {
log_msg(0, strerror(errno));
}
exit(1);
}
if (getpid() == master_pid)
{
exit(0);
}
umask(FORK_UMASK);
// get rid of master terminal
pid_t new_sid = setsid();
if (new_sid < 0)
{
log_msg(0, "setsid error");
if (errno) log_msg(0, strerror(errno));
exit(2);
}
if (chdir(CHDIR_DIR) < 0)
{
log_msg(0, "chdir error");
if (errno) log_msg(0, strerror(errno));
exit(3);
}
log_msg(0, "postcdir");
//close master process descriptors
close(STDIN_FILENO);
close(STDOUT_FILENO);
close(STDERR_FILENO);
}
void set_globs(s_opt_t *opt)
{
errno = 0;
mode_t l_sockmask;
char *l_locale;
char *l_pid;
char *sockpath;
if (opt && opt->sock)
{
g_sock = FCGX_OpenSocket(opt->sock, FCGX_BACKLOG_SIZE);
sockpath = opt->sock;
}
else
{
g_sock = FCGX_OpenSocket(SOCK_PATH, FCGX_BACKLOG_SIZE);
sockpath = SOCK_PATH;
}
if (g_sock < 0)
{
log_msg(0, "FCGX_OpenSocket error");
if (errno)
log_msg(0, strerror(errno));
exit(1);
}
else
{
// fix for FCGX_OpenSocket No such file or directory
errno = 0;
}
if (opt && opt->sockmask)
{
enum
{
// all permission bits, equal to 07777
PERMBITS = S_IRWXU | S_IRWXG | S_IRWXO | S_ISUID | S_ISGID | S_ISVTX,
};
char *endptr;
mode_t mode;
unsigned long newmode;
newmode = strtoul(opt->sockmask, &endptr, 8);
if(errno)
{
log_msg(0, "sockmask error");
log_msg(0, strerror(errno));
exit(2);
}
mode &= ~PERMBITS;
mode |= newmode;
l_sockmask = mode;
}
else
{
l_sockmask = SOCK_MASK;
}
if (chmod(sockpath, l_sockmask) != 0 || errno)
{
log_msg(0, "Socket chmod error");
log_msg(0, strerror(errno));
exit(3);
}
if (opt && opt->logf)
{
g_log_fd = fopen(opt->logf, "a+");
}
else
{
g_log_fd = fopen(LOGFILE, "a+");
}
if (!g_log_fd || errno)
{
log_msg(0, "error opening log file");
log_msg(0, strerror(errno));
exit(4);
}
if (opt && opt->locale)
{
l_locale = opt->locale;
}
else
{
l_locale = DEFLOCALE;
}
if (!l_locale)
{
log_msg(0, "error setting locale");
if (errno)
log_msg(0, strerror(errno));
exit(5);
}
if (strcmp(setlocale(LC_CTYPE, l_locale), l_locale) != 0 )
{
log_msg(0, "Locale setup error");
exit(6);
}
if (opt && opt->d_mode) {
daemon_mode();
}
if (opt && opt->thr_c)
{
g_thr_c = opt->thr_c;
}
else
{
g_thr_c = THREAD_COUNT;
}
if (!g_thr_c)
{
log_msg(0, "error setting threads count");
exit(7);
}
if (opt && opt->pid)
{
l_pid = opt->pid;
}
else
{
l_pid = PIDFILE;
}
if (!l_pid)
{
log_msg(0, "error opening pid file");
exit(8);
}
createpidf(l_pid);
}
void log_msg(int thr_id, const char *msg)
{
pthread_mutex_lock(&g_log_mutex);
time_t t = time(NULL);
struct tm *tm = localtime(&t);
char s[100];
strftime(s, sizeof(s), "%Y/%m/%d %H:%M:%S", tm);
if (g_log_fd) {
fprintf(g_log_fd, "[%s] thread %d:%s\n", s, thr_id, msg);
fflush(g_log_fd);
} else {
printf("[%s] thread %d:%s\n", s, thr_id, msg);
}
pthread_mutex_unlock(&g_log_mutex);
}
s_opt_t *parse_args(int argc, char *argv[])
{
errno = 0;
s_opt_t *opt = calloc(1, sizeof(s_opt_t));
if (!opt)
{
log_msg(0, "parse_main calloc error");
exit(1);
}
int i, o = 0;
while ( (i = getopt(argc, argv, "s:p:c:w:m:l:d")) != -1 )
{
switch(i)
{
case 's':
opt->sock = strdup(optarg);
if(!(*opt).sock)
{
log_msg(0, "parse_main sock malloc error");
exit(1);
}
break;
case 'p':
opt->pid = strdup(optarg);
if(!(*opt).pid)
{
log_msg(0, "parse_main pid malloc error");
exit(1);
}
break;
case 'c':
opt->locale = strdup(optarg);
if(!(*opt).locale)
{
log_msg(0, "parse_main pid malloc error");
exit(1);
}
break;
case 'w':
opt->thr_c = atoi(optarg);
break;
case 'm':
opt->sockmask = strdup(optarg);
break;
case 'd':
opt->d_mode = 1;
break;
case 'l':
opt->logf = strdup(optarg);
if(!(*opt).logf)
{
log_msg(0, "parse_main logfile_name malloc error");
exit(1);
}
break;
default:
fprintf(stderr, "%s\n", "To change default settings: -s (listen socket) -w (workers) -d (daemon mode) -l (logfile) -p (pid file) -m (socket mask 0000666) -c (locale)");
exit(1);
}
o++;
}
if (o)
{
return opt;
}
else
{
free(opt);
return NULL;
}
}