/[pdpsoft]/trunk/grid-mw-security/ees/src/app/main.c
ViewVC logotype

Contents of /trunk/grid-mw-security/ees/src/app/main.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2219 - (show annotations) (download) (as text)
Tue Mar 1 21:41:39 2011 UTC (11 years, 7 months ago) by aramv
File MIME type: text/x-chdr
File size: 4515 byte(s)
Moved away statics, removed dead code, not installing the test binaries. Added first sort of manpage.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <pthread.h>
4
5 #include <unistd.h>
6 #include <netinet/in.h>
7 #include <sys/types.h>
8 #include <sys/socket.h>
9 #include <string.h>
10 #include <netdb.h>
11 #include <arpa/inet.h>
12
13 #include "ees_eics.h"
14 #include "config.h"
15 #include "xacml.h"
16 #include "xacml_server.h"
17 #include "eef_library.h"
18 #include "cgul/log_to_file/log_to_file.h"
19 #include "cgul_patched/network/net_common.h"
20
21 #define _MULTI_THREADED
22 #define PORT 9100
23 #define BACKLOG 100
24
25 char* config_file_path; /* config file path */
26 xacml_server_t server; /* xacml server object */
27 int signal_is_being_handled = 0; /* signal handler state/lock */
28
29 void signal_handler(int sig); /* main signal handler */
30 EES_RC process_xacml(void); /* creates and starts new xacml server instance */
31 EES_RC ees_start(void); /* initializes EEF */
32 EES_RC ees_stop(void); /* terminates EEF */
33 EES_RC ees_loop(void); /* main loop - calls process_xacml and pauses */
34
35 /**
36 * Prepares logging, initializes EEF and puts the EEF in threading mode
37 */
38 EES_RC ees_start(void){
39 /* set log options */
40 if(cgul_openlog("/tmp/ees_log") != 0){
41 return EES_FAILURE;
42 }
43 cgul_set_ident(BINARY_NAME);
44 cgul_set_log_options(CGUL_SYSLOG | CGUL_LOGFILE | CGUL_STDERR);
45
46 /* reuse this part when implementing SIGHUP */
47 cgul_log(LOG_INFO, "Parsing config file %s\n", config_file_path);
48 if(EEF_init(config_file_path, cgul_log) == EES_FAILURE){
49 /* at this point, EEF_log can/will be a reference to the second argument passed to EEF_init */
50 EEF_log(LOG_ERR, "Failed to parse config file (%s), exiting...", config_file_path);
51 return EES_FAILURE;
52 } else {
53 EEF_startThreading();
54 }
55 EEF_log(LOG_NOTICE, "EES Ready.\n");
56 return EES_SUCCESS;
57 }
58
59 /**
60 * Main EES loop. Starts xacml server and pauses.
61 */
62 EES_RC ees_loop(void){
63 while(1){ /* this is to prevent falling out of the loop when a signal gets handled */
64 signal_is_being_handled = 0;
65 if(process_xacml() == EES_SUCCESS){
66 printf("Paused.\n");
67 pause();
68 } else {
69 EEF_log(LOG_ERR, "Unable to start xacml server\n");
70 ees_stop();
71 exit(EXIT_FAILURE);
72 }
73 }
74 return EES_SUCCESS;
75 }
76
77 /**
78 * Terminates EES
79 */
80 EES_RC ees_stop(void){
81 xacml_server_destroy(server);
82 AOS_Clean();
83 if(EEF_term() == EES_SUCCESS){
84 EEF_log(LOG_NOTICE, "EEF terminated!\n");
85 } else {
86 EEF_log(LOG_NOTICE, "Error while terminating EEF!\n");
87 }
88 fflush(stdout);
89 fflush(stderr);
90 EEF_log(LOG_NOTICE, "EES Stopped.\n");
91 cgul_closelog();
92 return EES_SUCCESS;
93 }
94
95 /**
96 * Creates and starts xacml server.
97 */
98 EES_RC process_xacml(void){
99 int fd;
100 /*shutdown(fd, 0);*/
101 if((fd = createAndSetUpATCPServerSocket(PORT, BACKLOG)) > -1){
102 xacml_init(); /* if you look at the code, you'll see this function is empty */
103 if(xacml_server_init(&server, ees_xacml_authorize, NULL) == XACML_RESULT_SUCCESS){
104 xacml_server_set_fd(server, fd);
105 xacml_server_start(server);
106 return EES_SUCCESS;
107 }
108 }
109 return EES_FAILURE;
110 }
111
112 /**
113 * Handles incoming signals
114 */
115 void signal_handler(int sig){
116 printf("Got signal\n");
117 if(signal_is_being_handled == 0){
118 signal_is_being_handled = 1;
119 switch(sig){
120 case SIGHUP:
121 EEF_log(LOG_NOTICE, "Got SIGHUP - restart \n");
122 ees_stop();
123 if(ees_start() == EES_FAILURE){
124 EEF_log(LOG_ERR, "Unable to restart EES. Exiting...\n");
125 exit(EXIT_FAILURE);
126 }
127 break;
128 case SIGTERM:
129 EEF_log(LOG_NOTICE, "Got SIGTERM - shutting down\n");
130 ees_stop();
131 exit(EXIT_SUCCESS);
132 break;
133 }
134 }
135 }
136
137 /**
138 * Main
139 */
140 int main (int argc, char* argv[]){
141 struct sigaction sa;
142
143 if(argc > 1){
144 config_file_path = argv[1];
145 } else {
146 config_file_path = "setup/ees.conf.example";
147 }
148
149 /* catch hangup and term signals */
150 sa.sa_handler = signal_handler;
151 sigemptyset(&sa.sa_mask);
152 sa.sa_flags = SA_RESTART;
153
154 if(sigaction(SIGHUP, &sa, NULL) != 0){
155 EEF_log(LOG_ERR, "Unable to add signal handler for SIGHUP\n");
156 }
157 if(sigaction(SIGTERM, &sa, NULL) != 0){
158 EEF_log(LOG_ERR, "Unable to add signal handler for SIGHUP\n");
159 }
160
161 #if ENABLE_DEBUG
162 cgul_set_log_options(cgul_get_log_options() | CGUL_STDDEBUG);
163 #endif
164 if(ees_start() == EES_SUCCESS){
165 #if !ENABLE_DEBUG
166 EEF_daemonize();
167 #endif
168
169 ees_loop();
170 return EXIT_SUCCESS;
171 }
172
173 return EXIT_FAILURE;
174
175 }
176

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28