Debian Codegraph @

plugin/firewall/eurephiafw_helpers.c

raw
1 /* eurephiafw_helpers.c -- Helper functions, shared between main module and
2 * firewall module. Setting up POSIX MQ and semaphores
3 *
4 * GPLv2 only - Copyright (C) 2008 - 2012
5 * David Sommerseth <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; version 2
10 * of the License.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 /**
24 * @file eurephiafw_helpers.c
25 * @author David Sommerseth <[email protected]>
26 * @date 2009-08-14
27 *
28 * @brief Helper functions which is shared between the main eurephia-auth module and
29 * the firewall module. It takes care of preparing POSIX MQ queues and semaphores.
30 *
31 */
32
33 #include <stdio.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <unistd.h>
37 #include <semaphore.h>
38 #include <mqueue.h>
39
40 #define EUREPHIA_FWINTF
41 #include <eurephiafw_struct.h>
42 #include <eurephia_context.h>
43 #include "eurephia_log.h"
44 #include "eurephiafw.h"
45 #include "eurephiafw_helpers.h"
46
47 /**
48 * Prepares the POSIX Semaphores used to control the communication between the openvpn process
49 * and the firewall updater process.
50 *
51 * @param ctx eurephiaCTX
52 * @param cfg efw_threaddata, data used in the firewall process and contains pointers to the semaphores.
53 *
54 * @return Returns 1 on success, otherwise 0.
55 */
56 int efwSetupSemaphores(eurephiaCTX *ctx, efw_threaddata *cfg) {
57 // Initialise the main process' semaphore
58 cfg->semp_master = sem_open(SEMPH_MASTER, O_CREAT, 0666, 0);
59 if( cfg->semp_master == SEM_FAILED ) {
60 eurephia_log(ctx, LOG_FATAL, 0, "Could not setup semaphore for eFW master: %s", strerror(errno));
61 return 0;
62 }
63
64 // Initialise the worker process' semaphore
65 cfg->semp_worker = sem_open(SEMPH_WORKER, O_CREAT, 0666, 0);
66 if( cfg->semp_worker == SEM_FAILED ) {
67 eurephia_log(ctx, LOG_FATAL, 0, "Could not setup semaphore for eFW worker: %s", strerror(errno));
68 return 0;
69 }
70 return 1;
71 }
72
73
74 /**
75 * Removes the semaphores created by efwSetupSemaphores().
76 *
77 * @param ctx eurephiaCTX
78 * @param cfg efw_threaddata, data used in the firewall process and contains pointers to the semaphores.
79 *
80 * @return Returns 1 on success, otherwise 0.
81 */
82 int efwRemoveSemaphores(eurephiaCTX *ctx, efw_threaddata *cfg) {
83 if( sem_close(cfg->semp_worker) != 0 ) {
84 eurephia_log(ctx, LOG_WARNING, 0,
85 "eFW: Could not destroy semaphore for worker: %s", strerror(errno));
86 }
87 sem_unlink(SEMPH_WORKER);
88
89 if( sem_close(cfg->semp_master) != 0 ) {
90 eurephia_log(ctx, LOG_WARNING, 0,
91 "eFW: Could not destroy semaphore for master: %s", strerror(errno));
92 }
93 sem_unlink(SEMPH_MASTER);
94 return 1;
95 }
96
97
98 /**
99 * Creates the needed POSIX MQ message queues.
100 *
101 * @param ctx eurephiaCTX
102 * @param cfg efw_threaddata, data used in the firewall process and contains pointers to the MQ fd.
103 *
104 * @return Returns 1 on success, otherwise 0.
105 */
106 int efwSetupMessageQueue(eurephiaCTX *ctx, efw_threaddata *cfg) {
107 struct mq_attr mqattr;
108
109 // Prepare a POSIX Message Queue
110 mqattr.mq_maxmsg = 10;
111 mqattr.mq_msgsize = EFW_MSG_SIZE;
112 mqattr.mq_flags = 0;
113 cfg->msgq = mq_open(MQUEUE_NAME, O_RDWR | O_CREAT, 0600, &mqattr);
114 if( cfg->msgq < 0 ) {
115 eurephia_log(ctx, LOG_FATAL, 0, "Could not open message queue: %s", strerror(errno));
116 return 0;
117 }
118 return 1;
119 }
120
121
122 /**
123 * Removes the POSIX MQ message queues created by efwSetupMessageQueue()
124 *
125 * @param ctx eurephiaCTX
126 * @param cfg efw_threaddata, data used in the firewall process and contains pointers to the MQ fd.
127 *
128 * @return Returns 1 on success, otherwise 0.
129 */
130 int efwRemoveMessageQueue(eurephiaCTX *ctx, efw_threaddata *cfg) {
131 // Close and remove the message queue used
132 if( mq_close((*cfg).msgq) != 0 ) {
133 eurephia_log(ctx, LOG_WARNING, 0, "Could not do close the message queue used for eFW: %s",
134 strerror(errno));
135 }
136
137 if( mq_unlink(MQUEUE_NAME) != 0 ) {
138 eurephia_log(ctx, LOG_WARNING, 0, "Could not do close the message queue used for eFW: %s",
139 strerror(errno));
140 }
141 return 1;
142 }