00001
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 #include <unistd.h>
00038 #include <string.h>
00039 #include <strings.h>
00040 #include <sys/types.h>
00041 #include <sys/stat.h>
00042 #include <fcntl.h>
00043 #include <signal.h>
00044 #include <errno.h>
00045 #include <pthread.h>
00046 #include "sense_impl.h"
00047 #include "stacktrace.h"
00048
00049
00050 #ifndef RUNNING_DIR
00051 # define RUNNING_DIR "/tmp"
00052 #endif
00053 #ifndef LOCK_DIR
00054 # define LOCK_DIR "/var/run"
00055 #endif
00056
00057
00058 extern char lock_file[255];
00059
00060
00061 static void shutdown(int sig, siginfo_t *si, void *unused)
00062 {
00063 log_info("%s: Begin shutting down ...\n", APP);
00064 descendants_shutdown(sig);
00065 ancestors_shutdown(sig);
00066 }
00067
00068
00069 #ifndef DEBUG
00070 static void daemonize(char *lock_file)
00071 {
00072 int fd, err, lock;
00073 char pid_str[255];
00074 struct sigaction sa;
00075
00076 err = fork();
00077 if (err < 0)
00078 die("fork", "daemonize");
00079 if (err > 0)
00080 exit(0);
00081 setsid();
00082 for (fd = 0; fd <= getdtablesize(); fd++)
00083 close(fd);
00084 fd = open("/dev/null", O_RDWR);
00085 dup(fd);
00086 dup(fd);
00087 umask(027);
00088 chdir(RUNNING_DIR);
00089 if ((lock = open(lock_file, O_RDWR | O_CREAT, 0640)) < 0)
00090 die("open", lock_file);
00091 if (lockf(lock, F_TLOCK, 0) < 0)
00092 die("lockf", lock_file);
00093 sprintf(pid_str, "%d\n", getpid());
00094 write(lock, pid_str, strlen(pid_str));
00095
00096 sigemptyset(&sa.sa_mask);
00097 sa.sa_handler = SIG_IGN;
00098 if (sigaction(SIGCHLD, &sa, NULL) < 0)
00099 die("sigaction", "SIGCHLD");
00100 if (sigaction(SIGTSTP, &sa, NULL) < 0)
00101 die("sigaction", "SIGTSTP");
00102 if (sigaction(SIGTTOU, &sa, NULL) < 0)
00103 die("sigaction", "SIGTTOU");
00104 if (sigaction(SIGTTIN, &sa, NULL) < 0)
00105 die("sigaction", "SIGTTIN");
00106 }
00107 #endif // DEBUG
00108
00109
00110 void usage(char *app) {
00111 fprintf(stderr, "Usage: %s [--level h_level] [--id node_id] ", app);
00112 fprintf(stderr, "[CORBA parameters]\n");
00113 fprintf(stderr, " where\n");
00114 fprintf(stderr, " 'h_level' (required) represents this node's ");
00115 fprintf(stderr, "position in the network hierarchy in [0 - 255]\n");
00116 fprintf(stderr, " 'node_id' (required) is a unique identifier ");
00117 fprintf(stderr, "for this node in [0 - 4294967295\n");
00118 fprintf(stderr, " any remaining parameters are passed to the CORBA ");
00119 fprintf(stderr, "implementation\n");
00120 exit(-1);
00121 }
00122
00123
00124 int main(int argc, char *argv[])
00125 {
00126 int i, j;
00127 struct sigaction sa;
00128 char *app = rindex(argv[0], '/')+1;
00129 pthread_t descendants, ancestors;
00130 struct args arguments;
00131 bool_t level_set = false;
00132 bool_t id_set = false;
00133
00134 arguments.argc = argc;
00135 for (i = 0, j = 0; i < argc; i++) {
00136 char *substr = rindex(argv[i], '-');
00137 int idx = substr - argv[i];
00138 if (idx > 0 && idx < 3) {
00139 if (strcasecmp(substr, "level") == 0) {
00140 arguments.level = atoi(argv[++i]);
00141 arguments.argc -= 2;
00142 level_set = true;
00143 }
00144 else if (strcasecmp(substr, "id") == 0) {
00145 arguments.id = atoi(argv[++i]);
00146 arguments.argc -= 2;
00147 id_set = true;
00148 }
00149 else
00150 arguments.argv[j++] = argv[i];
00151 }
00152 else
00153 arguments.argv[j++] = argv[i];
00154 }
00155 if (id_set == false || level_set == false)
00156 usage(app);
00157
00158 start_log(app);
00159 sprintf(lock_file, "%s/%s", LOCK_DIR, app);
00160 #ifndef DEBUG
00161 daemonize(lock_file);
00162 #endif
00163 sa.sa_flags = SA_SIGINFO;
00164 sigemptyset(&sa.sa_mask);
00165 sa.sa_sigaction = shutdown;
00166 if (sigaction(SIGTERM, &sa, NULL) < 0)
00167 die("sigaction", "SIGTERM");
00168 if (sigaction(SIGINT, &sa, NULL) < 0)
00169 die("sigaction", "SIGINT");
00170 enable_backtrace();
00171
00172 if (pthread_create(&ancestors, NULL, ancestors_server, &arguments) != 0)
00173 die("Failed to start", "server thread");
00174 if (pthread_create(&descendants, NULL, descendants_client, &arguments) != 0)
00175 die("Failed to start", "descendants thread");
00176
00177 pthread_join(descendants, NULL);
00178 pthread_join(ancestors, NULL);
00179
00180 log_info("%s: shutdown\n", APP);
00181 stop_log();
00182 unlink(lock_file);
00183
00184 return 0;
00185 }