33#include "../dlib/dlib.h"
34#include "../dpip/dpip.h"
63 int newsock, old_stdout=-1, old_stdin=-1;
65 struct sockaddr_un clnt_addr;
68 csz = (socklen_t)
sizeof(clnt_addr);
70 newsock = accept(dpi_attr.
sock_fd, (
struct sockaddr *) &clnt_addr, &csz);
72 ERRMSG(
"start_plugin",
"accept", errno);
74 dup2(STDIN_FILENO, old_stdin);
75 if (dup2(newsock, STDIN_FILENO) == -1) {
76 ERRMSG(
"start_plugin",
"dup2", errno);
77 MSG_ERR(
"ERROR in child proc for %s\n", dpi_attr.
path);
81 dup2(STDOUT_FILENO, old_stdout);
82 if (dup2(newsock, STDOUT_FILENO) == -1) {
83 ERRMSG(
"start_plugin",
"dup2", errno);
84 MSG_ERR(
"ERROR in child proc for %s\n", dpi_attr.
path);
87 if ((pid = fork()) == -1) {
88 ERRMSG(
"main",
"fork", errno);
93 if (execl(dpi_attr.
path, dpi_attr.
path, (
char*)NULL) == -1) {
94 ERRMSG(
"start_plugin",
"execl", errno);
95 MSG_ERR(
"ERROR in child proc for %s\n", dpi_attr.
path);
101 if (
dClose(newsock) == -1) {
102 ERRMSG(
"start_plugin",
"close", errno);
103 MSG_ERR(
"ERROR in child proc for %s\n", dpi_attr.
path);
108 dup2(old_stdin, STDIN_FILENO);
109 dup2(old_stdout, STDOUT_FILENO);
115 if (dup2(dpi_attr.
sock_fd, STDIN_FILENO) == -1) {
116 ERRMSG(
"start_plugin",
"dup2", errno);
117 MSG_ERR(
"ERROR in child proc for %s\n", dpi_attr.
path);
121 ERRMSG(
"start_plugin",
"close", errno);
122 MSG_ERR(
"ERROR in child proc for %s\n", dpi_attr.
path);
125 if (execl(dpi_attr.
path, dpi_attr.
path, (
char*)NULL) == -1) {
126 ERRMSG(
"start_plugin",
"execl", errno);
127 MSG_ERR(
"ERROR in child proc for %s\n", dpi_attr.
path);
158 if (dpi_tag == NULL) {
159 _ERRMSG(
"get_command",
"dpid tag is NULL", 0);
166 ERRMSG(
"get_command",
"a_Dpip_get_attr", 0);
167 MSG_ERR(
": dpid failed to parse cmd in %s\n", dpi_tag);
169 "DpiError",
"Failed to parse request");
173 }
else if (strcmp(
"auth", cmd) == 0) {
175 }
else if (strcmp(
"DpiBye", cmd) == 0) {
177 }
else if (strcmp(
"check_server", cmd) == 0) {
179 }
else if (strcmp(
"register_all", cmd) == 0) {
181 }
else if (strcmp(
"register_service", cmd) == 0) {
199 for (i = 0; i <
numdpis; i++) {
216 int ret = sysconf(_SC_OPEN_MAX);
229 int i, n = 0, open_max;
230 int dpid_idle_timeout = 60 * 60;
231 struct timeval select_timeout;
248 for (i = 3; i < open_max; i++)
263 ERRMSG(
"main",
"init_sockdir", 0);
264 MSG_ERR(
"Failed to create socket directory\n");
279 MSG_ERR(
"dpid refuses to start, possibly because:\n");
280 MSG_ERR(
"\t1) An instance of dpid is already running.\n");
281 MSG_ERR(
"\t2) A previous dpid didn't clean up on exit.\n");
285 ERRMSG(
"main",
"init_ids_srs_socket failed", 0);
295 (void) sigemptyset(&mask_none);
296 (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
298 printf(
"dpid started (found %d dpis)\n",
numdpis);
308 select_timeout.tv_sec = dpid_idle_timeout;
309 select_timeout.tv_usec = 0;
311 n = select(FD_SETSIZE, &selected_set, NULL, NULL, &select_timeout);
324 }
while (n == -1 && errno == EINTR);
327 ERRMSG(
"main",
"select", errno);
331 if (FD_ISSET(
srs_fd, &selected_set)) {
334 struct sockaddr_in sin;
339 sin_sz = (socklen_t)
sizeof(sin);
340 sock_fd = accept(
srs_fd, (
struct sockaddr *)&sin, &sin_sz);
342 ERRMSG(
"main",
"accept", errno);
343 MSG_ERR(
"accept on srs socket failed\n");
344 MSG_ERR(
"service pending connections, and continue\n");
374 "DpiError",
"Unknown command");
377 ERRMSG(
"main",
"Unknown command", 0);
378 MSG_ERR(
" for request: %s\n", req);
382 _ERRMSG(
"main",
"get_command failed", 0);
394 for (i = 0; n > 0 && i <
numdpis; i++) {
402 (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
411 ERRMSG(
"main",
"fork", errno);
415 (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
int dClose(int fd)
Close a FD handling EINTR.
volatile sig_atomic_t caught_sigchld
char * init_sockdir(char *dpi_socket_dir)
char * a_Dpi_sockdir_file(void)
void send_sockport(int sock_fd, char *dpi_tag, struct dp *dpi_attr_list)
int init_all_dpi_sockets(struct dp *dpi_attr_list)
int register_all_cmd(void)
int register_all(struct dp **attlist)
int init_ids_srs_socket(void)
void est_dpi_sigchld(void)
void handle_sigchld(void)
void stop_active_dpis(struct dp *dpi_attr_list, int numdpis)
void est_dpi_terminator(void)
int fill_services_list(struct dp *attlist, int numdpis, Dlist **services_list)
#define CKD_WRITE(fd, msg)
#define _ERRMSG(CALLER, CALLED, ERR)
#define ERRMSG(CALLER, CALLED, ERR)
void a_Dpip_dsh_free(Dsh *dsh)
Free the SockHandler structure.
char * a_Dpip_build_cmd(const char *format,...)
Printf like function for building dpip commands.
int a_Dpip_dsh_write_str(Dsh *dsh, int flush, const char *str)
Convenience function.
char * a_Dpip_get_attr(const char *tag, const char *attrname)
Task: given a tag and an attribute name, return its value.
char * a_Dpip_dsh_read_token(Dsh *dsh, int blocking)
Return a newlly allocated string with the next dpip token in the socket.
void a_Dpip_dsh_close(Dsh *dsh)
Close this socket for reading and writing.
int a_Dpip_check_auth(const char *auth_tag)
Check whether the given 'auth' string equals what dpid saved.
Dsh * a_Dpip_dsh_new(int fd_in, int fd_out, int flush_sz)
Create and initialize a dpip socket handler.
static int get_open_max(void)
Get MAX open FD limit (yes, it's tricky –Jcid).
static int get_command(Dsh *sh, char *dpi_tag)
static void start_server_plugin(struct dp dpi_attr)
static int server_is_running(char *server_id)
Check whether a dpi server is running.
static char * get_request(Dsh *sh)
struct dp * dpi_attr_list
static int start_filter_plugin(struct dp dpi_attr)
Start a dpi filter plugin after accepting the pending connection \Return.
Dpip socket handler type.