3 * \author written by Emmanuel Roullit emmanuel.roullit@gmail.com (c) 2012
7 /* __LICENSE_HEADER_BEGIN__ */
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
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__ */
34 #include <dabbad/ipc.h>
35 #include <dabbad/list.h>
36 #include <dabbad/capture.h>
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
)
52 switch (msg
->msg_body
.type
) {
54 rc
= dabbad_ifconf_get(msg
);
56 case DABBA_CAPTURE_START
:
57 rc
= dabbad_capture_start(msg
);
59 case DABBA_CAPTURE_LIST
:
60 rc
= dabbad_capture_list(msg
);
62 case DABBA_CAPTURE_STOP
:
63 rc
= dabbad_capture_stop(msg
);
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
);
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
;
100 msgrcv(qid
, &msg
, sizeof(msg
.msg_body
), 0,
101 IPC_NOWAIT
| MSG_NOERROR
);
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,
117 int dabbad_ipc_msg_poll(void)
122 struct dabba_ipc_msg msg
;
124 qid
= dabba_get_ipc_queue_id(IPC_CREAT
| IPC_EXCL
| 0660);
127 perror("Cannot get IPC id");
131 dabbad_ipc_msg_flush(qid
);
134 memset(&msg
, 0, sizeof(msg
));
135 rcv
= msgrcv(qid
, &msg
, sizeof(msg
.msg_body
), 0, 0);
138 perror("Error while receiving IPC msg");
142 rcv
= dabbad_handle_msg(&msg
);
144 msg
.msg_body
.error
= rcv
;
147 perror("Error while handling IPC msg");
150 snd
= msgsnd(qid
, &msg
, sizeof(msg
.msg_body
), 0);
153 perror("Error while sending back IPC msg");