[Initng-svn] r4355 - in initng/trunk: plugins/bash_parser
svn at initng.thinktux.net
svn at initng.thinktux.net
Sun Jun 4 14:47:03 CEST 2006
Author: jimmy
Date: Sun Jun 4 14:47:01 2006
New Revision: 4355
Modified:
initng/trunk/plugins/bash_parser/bp.c
initng/trunk/plugins/bash_parser/initng_bash_parser.c
initng/trunk/src/initng_fd.c
Log:
Finnaly!, initng_bash_parser works over a pipe AND as a fallback, over a fifo...
Modified: initng/trunk/plugins/bash_parser/bp.c
==============================================================================
--- initng/trunk/plugins/bash_parser/bp.c (original)
+++ initng/trunk/plugins/bash_parser/bp.c Sun Jun 4 14:47:01 2006
@@ -239,10 +239,10 @@
return (bp_send(&to_send));
}
-/* Open, Send, Close */
+/* Open, Send, Read, Close */
int bp_send(bp_req * to_send)
{
- int sock = -1;
+ int sock = 3; /* testing fd 3, that is the oficcial pipe to initng for this communication */
int len;
struct sockaddr_un sockname;
@@ -254,26 +254,31 @@
assert(to_send);
to_send->version = BASH_PARSER_VERSION;
- /* Create the socket. */
- sock = socket(PF_UNIX, SOCK_STREAM, 0);
- if (sock < 0)
+ /* check if we can use fd 3 to talk to initng */
+ if(fcntl(sock, F_GETFD)<0)
{
- message = strdup("Failed to init socket.");
- return (FALSE);
+
+ /* ELSE Create new socket. */
+ sock = socket(PF_UNIX, SOCK_STREAM, 0);
+ if (sock < 0)
+ {
+ message = strdup("Failed to init socket.");
+ return (FALSE);
+ }
+
+ /* Bind a name to the socket. */
+ sockname.sun_family = AF_UNIX;
+ strcpy(sockname.sun_path, SOCKET_PATH);
+ len = strlen(SOCKET_PATH) + sizeof(sockname.sun_family);
+
+ if (connect(sock, (struct sockaddr *) &sockname, len) < 0)
+ {
+ close(sock);
+ message = strdup("Error connecting to socket");
+ return (FALSE);
+ }
}
-
- /* Bind a name to the socket. */
- sockname.sun_family = AF_UNIX;
- strcpy(sockname.sun_path, SOCKET_PATH);
- len = strlen(SOCKET_PATH) + sizeof(sockname.sun_family);
-
- if (connect(sock, (struct sockaddr *) &sockname, len) < 0)
- {
- close(sock);
- message = strdup("Error connecting to socket");
- return (FALSE);
- }
-
+
/* Put it not to block, waiting for more data on rscv */
{
int cur = fcntl(sock, F_GETFL, 0);
@@ -302,8 +307,9 @@
return (FALSE);
}
- /* close the socket */
- close(sock);
+ /* close the socket, if its an own created one */
+ if(sock != 3)
+ close(sock);
/* if initng leave us a message, save it */
if (strlen(rep.message) > 1)
Modified: initng/trunk/plugins/bash_parser/initng_bash_parser.c
==============================================================================
--- initng/trunk/plugins/bash_parser/initng_bash_parser.c (original)
+++ initng/trunk/plugins/bash_parser/initng_bash_parser.c Sun Jun 4 14:47:01 2006
@@ -99,7 +99,7 @@
struct stat sock_stat;
f_module_h bpf = { &bp_incomming, FDW_READ, -1 };
-#define RSCV() (TEMP_FAILURE_RETRY(recv(fd, &req, sizeof(bp_req), 0)) == (signed) sizeof(bp_req))
+#define RSCV() (TEMP_FAILURE_RETRY(recv(fd, &req, sizeof(bp_req), 0)))
#define SEND() send(fd, &rep, sizeof(bp_rep), 0)
@@ -107,13 +107,24 @@
{
bp_req req;
bp_rep rep;
+ int r;
D_("Got connection\n");
memset(&req, 0, sizeof(bp_req));
memset(&rep, 0, sizeof(bp_rep));
/* use file descriptor, because fread hangs here? */
- if (!RSCV())
+ r=RSCV();
+
+ /* make sure it has not closed */
+ if(r==0)
+ {
+ /*printf("Closing %i.\n", fd);*/
+ close(fd);
+ return;
+ }
+
+ if (r != (signed) sizeof(bp_req))
{
F_("Could not read incomming bash_parser req.\n");
strcpy(rep.message, "Unable to read request");
@@ -632,7 +643,7 @@
active_db_h *new_active;
process_h *process;
pipe_h *current_pipe;
- printf("create_new_active(%s);\n", name);
+ /*printf("create_new_active(%s);\n", name);*/
/* check if initfile exists */
strncat(file, name, 1020 - strlen(SCRIPT_PATH));
@@ -703,11 +714,8 @@
static int get_pipe(active_db_h * service, process_h * process, pipe_h * pi)
{
- char buffer[1025];
- int r;
-
- printf("Got and get_pipe: %s, %i, %i\n", service->name, pi->dir, pi->targets[0]);
-
+ /*printf("get_pipe(%s, %i, %i);\n", service->name, pi->dir, pi->targets[0]);*/
+
/* extra check */
if(pi->dir != IN_AND_OUT_PIPE)
return(FALSE);
@@ -715,24 +723,11 @@
/* the pipe we opened was on fd 3 */
if(pi->targets[0] != 3)
return(FALSE);
-
- r=read(pi->pipe[1], buffer, 1024);
-
-
- /* if the other side closed the connection ... */
- if(r==0)
- {
- close(pi->pipe[1]);
- pi->pipe[1]=-1;
- }
-
- if(r>0)
- {
- /* terminate end, read does not do that */
- buffer[r]='\0';
- printf("LOOK: \"%s\"\n", buffer);
- }
+
+ /* handle the client in the same way, as a fifo connected one */
+ bp_handle_client(pi->pipe[1]);
+ /* return happy */
return(TRUE);
}
@@ -751,7 +746,7 @@
D_("adding hook, that will reopen socket, for every started service.\n");
initng_process_db_ptype_register(&parse);
- initng_plugin_hook_register(&g.FDWATCHERS, 30, &bpf);
+ /*initng_plugin_hook_register(&g.FDWATCHERS, 30, &bpf);*/
initng_plugin_hook_register(&g.SIGNAL, 50, &bp_check_socket);
initng_plugin_hook_register(&g.NEW_ACTIVE, 50, &create_new_active);
initng_plugin_hook_register(&g.PIPE_WATCHER, 30, &get_pipe);
@@ -773,7 +768,7 @@
/* remove hooks */
initng_process_db_ptype_unregister(&parse);
- initng_plugin_hook_unregister(&g.FDWATCHERS, &bpf);
+ /* initng_plugin_hook_unregister(&g.FDWATCHERS, &bpf);*/
initng_plugin_hook_unregister(&g.SIGNAL, &bp_check_socket);
initng_plugin_hook_unregister(&g.NEW_ACTIVE, &create_new_active);
initng_plugin_hook_unregister(&g.PIPE_WATCHER, &get_pipe);
Modified: initng/trunk/src/initng_fd.c
==============================================================================
--- initng/trunk/src/initng_fd.c (original)
+++ initng/trunk/src/initng_fd.c Sun Jun 4 14:47:01 2006
@@ -266,6 +266,7 @@
}
}
+#define STILL_OPEN(fd) (fcntl(fd, F_GETFD)>=0)
/*
* FILEDESCRIPTORPOLLNG
@@ -321,6 +322,14 @@
continue;
if (!currentC->c.fdh->call_module)
continue;
+
+ /* This is a expensive test, but better safe then sorry */
+ if(!STILL_OPEN(currentC->c.fdh->fds))
+ {
+ W_("%i is not open anymore.\n", currentC->c.fdh->fds);
+ currentC->c.fdh->fds=-1;
+ continue;
+ }
/*D_("adding fd #%i, from an call_db.\n", current->c.fdh->fds); */
if (currentC->c.fdh->what & FDW_READ)
@@ -346,6 +355,15 @@
current_pipe->dir == BUFFERED_OUT_PIPE) &&
current_pipe->pipe[0] > 2)
{
+ /* expensive test to make sure the pipe is open, before adding */
+ if(!STILL_OPEN(current_pipe->pipe[0]))
+ {
+ W_("%i is not open anymore.\n", current_pipe->pipe[0]);
+ current_pipe->pipe[0]=-1;
+ continue;
+ }
+
+
FD_SET(current_pipe->pipe[0], &readset);
added++;
}
@@ -353,6 +371,15 @@
if (current_pipe->dir == IN_AND_OUT_PIPE &&
current_pipe->pipe[1] > 2)
{
+ /* expensive test to make sure the pipe is open, before adding */
+ if(!STILL_OPEN(current_pipe->pipe[1]))
+ {
+ W_("%i is not open anymore.\n", current_pipe->pipe[1]);
+ current_pipe->pipe[1]=-1;
+ continue;
+ }
+
+
FD_SET(current_pipe->pipe[1], &readset);
added++;
}
@@ -360,6 +387,15 @@
if (current_pipe->dir == IN_PIPE &&
current_pipe->pipe[1] > 2)
{
+ /* expensive test to make sure the pipe is open, before adding */
+ if(!STILL_OPEN(current_pipe->pipe[1]))
+ {
+ W_("%i is not open anymore.\n", current_pipe->pipe[1]);
+ current_pipe->pipe[1]=-1;
+ continue;
+ }
+
+
FD_SET(current_pipe->pipe[1], &writeset);
added++;
}
@@ -516,6 +552,11 @@
}
}
}
+
+ if(retval!=0)
+ {
+ F_("There is a missed pipe or fd that we missed to poll!\n");
+ }
return;
}
More information about the Initng-svn
mailing list