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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2219 - (hide 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 aramv 1589 #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 aramv 1609 #include "config.h"
15 aramv 1589 #include "xacml.h"
16     #include "xacml_server.h"
17     #include "eef_library.h"
18 aramv 1615 #include "cgul/log_to_file/log_to_file.h"
19 aramv 1621 #include "cgul_patched/network/net_common.h"
20 aramv 1589
21 aramv 1595 #define _MULTI_THREADED
22 aramv 1757 #define PORT 9100
23 aramv 1621 #define BACKLOG 100
24 aramv 1589
25     char* config_file_path; /* config file path */
26 aramv 1621 xacml_server_t server; /* xacml server object */
27     int signal_is_being_handled = 0; /* signal handler state/lock */
28 aramv 1589
29 aramv 1621 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 aramv 1589
35 aramv 1621 /**
36     * Prepares logging, initializes EEF and puts the EEF in threading mode
37     */
38 aramv 1589 EES_RC ees_start(void){
39 aramv 1619 /* set log options */
40 aramv 1706 if(cgul_openlog("/tmp/ees_log") != 0){
41     return EES_FAILURE;
42     }
43 aramv 1619 cgul_set_ident(BINARY_NAME);
44 aramv 1615 cgul_set_log_options(CGUL_SYSLOG | CGUL_LOGFILE | CGUL_STDERR);
45 aramv 1619
46 aramv 1589 /* reuse this part when implementing SIGHUP */
47 aramv 1615 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 aramv 1609 return EES_FAILURE;
52 aramv 1589 } else {
53 aramv 1615 EEF_startThreading();
54 aramv 1589 }
55 aramv 1621 EEF_log(LOG_NOTICE, "EES Ready.\n");
56 aramv 1589 return EES_SUCCESS;
57     }
58    
59 aramv 1621 /**
60     * Main EES loop. Starts xacml server and pauses.
61     */
62 aramv 1589 EES_RC ees_loop(void){
63 aramv 1609 while(1){ /* this is to prevent falling out of the loop when a signal gets handled */
64     signal_is_being_handled = 0;
65 aramv 1619 if(process_xacml() == EES_SUCCESS){
66 aramv 1621 printf("Paused.\n");
67 aramv 1619 pause();
68     } else {
69 aramv 1621 EEF_log(LOG_ERR, "Unable to start xacml server\n");
70     ees_stop();
71 aramv 1619 exit(EXIT_FAILURE);
72     }
73 aramv 1609 }
74 aramv 1589 return EES_SUCCESS;
75     }
76    
77 aramv 1621 /**
78     * Terminates EES
79     */
80 aramv 1589 EES_RC ees_stop(void){
81 aramv 1609 xacml_server_destroy(server);
82 aramv 1589 AOS_Clean();
83 aramv 1754 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 aramv 1589 fflush(stdout);
89     fflush(stderr);
90 aramv 1621 EEF_log(LOG_NOTICE, "EES Stopped.\n");
91 aramv 1615 cgul_closelog();
92 aramv 1589 return EES_SUCCESS;
93     }
94    
95 aramv 1621 /**
96     * Creates and starts xacml server.
97     */
98 aramv 1619 EES_RC process_xacml(void){
99 aramv 1621 int fd;
100 aramv 1692 /*shutdown(fd, 0);*/
101 aramv 1621 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 aramv 1606 }
109 aramv 1619 return EES_FAILURE;
110 aramv 1606 }
111    
112 aramv 1621 /**
113     * Handles incoming signals
114     */
115 aramv 1606 void signal_handler(int sig){
116 aramv 1661 printf("Got signal\n");
117 aramv 1609 if(signal_is_being_handled == 0){
118     signal_is_being_handled = 1;
119     switch(sig){
120     case SIGHUP:
121 aramv 1615 EEF_log(LOG_NOTICE, "Got SIGHUP - restart \n");
122 aramv 1609 ees_stop();
123 aramv 1615 if(ees_start() == EES_FAILURE){
124     EEF_log(LOG_ERR, "Unable to restart EES. Exiting...\n");
125     exit(EXIT_FAILURE);
126     }
127 aramv 1609 break;
128     case SIGTERM:
129 aramv 1615 EEF_log(LOG_NOTICE, "Got SIGTERM - shutting down\n");
130 aramv 1609 ees_stop();
131 aramv 1615 exit(EXIT_SUCCESS);
132 aramv 1609 break;
133     }
134     }
135 aramv 1606 }
136    
137 aramv 1621 /**
138     * Main
139     */
140 aramv 1589 int main (int argc, char* argv[]){
141 aramv 1661 struct sigaction sa;
142    
143 aramv 1589 if(argc > 1){
144     config_file_path = argv[1];
145     } else {
146 aramv 1845 config_file_path = "setup/ees.conf.example";
147 aramv 1589 }
148    
149 aramv 1661 /* catch hangup and term signals */
150     sa.sa_handler = signal_handler;
151     sigemptyset(&sa.sa_mask);
152     sa.sa_flags = SA_RESTART;
153 aramv 1609
154 aramv 1661 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 aramv 1754 #if ENABLE_DEBUG
162     cgul_set_log_options(cgul_get_log_options() | CGUL_STDDEBUG);
163     #endif
164 aramv 1615 if(ees_start() == EES_SUCCESS){
165 aramv 1589 #if !ENABLE_DEBUG
166 aramv 1615 EEF_daemonize();
167 aramv 1589 #endif
168    
169 aramv 1615 ees_loop();
170     return EXIT_SUCCESS;
171     }
172 aramv 1589
173 aramv 1615 return EXIT_FAILURE;
174 aramv 1589
175     }
176    

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