Use dot to make dependancy graphs in documentation
[dabba.git] / dabbad / dabbad.c
blob918eef4ef7f8ef99f85b0c3c87f97657f5694dc5
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__ */
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <errno.h>
33 #include <unistd.h>
34 #include <assert.h>
35 #include <getopt.h>
36 #include <sys/types.h>
37 #include <sys/stat.h>
38 #include <fcntl.h>
40 #include <dabbad/ipc.h>
41 #include <dabbad/help.h>
43 enum dabbad_opts {
44 OPT_DAEMONIZE,
45 OPT_HELP
48 /**
49 * \brief Dabbad options getter
50 * \return Dabbad option data structure
53 const struct option *dabbad_options_get(void)
55 static const struct option dabbad_long_options[] = {
56 {"daemonize", no_argument, NULL, OPT_DAEMONIZE},
57 {"help", no_argument, NULL, OPT_HELP},
58 {NULL, 0, NULL, 0}
61 return (dabbad_long_options);
64 /**
65 * \internal
66 * \brief Create dabbad pidfile
67 * \return 0 on success, else on failure.
69 * This function creates start time a file containing dabbad process id.
72 static inline int dabbad_pidfile_create(void)
74 int rc = EIO;
75 int pidfd = -1;
76 char pidstr[8] = { 0 };
77 ssize_t pidstrlen = 0;
79 pidfd = creat(DABBAD_PID_FILE, 0600);
81 if (pidfd < 0)
82 return errno;
84 pidstrlen = snprintf(pidstr, sizeof(pidstr), "%i\n", getpid());
86 if (write(pidfd, pidstr, pidstrlen) != pidstrlen) {
87 rc = EIO;
90 close(pidfd);
91 return rc;
94 /**
95 * \brief Dabbad entry point
96 * \param[in] argc Argument counter
97 * \param[in] argv Argument vector
98 * \return 0 on success, else on failure
100 * This function is dabbad entry point.
101 * It parses given arguments, configure the daemon accordingly and start
102 * IPC message polling.
105 int main(int argc, char **argv)
107 int opt, opt_idx;
108 int daemonize = 0;
110 assert(argc);
111 assert(argv);
113 while ((opt =
114 getopt_long_only(argc, argv, "", dabbad_options_get(),
115 &opt_idx)) != EOF) {
116 switch (opt) {
117 case OPT_DAEMONIZE:
118 daemonize = 1;
119 break;
120 case OPT_HELP:
121 default:
122 show_usage(dabbad_options_get());
123 return EXIT_FAILURE;
124 break;
129 if (daemonize) {
130 if (daemon(-1, 0)) {
131 perror("Could not daemonize process");
132 return errno;
136 assert(dabbad_pidfile_create());
137 dabbad_ipc_msg_init();
139 return dabbad_ipc_msg_poll();