00001 #include <sys/types.h>
00002 #include <sys/stat.h>
00003 #include <stdio.h>
00004 #include <stdlib.h>
00005 #include <fcntl.h>
00006 #include <errno.h>
00007 #include <unistd.h>
00008 #include <syslog.h>
00009 #include <string.h>
00010 #include <signal.h>
00012 #include <sys/ipc.h>
00013 #include <sys/msg.h>
00015 #include <unistd.h>
00016 #include <pthread.h>
00017
00018 #include <hlog.h>
00019 #include <hmsg.h>
00020 #include <hmqueue.h>
00021 #include <hmem.h>
00022
00023
00024 #include <integer_functions.h>
00025 #include <hstructures.h>
00026 #include <hprocess.h>
00027 #include <hevent.h>
00028
00029 #include "hdaemon.h"
00030 #include "hdsched.h"
00031 #include "hdmsg.h"
00032 #include "hdplacer.h"
00033 #include "hdtimer.h"
00034
00040 static void* process_request_(void* req_msg);
00041 static void cleanup_(int exit_code);
00042 static int handle_event_();
00043
00045 static struct hevent* event_handler = NULL;
00046
00047
00048 static int handle_event_()
00049 {
00050 enum hdmevent incoming_event = (enum hdmevent)hevent_wait(event_handler);
00051
00052
00053 switch (incoming_event) {
00054 case HDME_QUIT:
00055
00056 printf("wait_event_: Exiting message server thread.\n");
00057 cleanup_(EXIT_SUCCESS);
00058 break;
00059 }
00060
00061 return 0;
00062 }
00063
00064
00065 void* process_request_(void* req_msg)
00066 {
00067 hlog_write(HLOG_NORMAL, "process_request_: Processing message.\n");
00068
00069
00070 void* resp_msg = NULL;
00071
00072 hlog_write(HLOG_DEBUG, "process_request_: command=");
00073 hlog_write_integer(hmsg_get_command(req_msg));
00074 hlog_write_text(".\n");
00075
00076 switch (hmsg_get_command(req_msg)) {
00077
00078 case HMC_ALLOC: {
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095 break;
00096 }
00097 case HMC_EXEC:
00098 hlog_write(HLOG_NORMAL, "process_request_: HMC_EXEC\n");
00099 resp_msg = hmsg_create(HMT_CTRL);
00100
00101
00102 hmsg_set_return(resp_msg, HMR_OK);
00103 break;
00104 case HMC_FREE:
00105 hlog_write(HLOG_NORMAL, "process_request_: HMC_FREE\n");
00106 resp_msg = hmsg_create(HMT_CTRL);
00107
00108
00109 hmsg_set_return(resp_msg, HMR_OK);
00110 break;
00111 case HMC_LDDEV:
00112 hlog_write(HLOG_NORMAL, "process_request_: HMC_LDDEV\n");
00113 resp_msg = hmsg_create(HMT_CTRL);
00114 hmsg_set_return(resp_msg, HMR_OK);
00115 break;
00116 case HMC_RMDEV:
00117 hlog_write(HLOG_NORMAL, "process_request_: HMC_RMDEV\n");
00118 resp_msg = hmsg_create(HMT_CTRL);
00119 hmsg_set_return(resp_msg, HMR_OK);
00120 break;
00121 case HMC_RMQUE: {
00122
00123
00124
00125 hlog_write(HLOG_NORMAL, "process_request_: HMC_RMQUE\n");
00126 resp_msg = hmsg_create(HMT_CTRL);
00127
00128 struct hmqueue* reg_mqueue = hmqueue_add(hmsg_get_sender(req_msg), HMQ_CONNECT);
00129 if (reg_mqueue == NULL) {
00130 hlog_write(HLOG_ERROR, "process_request_: Could not connect to message queue.\n");
00131 hmsg_set_return(resp_msg, HMR_ERROR);
00132 } else {
00133 hlog_write(HLOG_DEBUG, "process_request_: Connected to message queue. msqid_req=");
00134 hlog_write_integer(hmqueue_get_id(reg_mqueue));
00135 hlog_write_text(". key=");
00136 hlog_write_integer(hmqueue_get_key(reg_mqueue));
00137 hlog_write_text(".\n");
00138 hmsg_set_return(resp_msg, HMR_OK);
00139 }
00140 reg_mqueue = NULL;
00141 break;
00142 }
00143 case HMC_READ:
00144 hlog_write(HLOG_NORMAL, "process_request_: HMC_READ\n");
00145 resp_msg = hmsg_create(HMT_CTRLMEM);
00146
00147
00148 hmsg_set_return(resp_msg, HMR_OK);
00149 break;
00150 case HMC_UMQUE:
00151 hlog_write(HLOG_NORMAL, "process_request_: HMC_UMQUE\n");
00152 hmqueue_remove_by_key(hmsg_get_sender(req_msg));
00153 break;
00154 case HMC_WRITE:
00155 hlog_write(HLOG_NORMAL, "process_request_: HMC_WRITE\n");
00156 resp_msg = hmsg_create(HMT_CTRL);
00157
00158
00159 hmsg_set_return(resp_msg, HMR_OK);
00160 break;
00161 case HMC_REGPROC:
00162
00163 hlog_write(HLOG_NORMAL, "process_request_: HMC_REGPROC\n");
00164 resp_msg = hmsg_create(HMT_CTRL);
00165 struct hprocess* process_new = hprocess_create(hmsg_get_nice(req_msg));
00166 hlog_write(HLOG_DEBUG, "process_request_: Filename in msg: ");
00167 hlog_write_text(hmsg_get_bitfilename(req_msg));
00168 hlog_write_text("\n");
00169 hprocess_set_bitfilename( process_new, hmsg_get_bitfilename(req_msg) );
00170 if (process_new == NULL) {
00171 hmsg_set_return(resp_msg, HMR_NOPID);
00172 } else {
00173 hprocess_set_bitfilename(process_new, hmsg_get_bitfilename(req_msg));
00174 hmsg_set_return(resp_msg, HMR_OK);
00175 hdsched_add_new_process(process_new);
00176 hdsched_notify(HDSE_NEW_PROCESS);
00177 }
00178 break;
00179 default:
00180 break;
00181 }
00182
00183 return resp_msg;
00184 }
00185
00186
00187 static void cleanup_(int exit_code)
00188 {
00189 if (exit_code == EXIT_FAILURE) {
00190 hlog_write(HLOG_ERROR, "HWOS message server terminated with error=");
00191 hlog_write_integer(errno);
00192 hlog_write_text(".\n");
00193 }
00194 else
00195 hlog_write(HLOG_NORMAL, "HWOS message server terminated normally.\n");
00196
00197
00198 hdtimer_notify(HDTE_QUIT);
00199 hdplacer_notify(HDPE_QUIT);
00200 hdsched_notify(HDSE_QUIT);
00201
00202 hevent_remove(event_handler);
00203 pthread_exit(NULL);
00204 }
00205
00206
00207 int hdmsg_notify(enum hdmevent event)
00208 {
00209 hevent_notify(event_handler, (int)event);
00210
00211 return 0;
00212 }
00213
00214
00215 int hdmsg_synchronize()
00216 {
00217
00218
00219 void* sync_msg = hmsg_create(HMT_CTRL);
00220 hmsg_send(sync_msg, hmqueue_get(hmqueue_get_daemonkey()));
00221 free(sync_msg);
00222
00223 return 0;
00224 }
00225
00226
00227 void* hdmsg_main()
00228 {
00229
00230 struct hmqueue* mqueue;
00231 mqueue = hmqueue_add(hmqueue_get_daemonkey(), HMQ_CREATE);
00232
00233
00234 void* req_msg;
00235
00236
00237 event_handler = hevent_create(HEVENT_NONBLOCKING);
00238 if (event_handler == NULL) {
00239 cleanup_(EXIT_FAILURE);
00240 }
00241
00242
00243 while (1) {
00244
00245 hlog_write(HLOG_NORMAL, "hdmsg_main: Waiting for message.\n");
00246 req_msg = hmsg_receive(mqueue);
00247
00248 if (req_msg == NULL) {
00249 hlog_write(HLOG_ERROR, "hdmsg_main: req_msg is NULL.\n");
00250 cleanup_(EXIT_FAILURE);
00251 }
00252
00253 void* resp_msg;
00254
00255 resp_msg = process_request_(req_msg);
00256
00257 if (resp_msg != NULL) {
00258
00259 hmsg_send(resp_msg, hmqueue_get(hmsg_get_sender(req_msg)));
00260 }
00261
00262
00263 handle_event_();
00264 }
00265
00266 cleanup_(EXIT_SUCCESS);
00267 }
00268
00269
00270
00271
00272
00273