core/kill.c

Go to the documentation of this file.
00001 
00009 #ifdef WIN32
00010 #include "windows.h"
00011 #include "output.h"
00012 #include "signal.h"
00013 #include "process.h"
00014 
00015 /* KILLONLY is undefined when building the kill program, 
00016    and defined when compiling with the main GridLAB-D 
00017    core modules
00018  */
00019 #ifndef KILLONLY
00020 
00021 #include "gridlabd.h"
00022 
00023 static int handler_stop = 0;
00024 void kill_stophandler(void)
00025 {
00026     handler_stop = 1;
00027 }
00028 
00029 void msghandler(void *param)
00030 {
00031     char name[32];
00032     HANDLE hEvent;
00033     unsigned int sig = (unsigned int)(int64)param;
00034     unsigned int pid = _getpid();
00035     sprintf(name,"gridlabd.%d.%d",pid,sig);
00036     hEvent = CreateEventA(NULL,TRUE,FALSE,name);
00037     output_verbose("creating gridlabd signal handler %d for process %d",sig,pid);
00038     while (WaitForSingleObject(hEvent,INFINITE)==WAIT_OBJECT_0)
00039     {
00040         output_verbose("windows signal handler activated");
00041         raise(sig);
00042         ResetEvent(hEvent);
00043     }
00044 }
00045 
00046 void kill_starthandler(void)
00047 {
00048     if (_beginthread(&msghandler, 0, (void*)SIGINT)==1 || _beginthread(&msghandler, 0, (void*)SIGTERM)==1)
00049         output_error("kill handler failed to start");
00050     else
00051         output_verbose("windows message signal handlers started");
00052 }
00053 #else
00054 #define output_error printf
00055 #define output_verbose
00056 #endif
00057 
00060 int kill(unsigned short pid,    
00061          int sig)               
00062 {
00063     char name[32];
00064     HANDLE hEvent;
00065     sprintf(name,"gridlabd.%d.%d",(int)pid,sig);
00066     hEvent = OpenEventA(EVENT_MODIFY_STATE,FALSE,name);
00067     if (hEvent==NULL)
00068     {
00069         output_error("unable to signal gridlabd process %d with signal %d (error %d)", pid, sig, GetLastError());
00070         return 0;
00071     }
00072     else
00073     {
00074         SetEvent(hEvent);
00075         output_verbose("signal %d sent to gridlabd process %d", sig, pid);
00076         return 1;
00077     }
00078 }
00079 #endif
00080 

GridLAB-DTM Version 1.0
An open-source project initiated by the US Department of Energy