docs: updated roadmap document.
[dabba.git] / dabbad / ipc.c
blobe493e585004bcf0a3b2f905d0ad3d94a7c545a51
1 /**
2 * \file ipc.c
3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (c) 2012
4 * \date 2012
5 */
7 /* __LICENSE_HEADER_BEGIN__ */
9 /*
10 * Copyright (C) 2012 Emmanuel Roullit <emmanuel.roullit@gmail.com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or (at
15 * your option) any later version.
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 * for more details.
22 * You should have received a copy of the GNU General Public License along
23 * with this program; if not, write to the Free Software Foundation, Inc.,
24 * 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
28 /* __LICENSE_HEADER_END__ */
30 #include <stdio.h>
31 #include <string.h>
32 #include <errno.h>
33 #include <assert.h>
34 #include <dabbad/ipc.h>
35 #include <dabbad/list.h>
36 #include <dabbad/capture.h>
38 /**
39 * \internal
40 * \brief Handle request received in incoming IPC message
41 * \return 0 on success, -1 on failure. Set errno accordingly on failure.
43 * This function checks the operation to perform in the IPC message and passes
44 * it to the right processing function.
47 static int dabbad_handle_msg(struct dabba_ipc_msg *msg)
49 int rc;
50 assert(msg);
52 switch (msg->msg_body.type) {
53 case DABBA_IFCONF:
54 rc = dabbad_ifconf_get(msg);
55 break;
56 case DABBA_CAPTURE_START:
57 rc = dabbad_capture_start(msg);
58 break;
59 case DABBA_CAPTURE_LIST:
60 rc = dabbad_capture_list(msg);
61 break;
62 case DABBA_CAPTURE_STOP:
63 rc = dabbad_capture_stop(msg);
64 break;
65 default:
66 rc = -1;
67 errno = ENOSYS;
68 break;
71 return rc;
74 /**
75 * \brief Initialize dabbad Inter Process Communication message queue
76 * \return 0 on success, msgctl(2) return code on failure.
78 * This function removes all previously created IPC message queue.
81 int dabbad_ipc_msg_init(void)
83 return msgctl(dabba_get_ipc_queue_id(0), IPC_RMID, NULL);
86 /**
87 * \brief Flush dabbad Inter Process Communication message queue
88 * \param[in] qid Dabba daemon message queue
90 * This function removes all previously created IPC message queue.
93 void dabbad_ipc_msg_flush(const int qid)
95 struct dabba_ipc_msg msg;
96 ssize_t rcv;
98 do {
99 rcv =
100 msgrcv(qid, &msg, sizeof(msg.msg_body), 0,
101 IPC_NOWAIT | MSG_NOERROR);
102 } while (rcv > 0);
106 * \brief Wait for IPC messages for dabbad.
107 * \return 0 on success, else otherwise.
109 * Dabbad main loop. It awaits for incoming IPC messages.
110 * When a message has been received, it is passed to a message handler which
111 * performs the request.
113 * When done, an IPC message is sent back with the requested information,
114 * error code, etc.
117 int dabbad_ipc_msg_poll(void)
119 int qid;
120 ssize_t rcv;
121 int snd;
122 struct dabba_ipc_msg msg;
124 qid = dabba_get_ipc_queue_id(IPC_CREAT | IPC_EXCL | 0660);
126 if (qid < 0) {
127 perror("Cannot get IPC id");
128 return errno;
131 dabbad_ipc_msg_flush(qid);
133 for (;;) {
134 memset(&msg, 0, sizeof(msg));
135 rcv = msgrcv(qid, &msg, sizeof(msg.msg_body), 0, 0);
137 if (rcv <= 0) {
138 perror("Error while receiving IPC msg");
139 return errno;
142 rcv = dabbad_handle_msg(&msg);
144 msg.msg_body.error = rcv;
146 if (rcv != 0) {
147 perror("Error while handling IPC msg");
150 snd = msgsnd(qid, &msg, sizeof(msg.msg_body), 0);
152 if (snd < 0) {
153 perror("Error while sending back IPC msg");
154 return errno;
158 return 0;