Fixed up copyright year mention.
[dabba.git] / dabbad / ipc.c
blobf5466b3836f3f29e430a54cc7b0d21fdd075a0d4
1 /* __LICENSE_HEADER_BEGIN__ */
3 /*
4 * Copyright (C) 2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>
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 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
22 /* __LICENSE_HEADER_END__ */
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
28 #include <dabbad/ipc.h>
29 #include <dabbad/list.h>
30 #include <dabbad/capture.h>
32 static int dabbad_handle_msg(struct dabba_ipc_msg *msg)
34 int rc;
35 assert(msg);
37 switch (msg->msg_body.type) {
38 case DABBA_IFCONF:
39 rc = dabbad_ifconf_get(msg);
40 break;
41 case DABBA_CAPTURE_START:
42 rc = dabbad_capture_start(msg);
43 break;
44 case DABBA_CAPTURE_LIST:
45 rc = dabbad_capture_list(msg);
46 break;
47 case DABBA_CAPTURE_STOP:
48 rc = dabbad_capture_stop(msg);
49 break;
50 default:
51 rc = -1;
52 errno = ENOSYS;
53 break;
56 return rc;
59 int dabbad_ipc_msg_init(void)
61 return msgctl(dabba_get_ipc_queue_id(0), IPC_RMID, NULL);
64 void dabbad_ipc_msg_flush(int qid)
66 struct dabba_ipc_msg msg;
67 ssize_t rcv;
69 do {
70 rcv =
71 msgrcv(qid, &msg, sizeof(msg.msg_body), 0,
72 IPC_NOWAIT | MSG_NOERROR);
73 } while (rcv > 0);
76 int dabbad_ipc_msg_poll(void)
78 int qid;
79 ssize_t rcv;
80 int snd;
81 struct dabba_ipc_msg msg;
83 qid = dabba_get_ipc_queue_id(IPC_CREAT | IPC_EXCL | 0660);
85 if (qid < 0) {
86 perror("Cannot get IPC id");
87 return errno;
90 dabbad_ipc_msg_flush(qid);
92 for (;;) {
93 memset(&msg, 0, sizeof(msg));
94 rcv = msgrcv(qid, &msg, sizeof(msg.msg_body), 0, 0);
96 if (rcv <= 0) {
97 perror("Error while receiving IPC msg");
98 return errno;
101 rcv = dabbad_handle_msg(&msg);
103 msg.msg_body.error = rcv;
105 if (rcv != 0) {
106 perror("Error while handling IPC msg");
109 snd = msgsnd(qid, &msg, sizeof(msg.msg_body), 0);
111 if (snd < 0) {
112 perror("Error while sending back IPC msg");
113 return errno;
117 return 0;