Added dabba capture help command unit tests.
[dabba.git] / dabbad / dabbad.c
blob777256f83f4bb2dbf54dc1a8f1c55c07e5845189
1 /**
2 * \file dabbad.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__ */
32 =head1 NAME
34 dabbad - Multithreaded packet mmap daemon
36 =head1 SYNOPSIS
38 dabbad [--daemonize][--help]
40 =head1 DESCRIPTION
42 dabbad (dabba daemon) is here to manage multithreaded network captures.
43 It listens an IPC interface and waits for commands to execute.
45 =head1 EXAMPLES
47 =over
49 =item dabbad
51 Start dabbad in a classic process mode. It allows the user to
52 see debug messages.
54 =item dabbad --daemonize
56 Start dabbad as a daemon.
58 =item dabbad --help
60 Prints help content.
62 =back
64 =head1 AUTHOR
66 Written by Emmanuel Roullit <emmanuel.roullit@gmail.com>
68 =head1 BUGS
70 =over
72 =item Please report bugs to <https://github.com/eroullit/dabba/issues>
74 =item dabba project project page: <https://github.com/eroullit/dabba>
76 =back
78 =head1 COPYRIGHT
80 =over
82 =item Copyright © 2012 Emmanuel Roullit.
84 =item License GPLv2+: GNU GPL version 2 or later <http://gnu.org/licenses/gpl.html>.
86 =item This is free software: you are free to change and redistribute it.
88 =item There is NO WARRANTY, to the extent permitted by law.
90 =back
92 =cut
96 #include <stdio.h>
97 #include <stdlib.h>
98 #include <errno.h>
99 #include <unistd.h>
100 #include <assert.h>
101 #include <getopt.h>
102 #include <sys/types.h>
103 #include <sys/stat.h>
104 #include <fcntl.h>
106 #include <dabbad/ipc.h>
107 #include <dabbad/help.h>
109 enum dabbad_opts {
110 OPT_DAEMONIZE,
111 OPT_HELP
115 * \brief Dabbad options getter
116 * \return Dabbad option data structure
119 const struct option *dabbad_options_get(void)
121 static const struct option dabbad_long_options[] = {
122 {"daemonize", no_argument, NULL, OPT_DAEMONIZE},
123 {"help", no_argument, NULL, OPT_HELP},
124 {NULL, 0, NULL, 0}
127 return (dabbad_long_options);
131 * \internal
132 * \brief Create dabbad pidfile
133 * \return 0 on success, else on failure.
135 * This function creates start time a file containing dabbad process id.
138 static inline int dabbad_pidfile_create(void)
140 int rc = EIO;
141 int pidfd = -1;
142 char pidstr[8] = { 0 };
143 ssize_t pidstrlen = 0;
145 pidfd = creat(DABBAD_PID_FILE, 0600);
147 if (pidfd < 0)
148 return errno;
150 pidstrlen = snprintf(pidstr, sizeof(pidstr), "%i\n", getpid());
152 if (write(pidfd, pidstr, pidstrlen) != pidstrlen) {
153 rc = EIO;
156 close(pidfd);
157 return rc;
161 * \brief Dabbad entry point
162 * \param[in] argc Argument counter
163 * \param[in] argv Argument vector
164 * \return 0 on success, else on failure
166 * This function is dabbad entry point.
167 * It parses given arguments, configure the daemon accordingly and start
168 * IPC message polling.
171 int main(int argc, char **argv)
173 int opt, opt_idx;
174 int daemonize = 0;
176 assert(argc);
177 assert(argv);
179 while ((opt =
180 getopt_long_only(argc, argv, "", dabbad_options_get(),
181 &opt_idx)) != EOF) {
182 switch (opt) {
183 case OPT_DAEMONIZE:
184 daemonize = 1;
185 break;
186 case OPT_HELP:
187 default:
188 show_usage(dabbad_options_get());
189 return EXIT_SUCCESS;
190 break;
195 if (daemonize) {
196 if (daemon(-1, 0)) {
197 perror("Could not daemonize process");
198 return errno;
202 assert(dabbad_pidfile_create());
203 dabbad_ipc_msg_init();
205 return dabbad_ipc_msg_poll();