modified: configure.in
[katrin.git] / src / katrin / katrind.c
blob9f29364842fb32a8fe7f04de91ac5f49213042f7
1 #define _GNU_SOURCE
2 #include <stdio.h>
3 #include <unistd.h>
4 #include <stdlib.h>
5 #include <signal.h>
6 #include <syslog.h>
8 #include <sys/resource.h>
10 #include "def.h"
12 #include "utils/utils.h"
13 #include "conf/conf.h"
14 #include "module/module_api_declaration.h"
15 #include "module/module_api.h"
17 void term_handler(int i);
18 int daemon_init(const char* name, int facility);
19 int set_signal_handlers(void);
21 int main(void) {
22 /* Daemonization */
23 daemon_init("katrind", LOG_DAEMON);
25 /* Setting signal handlers map */
26 set_signal_handlers();
28 debug("katrind pid is %i\n", getpid());
30 /* Get initial configuration */
31 kcfg *cfg = get_katrin_cfg();
33 //get_katrin_cfg();
35 if (cfg == NULL) {
36 return EXIT_FAILURE;
39 /* Startup */
40 info("Start Katrin daemon");
41 add_module(DB, cfg->db_mod);
42 db.db_api.dbConnect();
44 /* Read service info from info collectors */
45 int pipedes[2];
46 pipe(pipedes);
48 char *info_mod = NULL;
49 service_list *p = NULL;
51 /* Loading business modules for each service */
53 for (p = cfg->service_list ; p != NULL ; p = p->next) {
54 debug("Service name: %s", p->name);
55 add_module(BZ, p->name);
57 list *l = NULL;
59 for (l = p->info_mod; l != NULL ; l = l->next) {
60 asprintf(&info_mod, "%s-%s", p->name, l->value);
61 debug("Info module: %s", info_mod);
63 add_module(INFO, info_mod, pipedes);
65 free(info_mod);
69 close(pipedes[1]);
71 /* Main collecting cycle */
73 char buf[1024];
74 int len = 0;
75 struct service_info *si = NULL;
76 struct user_service_info *uService = NULL;
78 while(1) {
79 len = read(pipedes[0], buf, 1024);
81 if(len != 0) {
82 si = (struct service_info *)buf;
84 debug("Service info was receive: %s", si->type);
86 if (get_service_cfg(cfg, si->type) == NULL)
87 continue;
89 /* Send service info to libkatrin-bz-<type_service> */
90 bz_module *bz_mod = get_bz_module(si->type);
92 uService = bz_mod->bz_api.service2userService(si);
94 debug("cost: %1.5f", uService->cost);
96 if (uService->cost > 0)
97 db.db_api.writeoffcost(uService->cost, uService->userid);
99 free(uService);
100 } else {
101 err("Nothing was readed - break!");
102 break;
106 close(pipedes[0]);
108 free_katrin_cfg(cfg);
109 return 0;
112 int daemon_init(const char* name, int facility) {
113 pid_t pid;
114 struct rlimit flim;
115 int i = 0;
117 if(getppid() != 1) {
118 signal(SIGTTOU, SIG_IGN);
119 signal(SIGTTIN, SIG_IGN);
120 signal(SIGTSTP, SIG_IGN);
123 if((pid == fork()) != 0) {
124 exit(0);
127 setsid();
129 chdir("/");
131 umask(0);
133 getrlimit(RLIMIT_NOFILE, &flim);
135 for(; i < flim.rlim_max; i++)
136 close(i);
138 openlog(name, LOG_PID, facility);
140 return 0;
143 int set_signal_handlers(void) {
144 signal(SIGTERM, term_handler);
147 struct sigaction sa;
148 sa.sa_handler = term_handler;
149 sigaction(SIGTERM, &sa, 0);
153 void term_handler(int signo) {
154 // Kill all info modules
155 debug("Kill all info modules");
156 remove_all_info_modules();
157 debug("Terminating");
158 exit(EXIT_SUCCESS);