Dillo v3.1.1-98-g318d1f14
Loading...
Searching...
No Matches
main.c
Go to the documentation of this file.
1/*
2 Copyright (C) 2003 Ferdi Franceschini <ferdif@optusnet.com.au>
3 2020 Axel Beckert <abe@debian.org>
4 2023 Michal Grezl <walley@walley.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include <errno.h> /* for ckd_write */
21#include <unistd.h> /* for ckd_write */
22#include <stdlib.h> /* for exit */
23#include <assert.h> /* for assert */
24#include <sys/stat.h> /* for umask */
25
26#include "dpid_common.h"
27#include "dpid.h"
28#include "dpi.h"
29#include "dpi_socket_dir.h"
30#include "misc_new.h"
31
32#include "../dlib/dlib.h"
33#include "../dpip/dpip.h"
34
35sigset_t mask_sigchld;
36
37/* fix for gcc 10 */
38
39enum {
41 dpid_srs_addrinuse /* dpid service request socket address already in use */
43
46fd_set sock_set;
51
52// end of fix
53
54
60static int start_filter_plugin(struct dp dpi_attr)
61{
62 int newsock, old_stdout=-1, old_stdin=-1;
63 socklen_t csz;
64 struct sockaddr_un clnt_addr;
65 pid_t pid;
66
67 csz = (socklen_t) sizeof(clnt_addr);
68
69 newsock = accept(dpi_attr.sock_fd, (struct sockaddr *) &clnt_addr, &csz);
70 if (newsock == -1)
71 ERRMSG("start_plugin", "accept", errno);
72
73 dup2(STDIN_FILENO, old_stdin);
74 if (dup2(newsock, STDIN_FILENO) == -1) {
75 ERRMSG("start_plugin", "dup2", errno);
76 MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
77 exit(1);
78 }
79
80 dup2(STDOUT_FILENO, old_stdout);
81 if (dup2(newsock, STDOUT_FILENO) == -1) {
82 ERRMSG("start_plugin", "dup2", errno);
83 MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
84 exit(1);
85 }
86 if ((pid = fork()) == -1) {
87 ERRMSG("main", "fork", errno);
88 return 0;
89 }
90 if (pid == 0) {
91 /* Child, start plugin */
92 if (execl(dpi_attr.path, dpi_attr.path, (char*)NULL) == -1) {
93 ERRMSG("start_plugin", "execl", errno);
94 MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
95 exit(1);
96 }
97 }
98
99 /* Parent, Close sockets fix stdio and return pid */
100 if (dClose(newsock) == -1) {
101 ERRMSG("start_plugin", "close", errno);
102 MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
103 exit(1);
104 }
105 dClose(STDIN_FILENO);
106 dClose(STDOUT_FILENO);
107 dup2(old_stdin, STDIN_FILENO);
108 dup2(old_stdout, STDOUT_FILENO);
109 return pid;
110}
111
112static void start_server_plugin(struct dp dpi_attr)
113{
114 if (dup2(dpi_attr.sock_fd, STDIN_FILENO) == -1) {
115 ERRMSG("start_plugin", "dup2", errno);
116 MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
117 exit(1);
118 }
119 if (dClose(dpi_attr.sock_fd) == -1) {
120 ERRMSG("start_plugin", "close", errno);
121 MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
122 exit(1);
123 }
124 if (execl(dpi_attr.path, dpi_attr.path, (char*)NULL) == -1) {
125 ERRMSG("start_plugin", "execl", errno);
126 MSG_ERR("ERROR in child proc for %s\n", dpi_attr.path);
127 exit(1);
128 }
129}
130
136static char *get_request(Dsh *sh)
137{
138 char *dpip_tag;
139
140 (void) sigprocmask(SIG_BLOCK, &mask_sigchld, NULL);
141 dpip_tag = a_Dpip_dsh_read_token(sh, 1);
142 (void) sigprocmask(SIG_UNBLOCK, &mask_sigchld, NULL);
143
144 return dpip_tag;
145}
146
152static int get_command(Dsh *sh, char *dpi_tag)
153{
154 char *cmd, *d_cmd;
155 int COMMAND;
156
157 if (dpi_tag == NULL) {
158 _ERRMSG("get_command", "dpid tag is NULL", 0);
159 return (-1);
160 }
161
162 cmd = a_Dpip_get_attr(dpi_tag, "cmd");
163
164 if (cmd == NULL) {
165 ERRMSG("get_command", "a_Dpip_get_attr", 0);
166 MSG_ERR(": dpid failed to parse cmd in %s\n", dpi_tag);
167 d_cmd = a_Dpip_build_cmd("cmd=%s msg=%s",
168 "DpiError", "Failed to parse request");
169 a_Dpip_dsh_write_str(sh, 1, d_cmd);
170 dFree(d_cmd);
171 COMMAND = -1;
172 } else if (strcmp("auth", cmd) == 0) {
173 COMMAND = AUTH_CMD;
174 } else if (strcmp("DpiBye", cmd) == 0) {
175 COMMAND = BYE_CMD;
176 } else if (strcmp("check_server", cmd) == 0) {
177 COMMAND = CHECK_SERVER_CMD;
178 } else if (strcmp("register_all", cmd) == 0) {
179 COMMAND = REGISTER_ALL_CMD;
180 } else if (strcmp("register_service", cmd) == 0) {
181 COMMAND = REGISTER_SERVICE_CMD;
182 } else { /* Error unknown command */
183 COMMAND = UNKNOWN_CMD;
184 }
185
186 dFree(cmd);
187 return (COMMAND);
188}
189
193static int server_is_running(char *server_id)
194{
195 int i;
196
197 /* Search in the set of running servers */
198 for (i = 0; i < numdpis; i++) {
199 if (!dpi_attr_list[i].filter && dpi_attr_list[i].pid > 1 &&
200 strcmp(dpi_attr_list[i].id, server_id) == 0)
201 return 1;
202 }
203 return 0;
204}
205
206
210static int get_open_max(void)
211{
212#ifdef OPEN_MAX
213 return OPEN_MAX;
214#else
215 int ret = sysconf(_SC_OPEN_MAX);
216 if (ret < 0)
217 ret = 256;
218 return ret;
219#endif
220}
221
226int main(void)
227{
228 int i, n = 0, open_max;
229 int dpid_idle_timeout = 60 * 60; /* default, in seconds */
230 struct timeval select_timeout;
231 sigset_t mask_none;
232 fd_set selected_set;
233
234 dpi_attr_list = NULL;
235 services_list = NULL;
236 //daemon(0,0); /* Use 0,1 for feedback */
237 /* TODO: call setsid() ?? */
238
239 /* Allow read and write access, but only for the user.
240 * TODO: can this cause trouble with umount? */
241 umask(0077);
242 /* TODO: make dpid work on any directory. */
243 // chdir("/");
244
245 /* close inherited file descriptors */
246 open_max = get_open_max();
247 for (i = 3; i < open_max; i++)
248 dClose(i);
249
250 /* this sleep used to unmask a race condition */
251 // sleep(2);
252
254
255 /* Get list of available dpis */
257
258#if 0
259 /* Get name of socket directory */
260 dirname = a_Dpi_sockdir_file();
261 if ((sockdir = init_sockdir(dirname)) == NULL) {
262 ERRMSG("main", "init_sockdir", 0);
263 MSG_ERR("Failed to create socket directory\n");
264 exit(1);
265 }
266#endif
267
268 /* Init and get services list */
270
271 /* Remove any sockets that may have been leftover from a crash */
272 //cleanup();
273
274 /* Initialise sockets */
275 if ((numsocks = init_ids_srs_socket()) == -1) {
276 switch (dpi_errno) {
278 MSG_ERR("dpid refuses to start, possibly because:\n");
279 MSG_ERR("\t1) An instance of dpid is already running.\n");
280 MSG_ERR("\t2) A previous dpid didn't clean up on exit.\n");
281 exit(1);
282 default:
283 //ERRMSG("main", "init_srs_socket failed", 0);
284 ERRMSG("main", "init_ids_srs_socket failed", 0);
285 exit(1);
286 }
287 }
291
292 (void) sigemptyset(&mask_sigchld);
293 (void) sigaddset(&mask_sigchld, SIGCHLD);
294 (void) sigemptyset(&mask_none);
295 (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
296
297 printf("dpid started\n");
298/* Start main loop */
299 while (1) {
300 do {
301 (void) sigprocmask(SIG_BLOCK, &mask_sigchld, NULL);
302 if (caught_sigchld) {
304 caught_sigchld = 0;
305 }
306 (void) sigprocmask(SIG_UNBLOCK, &mask_sigchld, NULL);
307 select_timeout.tv_sec = dpid_idle_timeout;
308 select_timeout.tv_usec = 0;
309 selected_set = sock_set;
310 n = select(FD_SETSIZE, &selected_set, NULL, NULL, &select_timeout);
311 if (n == 0) { /* select timed out, try to exit */
312 /* BUG: This is a workaround for dpid not to exit when the
313 * downloads server is active. The proper way to handle it is with
314 * a dpip command that asks the server whether it's busy.
315 * Note: the cookies server may lose session info too. */
316 if (server_is_running("downloads"))
317 continue;
318
320 //cleanup();
321 exit(0);
322 }
323 } while (n == -1 && errno == EINTR);
324
325 if (n == -1) {
326 ERRMSG("main", "select", errno);
327 exit(1);
328 }
329 /* If the service req socket is selected then service the req. */
330 if (FD_ISSET(srs_fd, &selected_set)) {
331 int sock_fd;
332 socklen_t sin_sz;
333 struct sockaddr_in sin;
334 char *req = NULL;
335
336 --n;
337 assert(n >= 0);
338 sin_sz = (socklen_t) sizeof(sin);
339 sock_fd = accept(srs_fd, (struct sockaddr *)&sin, &sin_sz);
340 if (sock_fd == -1) {
341 ERRMSG("main", "accept", errno);
342 MSG_ERR("accept on srs socket failed\n");
343 MSG_ERR("service pending connections, and continue\n");
344 } else {
345 int command;
346 Dsh *sh;
347
348 sh = a_Dpip_dsh_new(sock_fd, sock_fd, 1024);
349read_next:
350 req = get_request(sh);
351 command = get_command(sh, req);
352 switch (command) {
353 case AUTH_CMD:
354 if (a_Dpip_check_auth(req) != -1) {
355 dFree(req);
356 goto read_next;
357 }
358 break;
359 case BYE_CMD:
361 //cleanup();
362 exit(0);
363 break;
364 case CHECK_SERVER_CMD:
365 send_sockport(sock_fd, req, dpi_attr_list);
366 break;
367 case REGISTER_ALL_CMD:
369 break;
370 case UNKNOWN_CMD:
371 {
372 char *d_cmd = a_Dpip_build_cmd("cmd=%s msg=%s",
373 "DpiError", "Unknown command");
374 (void) CKD_WRITE(sock_fd, d_cmd);
375 dFree(d_cmd);
376 ERRMSG("main", "Unknown command", 0);
377 MSG_ERR(" for request: %s\n", req);
378 break;
379 }
380 case -1:
381 _ERRMSG("main", "get_command failed", 0);
382 break;
383 }
384 if (req)
385 free(req);
388 }
389 }
390
391 /* While there's a request on one of the plugin sockets
392 * find the matching plugin and start it. */
393 for (i = 0; n > 0 && i < numdpis; i++) {
394 if (FD_ISSET(dpi_attr_list[i].sock_fd, &selected_set)) {
395 --n;
396 assert(n >= 0);
397
398 if (dpi_attr_list[i].filter) {
399 /* start a dpi filter plugin and continue watching its socket
400 * for new connections */
401 (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
403 } else {
404 /* start a dpi server plugin but don't wait for new connections
405 * on its socket */
406 numsocks--;
407 assert(numsocks >= 0);
408 FD_CLR(dpi_attr_list[i].sock_fd, &sock_set);
409 if ((dpi_attr_list[i].pid = fork()) == -1) {
410 ERRMSG("main", "fork", errno);
411 /* exit(1); */
412 } else if (dpi_attr_list[i].pid == 0) {
413 /* child */
414 (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
416 }
417 }
418 }
419 }
420 }
421}
static Dsh * sh
Definition datauri.c:39
void dFree(void *mem)
Definition dlib.c:68
int dClose(int fd)
Close a FD handling EINTR.
Definition dlib.c:951
volatile sig_atomic_t caught_sigchld
Definition downloads.cc:218
@ REGISTER_SERVICE_CMD
Definition dpi.h:35
@ UNKNOWN_CMD
Definition dpi.h:30
@ REGISTER_ALL_CMD
Definition dpi.h:34
@ BYE_CMD
Definition dpi.h:32
@ AUTH_CMD
Definition dpi.h:31
@ CHECK_SERVER_CMD
Definition dpi.h:33
char * init_sockdir(char *dpi_socket_dir)
char * a_Dpi_sockdir_file(void)
Definition dpi.c:34
void send_sockport(int sock_fd, char *dpi_tag, struct dp *dpi_attr_list)
Definition dpid.c:868
int init_all_dpi_sockets(struct dp *dpi_attr_list)
Definition dpid.c:671
int register_all_cmd(void)
Definition dpid.c:811
int register_all(struct dp **attlist)
Definition dpid.c:347
int init_ids_srs_socket(void)
Definition dpid.c:623
void est_dpi_sigchld(void)
Definition dpid.c:717
void handle_sigchld(void)
Definition dpid.c:694
void stop_active_dpis(struct dp *dpi_attr_list, int numdpis)
Definition dpid.c:748
void est_dpi_terminator(void)
Definition dpid.c:117
int fill_services_list(struct dp *attlist, int numdpis, Dlist **services_list)
Definition dpid.c:437
#define CKD_WRITE(fd, msg)
Definition dpid_common.h:37
#define MSG_ERR(...)
Definition dpid_common.h:23
#define _ERRMSG(CALLER, CALLED, ERR)
Definition dpid_common.h:31
#define ERRMSG(CALLER, CALLED, ERR)
Definition dpid_common.h:29
void a_Dpip_dsh_free(Dsh *dsh)
Free the SockHandler structure.
Definition dpip.c:525
char * a_Dpip_build_cmd(const char *format,...)
Printf like function for building dpip commands.
Definition dpip.c:83
int a_Dpip_dsh_write_str(Dsh *dsh, int flush, const char *str)
Convenience function.
Definition dpip.c:374
char * a_Dpip_get_attr(const char *tag, const char *attrname)
Task: given a tag and an attribute name, return its value.
Definition dpip.c:192
char * a_Dpip_dsh_read_token(Dsh *dsh, int blocking)
Return a newlly allocated string with the next dpip token in the socket.
Definition dpip.c:493
void a_Dpip_dsh_close(Dsh *dsh)
Close this socket for reading and writing.
Definition dpip.c:504
int a_Dpip_check_auth(const char *auth_tag)
Check whether the given 'auth' string equals what dpid saved.
Definition dpip.c:201
Dsh * a_Dpip_dsh_new(int fd_in, int fd_out, int flush_sz)
Create and initialize a dpip socket handler.
Definition dpip.c:247
enum @2 dpi_errno
static int get_open_max(void)
Get MAX open FD limit (yes, it's tricky –Jcid).
Definition main.c:210
Dlist * services_list
Definition main.c:48
static int get_command(Dsh *sh, char *dpi_tag)
Definition main.c:152
char * srs_name
Definition main.c:44
static void start_server_plugin(struct dp dpi_attr)
Definition main.c:112
static int server_is_running(char *server_id)
Check whether a dpi server is running.
Definition main.c:193
int main(void)
Definition main.c:226
@ no_errors
Definition main.c:40
@ dpid_srs_addrinuse
Definition main.c:41
int numdpis
Definition main.c:45
int numsocks
Definition main.c:49
fd_set sock_set
Definition main.c:46
static char * get_request(Dsh *sh)
Definition main.c:136
struct dp * dpi_attr_list
Definition main.c:47
static int start_filter_plugin(struct dp dpi_attr)
Start a dpi filter plugin after accepting the pending connection \Return.
Definition main.c:60
int srs_fd
Definition main.c:50
sigset_t mask_sigchld
Definition main.c:35
Definition dlib.h:131
Dpip socket handler type.
Definition dpip.h:31
Definition dpid.h:35
char * path
Definition dpid.h:37
int sock_fd
Definition dpid.h:38