11 #include <panel-applet.h>
12 #include <doldaconnect/utils.h>
16 #define SUBPROCCMD "ksu fredrik -q -e /home/fredrik/bin/dctrmon"
24 size_t inbufsize, inbufdata;
27 static void pipefdcb(struct conduit *conduit, int fd, GdkInputCondition condition)
33 size_t argssize, argsdata;
34 struct transfer *transfer;
36 data = (struct data *)conduit->cdata;
37 if(conduit->state == CNDS_SYN)
38 condconnected(conduit);
39 sizebuf2(data->inbuf, data->inbufdata + 80, 1);
40 ret = read(data->fd, data->inbuf + data->inbufdata, data->inbufsize - data->inbufdata);
43 if((errno == EINTR) || (errno == EAGAIN)) /* Shouldn't happen, but, oh well... */
45 perror("conduit-pipe: read");
46 gdk_input_remove(data->gdktag);
48 kill(-data->subproc, SIGHUP);
56 gdk_input_remove(data->gdktag);
58 kill(-data->subproc, SIGHUP);
64 data->inbufdata += ret;
65 while((p = memchr(data->inbuf, '\n', data->inbufdata)) != NULL)
68 cmd = sstrdup(data->inbuf);
69 memmove(data->inbuf, p + 1, data->inbufdata -= (p - data->inbuf) + 1);
71 argssize = argsdata = 0;
75 if((p2 = strchr(p, '\t')) != NULL)
80 if(!strcmp(args[0], "N") && (argsdata >= 4))
82 transfer = newtransfer(conduit, args[1], atoi(args[2]), atoi(args[3]));
84 if(!strcmp(args[0], "D") && (argsdata >= 2))
86 if((transfer = findtransferbytag(conduit, args[1])) != NULL)
87 freetransfer(transfer);
89 if(!strcmp(args[0], "S") && (argsdata >= 3))
91 if((transfer = findtransferbytag(conduit, args[1])) != NULL)
92 transfersetsize(transfer, atoi(args[2]));
94 if(!strcmp(args[0], "P") && (argsdata >= 3))
96 if((transfer = findtransferbytag(conduit, args[1])) != NULL)
97 transfersetpos(transfer, atoi(args[2]));
104 static int init(struct conduit *conduit)
106 static int inited = 0;
111 signal(SIGCHLD, SIG_IGN);
114 data = smalloc(sizeof(*data));
115 memset(data, 0, sizeof(*data));
118 data->inbufsize = data->inbufdata = 0;
120 conduit->cdata = data;
124 static int connect(struct conduit *conduit)
130 data = conduit->cdata;
133 if((pid = fork()) < 0)
144 if((devnull = open("/dev/null", O_RDWR)) < 0)
151 /* Leave stderr as is */
152 execl("/bin/sh", "sh", "-c", SUBPROCCMD, NULL);
156 fcntl(pfd[0], F_SETFL, fcntl(pfd[0], F_GETFL) | O_NONBLOCK);
159 data->gdktag = gdk_input_add(pfd[0], GDK_INPUT_READ, (void (*)(gpointer, gint, GdkInputCondition))pipefdcb, conduit);
164 static void destroy(struct conduit *conduit)
168 data = conduit->cdata;
171 if(data->gdktag >= 0)
172 gdk_input_remove(data->gdktag);
173 if(data->subproc > 0)
174 kill(-data->subproc, SIGHUP);
177 if(data->inbuf != NULL)
180 conduit->cdata = NULL;
183 static struct conduitiface st_conduit_pipe =
190 struct conduitiface *conduit_pipe = &st_conduit_pipe;