2
winion.c: background ION handle holder for Windows.
4
Author: Scott Burleigh, JPL
6
Copyright (c) 2011, California Institute of Technology.
7
ALL RIGHTS RESERVED. U.S. Government Sponsorship
13
#define MAX_ION_IPCS 200
28
static int noteSmSegmentIpc(IonIpc *ipc, int key)
32
sprintf(memName, "%d.mmap", key);
33
ipc->handle = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, memName);
34
if (ipc->handle == NULL) /* Not found. */
36
printf("Can't find memory segment: %u.\n",
37
(unsigned int) GetLastError());
44
static int noteSemaphoreIpc(IonIpc *ipc, int key)
46
char semaphoreName[32];
48
sprintf(semaphoreName, "%d.event", key);
49
ipc->handle = OpenEvent(EVENT_ALL_ACCESS, FALSE, semaphoreName);
50
if (ipc->handle == NULL) /* Not found. */
52
printf("Can't find semaphore %u.\n",
53
(unsigned int) GetLastError());
60
static int noteIpc(IonIpc *ipc, IonIpcType type, int key)
64
if (type == IonSmSegment)
66
result = noteSmSegmentIpc(ipc, key);
70
result = noteSemaphoreIpc(ipc, key);
82
int main(int argc, char *argv[])
84
IonIpc ipcs[MAX_ION_IPCS];
86
HANDLE hPipe = INVALID_HANDLE_VALUE;
88
BOOL fConnected = FALSE;
91
BOOL fSuccess = FALSE;
95
char reply[1] = { '\0' };
98
hPipe = CreateNamedPipe("\\\\.\\pipe\\ion.pipe", PIPE_ACCESS_DUPLEX,
99
PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE | PIPE_WAIT,
100
1, sizeof reply, sizeof msg, 0, NULL);
101
if (hPipe == INVALID_HANDLE_VALUE)
103
printf("winion failed creating pipe, error code %u.\n",
104
(unsigned int) GetLastError());
108
memset((char *) ipcs, 0, sizeof ipcs);
109
signal(SIGINT, SIG_IGN);
112
fConnected = ConnectNamedPipe(hPipe, NULL) ? TRUE
113
: (GetLastError() == ERROR_PIPE_CONNECTED);
116
printf("winion failed on connect, error code %u.\n",
117
(unsigned int) GetLastError());
121
/* Read one request to retain a handle. */
123
fSuccess = ReadFile(hPipe, msg, sizeof msg, &bytesRead, NULL);
124
if (!fSuccess || bytesRead == 0)
126
if (GetLastError() == ERROR_BROKEN_PIPE)
132
printf("winion failed reading msg, error code %u.\n",
133
(unsigned int) GetLastError());
137
/* Parse the message, open and retain the handle. */
139
reply[0] = 1; /* Okay. */
155
printf("winion retaining %d IPCs.\n", ipcsCount);
156
reply[0] = 0; /* Dummy. */
160
reply[0] = 0; /* Fail. */
163
if (reply[0]) /* Valid IPC type. */
165
memcpy((char *) &key, msg + 1, sizeof(DWORD));
166
for (i = 0; i < ipcsCount; i++)
168
if (ipcs[i].type == type && ipcs[i].key == key)
174
if (i == ipcsCount) /* New IPC. */
176
if (i == MAX_ION_IPCS)
178
reply[0] = 0; /* Fail. */
182
if (noteIpc(&ipcs[i], type, (int) key))
194
/* Tell the client to continue. */
196
fSuccess = WriteFile(hPipe, reply, sizeof reply, &bytesWritten,
198
if (!fSuccess || bytesWritten != sizeof reply)
200
printf("winion failed writing reply, error code %u.\n",
201
(unsigned int) GetLastError());
205
/* Now disconnect pipe so it can be reconnected. */
207
FlushFileBuffers(hPipe);
208
DisconnectNamedPipe(hPipe);
211
/* Disconnect pipe and terminate. */
213
FlushFileBuffers(hPipe);
214
DisconnectNamedPipe(hPipe);
216
/* Termination of process closes all handles. */