Added package section.
[dabba.git] / dabba / ipc.c
blobf3ae3462ec325f7c6e0595c060f19df5d058233d
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 <unistd.h>
33 #include <assert.h>
34 #include <errno.h>
35 #include <dabba/ipc.h>
37 /**
38 * \brief Communicate via IPC to the dabba daemon
39 * \param[in,out] msg IPC message
40 * \return 0 on success, else on failure.
42 * This function coordinates the communication between dabba and the dabba
43 * daemon. It first sends a command request packed in an IPC message and
44 * awaits for a response from the dabba daemon which contains the request error
45 * code and the requested data.
48 int dabba_ipc_msg(struct dabba_ipc_msg *msg)
50 int qid;
51 ssize_t rcv;
52 int snd;
54 assert(msg);
56 qid = dabba_get_ipc_queue_id(0660);
58 if (qid < 0) {
59 perror("Cannot get IPC id");
60 return errno;
63 snd = msgsnd(qid, msg, sizeof(msg->msg_body), 0);
65 if (snd < 0) {
66 perror("Error while sending IPC msg");
67 return errno;
70 usleep(100);
72 rcv = msgrcv(qid, msg, sizeof(msg->msg_body), 0, 0);
74 if (rcv <= 0) {
75 perror("Error while receiving IPC msg");
76 return errno;
79 if (msg->msg_body.error != 0) {
80 printf("The daemon reported: %s\n",
81 strerror(msg->msg_body.error));
82 return msg->msg_body.error;
85 return 0;