8 #include <sys/resource.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);
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();
40 info("Start Katrin daemon");
41 add_module(DB
, cfg
->db_mod
);
42 db
.db_api
.dbConnect();
44 /* Read service info from info collectors */
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
);
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
);
71 /* Main collecting cycle */
75 struct service_info
*si
= NULL
;
76 struct user_service_info
*uService
= NULL
;
79 len
= read(pipedes
[0], buf
, 1024);
82 si
= (struct service_info
*)buf
;
84 debug("Service info was receive: %s", si
->type
);
86 if (get_service_cfg(cfg
, si
->type
) == NULL
)
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
);
101 err("Nothing was readed - break!");
108 free_katrin_cfg(cfg
);
112 int daemon_init(const char* name
, int facility
) {
118 signal(SIGTTOU
, SIG_IGN
);
119 signal(SIGTTIN
, SIG_IGN
);
120 signal(SIGTSTP
, SIG_IGN
);
123 if((pid
== fork()) != 0) {
133 getrlimit(RLIMIT_NOFILE
, &flim
);
135 for(; i
< flim
.rlim_max
; i
++)
138 openlog(name
, LOG_PID
, facility
);
143 int set_signal_handlers(void) {
144 signal(SIGTERM
, term_handler
);
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");