2 gcc -fPIC -c -g -O2 testload.c
3 gcc -shared tclWinkill.o -ltclstub -o winkill.dll
4 and then load winkill.dll
5 winkill -pid 23423 -signal INT
6 # should rewrite this all to make more general utilitity:
7 sharedmem init name length
8 sharedmem set name ind value
9 sharedmem get name ind length
15 to just init the shared memory..
26 #undef TCL_STORAGE_CLASS
27 #define TCL_STORAGE_CLASS DLLEXPORT
32 #define signal_mask(n) (1 << (n))
35 typedef struct _sharedMemory sharedMemory
;
36 struct _sharedMemory
{
45 static sharedMemory
*sharedMemoryPtr
;
47 #define MEMSIZE 0x10000
51 typedef struct {int signumber
; char *name
;} sigNameStruct
;
52 sigNameStruct sigNames
[]=
55 { SIGHUP
, "HUP" }, /* Hangup (POSIX). */
58 { SIGINT
, "INT" }, /* Interrupt (ANSI). */
61 { SIGQUIT
, "QUIT" }, /* Quit (POSIX). */
64 { SIGILL
, "ILL" }, /* Illegal instruction (ANSI). */
67 { SIGTRAP
, "TRAP" }, /* Trace trap (POSIX). */
70 { SIGABRT
, "ABRT" }, /* Abort (ANSI). */
73 { SIGIOT
, "IOT" }, /* IOT trap (4.2 BSD). */
76 { SIGBUS
, "BUS" }, /* BUS error (4.2 BSD). */
79 { SIGFPE
, "FPE" }, /* Floating-point exception (ANSI). */
82 { SIGKILL
, "KILL" }, /* Kill, unblockable (POSIX). */
85 { SIGUSR1
, "USR1" }, /* User-defined signal 1 (POSIX). */
88 { SIGSEGV
, "SEGV" }, /* Segmentation violation (ANSI). */
91 { SIGUSR2
, "USR2" }, /* User-defined signal 2 (POSIX). */
94 { SIGPIPE
, "PIPE" }, /* Broken pipe (POSIX). */
97 { SIGALRM
, "ALRM" }, /* Alarm clock (POSIX). */
100 { SIGTERM
, "TERM" }, /* Termination (ANSI). */
103 { SIGSTKFLT
, "STKFLT" }, /* Stack fault. */
106 { SIGCLD
, "CLD" }, /* Same as SIGCHLD (System V). */
109 { SIGCHLD
, "CHLD" }, /* Child status has changed (POSIX). */
112 { SIGCONT
, "CONT" }, /* Continue (POSIX). */
115 { SIGSTOP
, "STOP" }, /* Stop, unblockable (POSIX). */
118 { SIGTSTP
, "TSTP" }, /* Keyboard stop (POSIX). */
121 { SIGTTIN
, "TTIN" }, /* Background read from tty (POSIX). */
124 { SIGTTOU
, "TTOU" }, /* Background write to tty (POSIX). */
127 { SIGURG
, "URG" }, /* Urgent condition on socket (4.2 BSD). */
130 { SIGXCPU
, "XCPU" }, /* CPU limit exceeded (4.2 BSD). */
133 { SIGXFSZ
, "XFSZ" }, /* File size limit exceeded (4.2 BSD). */
136 { SIGVTALRM
, "VTALRM" }, /* Virtual alarm clock (4.2 BSD). */
139 { SIGPROF
, "PROF" }, /* Profiling alarm clock (4.2 BSD). */
142 { SIGWINCH
, "WINCH" }, /* Window size change (4.3 BSD, Sun). */
145 { SIGPOLL
, "POLL" }, /* Pollable event occurred (System V). */
148 { SIGIO
, "IO" }, /* I/O now possible (4.2 BSD). */
151 { SIGPWR
, "PWR" }, /* Power failure restart (System V). */
160 Tcl_WinKillCmd(ClientData clientData
,
166 void close_shared_memory1();
167 void close_shared_memory(ClientData clientData
);
169 EXTERN
int Tclwinkill_Init( Tcl_Interp
*interp
);
175 Tclwinkill_Init( Tcl_Interp
*interp
)
177 if (!Tcl_InitStubs(interp
, "8.0", 0)) {
181 atexit(close_shared_memory1
);
183 Tcl_CreateCommand(interp
, "winkill" ,Tcl_WinKillCmd
,NULL
,close_shared_memory
);
190 void close_shared_memory(ClientData data
)
192 if (--refcount
<= 0) {
193 sharedMemory
*p
,*old
= sharedMemoryPtr
;
196 if (p
->handle
) CloseHandle(p
->handle
);
198 if (p
->address
) UnmapViewOfFile(p
->address
);
205 sharedMemoryPtr
=NULL
;
208 #define ErrorHandler(x) do {Tcl_AppendResult(interp,x,0); return NULL;} while(0)
211 getSharedMemoryPtr(Tcl_Interp
*interp
,int pid
)
214 sharedMemory
* shmPtr
= sharedMemoryPtr
;
216 if (shmPtr
->pid
== pid
) {
223 memset(&shm
,0,sizeof(sharedMemory
));
226 shmPtr
->handle
= NULL
;
227 sprintf(shmPtr
->name
,"gcl-%d",pid
);
233 OpenFileMapping(FILE_MAP_WRITE
, /* Read/write permission. */
234 FALSE
, /* Do not inherit the name */
235 shmPtr
->name
); /* of the mapping object. */
237 if (shmPtr
->handle
== NULL
)
239 ErrorHandler("winkill: Could not open file-mapping object.");
243 MapViewOfFile(shmPtr
->handle
, /* Handle to mapping object. */
244 FILE_MAP_WRITE
, /* Read/write permission. */
245 0, /* Max. object size. */
246 0, /* Size of hFile. */
247 0); /* Map entire file. */
249 if (shmPtr
->address
== NULL
)
251 ErrorHandler("winkill: Could not map view of file.");
253 { sharedMemory
*newPtr
= malloc(sizeof(sharedMemory
));
255 newPtr
->next
= sharedMemoryPtr
;
256 sharedMemoryPtr
= newPtr
;
263 Tcl_WinKillCmd(ClientData clientData
,
274 sharedMemory
*shmPtr
;
275 sigNameStruct
*sigNamePtr
= sigNames
;
277 if (argc
< 3 || argv
[1][0] != '-') {
280 Tcl_AppendResult(interp
,"winkill -pid pid -signal SIG",0);
284 for (i
= 1 ; i
< argc
; i
+=2) {
285 if (argv
[i
][0]!='-') { goto USAGE
;}
286 if (argv
[i
][1]=='s' && strcmp(&argv
[i
][1],"signal")==0) {
287 in
= &(argv
[i
+1][1]);
288 if (sscanf(in
,"%d",&sig
)==0) {
289 while(sigNamePtr
->name
) {
290 if (strcmp(sigNamePtr
->name
,in
)==0) {
291 sig
= sigNamePtr
->signumber
;
299 Tcl_AppendResult(interp
,"Bad Signal",0);
302 value
|= signal_mask(sig
);
304 if (argv
[i
][1]=='p' && strcmp(&argv
[i
][1],"pid")==0) {
306 if (1 != sscanf(argv
[i
+1],"%d",&pid
)) {
307 Tcl_AppendResult(interp
,"Bad pid arg:",argv
[2],".",0);
313 if (pidPtr
== NULL
|| (shmPtr
= getSharedMemoryPtr(interp
,pid
))==NULL
) {
314 Tcl_AppendResult(interp
,"Could not open shared memory for pid ",
319 at
= (int *)(shmPtr
->address
);
326 void close_shared_memory1()
329 close_shared_memory(NULL
);