32#include "../dlib/dlib.h"
33#include "../dpip/dpip.h"
62 int newsock, old_stdout=-1, old_stdin=-1;
64 struct sockaddr_un clnt_addr;
67 csz = (socklen_t)
sizeof(clnt_addr);
69 newsock = accept(dpi_attr.
sock_fd, (
struct sockaddr *) &clnt_addr, &csz);
71 ERRMSG(
"start_plugin",
"accept", errno);
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);
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);
86 if ((pid = fork()) == -1) {
87 ERRMSG(
"main",
"fork", errno);
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);
100 if (
dClose(newsock) == -1) {
101 ERRMSG(
"start_plugin",
"close", errno);
102 MSG_ERR(
"ERROR in child proc for %s\n", dpi_attr.
path);
107 dup2(old_stdin, STDIN_FILENO);
108 dup2(old_stdout, STDOUT_FILENO);
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);
120 ERRMSG(
"start_plugin",
"close", errno);
121 MSG_ERR(
"ERROR in child proc for %s\n", dpi_attr.
path);
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);
157 if (dpi_tag == NULL) {
158 _ERRMSG(
"get_command",
"dpid tag is NULL", 0);
165 ERRMSG(
"get_command",
"a_Dpip_get_attr", 0);
166 MSG_ERR(
": dpid failed to parse cmd in %s\n", dpi_tag);
168 "DpiError",
"Failed to parse request");
172 }
else if (strcmp(
"auth", cmd) == 0) {
174 }
else if (strcmp(
"DpiBye", cmd) == 0) {
176 }
else if (strcmp(
"check_server", cmd) == 0) {
178 }
else if (strcmp(
"register_all", cmd) == 0) {
180 }
else if (strcmp(
"register_service", cmd) == 0) {
198 for (i = 0; i <
numdpis; i++) {
215 int ret = sysconf(_SC_OPEN_MAX);
228 int i, n = 0, open_max;
229 int dpid_idle_timeout = 60 * 60;
230 struct timeval select_timeout;
247 for (i = 3; i < open_max; i++)
262 ERRMSG(
"main",
"init_sockdir", 0);
263 MSG_ERR(
"Failed to create socket directory\n");
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");
284 ERRMSG(
"main",
"init_ids_srs_socket failed", 0);
294 (void) sigemptyset(&mask_none);
295 (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
297 printf(
"dpid started\n");
307 select_timeout.tv_sec = dpid_idle_timeout;
308 select_timeout.tv_usec = 0;
310 n = select(FD_SETSIZE, &selected_set, NULL, NULL, &select_timeout);
323 }
while (n == -1 && errno == EINTR);
326 ERRMSG(
"main",
"select", errno);
330 if (FD_ISSET(
srs_fd, &selected_set)) {
333 struct sockaddr_in sin;
338 sin_sz = (socklen_t)
sizeof(sin);
339 sock_fd = accept(
srs_fd, (
struct sockaddr *)&sin, &sin_sz);
341 ERRMSG(
"main",
"accept", errno);
342 MSG_ERR(
"accept on srs socket failed\n");
343 MSG_ERR(
"service pending connections, and continue\n");
373 "DpiError",
"Unknown command");
376 ERRMSG(
"main",
"Unknown command", 0);
377 MSG_ERR(
" for request: %s\n", req);
381 _ERRMSG(
"main",
"get_command failed", 0);
393 for (i = 0; n > 0 && i <
numdpis; i++) {
401 (void) sigprocmask(SIG_SETMASK, &mask_none, NULL);
410 ERRMSG(
"main",
"fork", errno);
414 (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.