File: commit_agent.c

package info (click to toggle)
ctn 3.2.0~dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 16,924 kB
  • sloc: ansic: 179,652; makefile: 7,006; java: 1,863; csh: 1,067; yacc: 523; sh: 424; cpp: 394; sql: 389; lex: 170
file content (420 lines) | stat: -rw-r--r-- 10,880 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
          Copyright (C) 1993, 1994, RSNA and Washington University

          The software and supporting documentation for the Radiological
          Society of North America (RSNA) 1993, 1994 Digital Imaging and
          Communications in Medicine (DICOM) Demonstration were developed
          at the
                  Electronic Radiology Laboratory
                  Mallinckrodt Institute of Radiology
                  Washington University School of Medicine
                  510 S. Kingshighway Blvd.
                  St. Louis, MO 63110
          as part of the 1993, 1994 DICOM Central Test Node project for, and
          under contract with, the Radiological Society of North America.

          THIS SOFTWARE IS MADE AVAILABLE, AS IS, AND NEITHER RSNA NOR
          WASHINGTON UNIVERSITY MAKE ANY WARRANTY ABOUT THE SOFTWARE, ITS
          PERFORMANCE, ITS MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR
          USE, FREEDOM FROM ANY COMPUTER DISEASES OR ITS CONFORMITY TO ANY
          SPECIFICATION. THE ENTIRE RISK AS TO QUALITY AND PERFORMANCE OF
          THE SOFTWARE IS WITH THE USER.

          Copyright of the software and supporting documentation is
          jointly owned by RSNA and Washington University, and free access
          is hereby granted as a license to use this software, copy this
          software and prepare derivative works based upon this software.
          However, any distribution of this software source code or
          supporting documentation or derivative works (source code and
          supporting documentation) must include the three paragraphs of
          the copyright notice.
*/
/* Copyright marker.  Copyright will be inserted above.  Do not remove */
/*
**				DINPACS 97
**		     Electronic Radiology Laboratory
**		   Mallinckrodt Institute of Radiology
**		Washington University School of Medicine
**
** Module Name(s):
** Author, Date:	Stephen Moore, 22-Mar-1997
** Intent:		commit_agent is a server process that listens
**			for storage commit event notifications.
** Last Update:		$Author: smm $, $Date: 2001-12-27 19:30:47 $
** Source File:		$RCSfile: commit_agent.c,v $
** Revision:		$Revision: 1.5 $
** Status:		$State: Exp $
*/

static char rcsid[] = "$Revision: 1.5 $ $RCSfile: commit_agent.c,v $";

#include "ctn_os.h"

#if 0
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/param.h>
#ifdef SOLARIS
#include <netdb.h>
#endif
#include <stdlib.h>
#include <string.h>
#ifdef MACH
#include <unistd.h>
#endif
/*lint -e46*/
#include <sys/wait.h>
/*lint +e46*/
#ifdef MALLOC_DEBUG
#include "malloc.h"
#endif
#endif

#include "dicom.h"
#include "ctnthread.h"
#include "condition.h"
#include "tbl.h"
#include "lst.h"
#include "dicom_uids.h"
#include "dulprotocol.h"
#include "dicom_objects.h"
#include "dicom_messages.h"
#include "dicom_services.h"
#include "tbl.h"
#include "manage.h"
#include "idb.h"
#include "utility.h"

#include "storage_commit.h"

static void usageerror();
static CONDITION
    addChildProcess(PROCESS_ELEMENT * processElement, LST_HEAD ** list);
static CONDITION harvestChildrenProcesses(LST_HEAD ** list);
static CONDITION
checkOrganizationConnections(char *organization,
			     LST_HEAD ** l, int connections);
int maxPDU = 16384;
CTNBOOLEAN verboseDUL = FALSE;
CTNBOOLEAN verboseTBL = FALSE;
CTNBOOLEAN verboseSRV = FALSE;
CTNBOOLEAN silent = FALSE;
char *controlDatabase = "CTNControl";
static char *logFile = NULL;
static CTNBOOLEAN forgiveFlag = FALSE;

CTNBOOLEAN doVerification = FALSE;

main(int argc, char **argv)
{
    CONDITION cond;
    DUL_NETWORKKEY *network;
    DUL_ASSOCIATIONKEY *association = NULL;
    DUL_ASSOCIATESERVICEPARAMETERS service;
    DUL_PRESENTATIONCONTEXT *requestedCtx;
    int pid;
    int port;
    char node[MAXHOSTNAMELEN + 1] = "";
    CTNBOOLEAN singleUserMode = FALSE;
    LST_HEAD *processList = NULL;
    DMAN_HANDLE *dmanHandle;
    char uidError[] = "\
To use this port (%d), you must run as root or the application\n\
must be setuid root (see chmod)\n";
    char currentDate[DICOM_DA_LENGTH + 1];
    char currentTime[DICOM_TM_LENGTH + 1];
    char *logString = "%-10s (%s %s %s) (%s %s) %s %s %d\n";
    int maxPerOrganization = 1000;
    PROCESS_ELEMENT processElement;

    (void) gethostname(node, MAXHOSTNAMELEN);
    while (--argc > 0 && *(++argv)[0] == '-') {
	switch ((*argv)[1]) {
	case 'f':
	    if (argc < 1)
		usageerror();
	    argc--;
	    argv++;
            if (!*argv) {
                fprintf(stderr, "Missing argument for option -f\n");
                usageerror();
            }   
	    controlDatabase = *argv;
	    break;
	case 'l':
	    if (argc < 1)
		usageerror();
	    argc--;
	    argv++;
            if (!*argv) {
                fprintf(stderr, "Missing argument for option -l\n");
                usageerror();
            }   
	    logFile = *argv;
	    break;
	case 'i':
	    forgiveFlag = TRUE;
	    break;
	case 'n':
	    if (argc < 1)
		usageerror();
	    argc--;
	    argv++;
	    if (!*argv) {
		fprintf(stderr, "Missing argument for option -n\n");
		usageerror();
	    }
	    strcpy(node, *argv);
	    break;
	case 'q':
	    silent = TRUE;
	    break;
	case 's':
	    singleUserMode = TRUE;
	    break;
	case 'v':
	    verboseDUL = TRUE;
	    break;
	case 'x':
	    if (--argc < 1)
		usageerror();
	    argv++;
            if (!*argv) {
                fprintf(stderr, "Missing argument for option -x\n");
                usageerror();
            }   
	    if (strcmp(*argv, "TBL") == 0)
		verboseTBL = TRUE;
	    else if (strcmp(*argv, "SRV") == 0)
		verboseSRV = TRUE;
	    else
		usageerror();
	    break;
	default:
	    printf("Unrecognized option: %s\n", *argv);
	    break;
	}
    }
    if (argc < 1)
	usageerror();
    if (sscanf(*argv++, "%d", &port) != 1)
	usageerror();
    (void) signal(SIGUSR1, SIG_IGN);
    if (port < 1024) {
	if (geteuid() != 0) {
	    fprintf(stderr, uidError, port);
	    return (1);
	}
    }
    THR_Init();
    (void) TBL_Debug(verboseTBL);
    DUL_Debug(verboseDUL);
    SRV_Debug(verboseSRV);

    cond = DUL_InitializeNetwork(DUL_NETWORK_TCP, DUL_AEACCEPTOR,
				 (void *) &port, DUL_TIMEOUT,
			 DUL_ORDERBIGENDIAN | DUL_FULLDOMAINNAME, &network);
    if (cond != DUL_NORMAL) {
	COND_DumpConditions();
	THR_Shutdown();
	return (2);
    }
    setuid(getuid());		/* Return to proper uid */

    if ((processList = LST_Create()) == NULL) {
	fprintf(stderr, "Unable to create list for process elements\n");
	THR_Shutdown();
	return (3);
    }
    while (!CTN_ERROR(cond)) {
	cond = nextAssociationRequest(node, &network, &service, maxPDU,
				      forgiveFlag, &association,
				      &processElement);
	UTL_GetDicomDate(currentDate);
	UTL_GetDicomTime(currentTime);

	if (!singleUserMode) {
	    (void) harvestChildrenProcesses(&processList);
	}
	if (cond == APP_NORMAL) {
	    DUL_ABORTITEMS abortItems;
	    cond = checkOrganizationConnections(processElement.organization,
					  &processList, maxPerOrganization);
	    if (cond != APP_NORMAL) {
		abortItems.result = DUL_REJECT_TRANSIENT;
		abortItems.source = DUL_ULSU_REJECT;
		abortItems.reason = DUL_ULSU_REJ_NOREASON;
		(void) DUL_RejectAssociationRQ(&association, &abortItems);
	    }
	}
	if (cond == APP_NORMAL) {
	    if (!singleUserMode) {
		pid = fork();
	    } else
		pid = 0;

	    if (pid == 0) {
		if (!silent)
		    printf("Forked child\n");
		cond = DUL_AcknowledgeAssociationRQ(&association, &service);
		if (cond != DUL_NORMAL)
		    continue;

		cond = serviceRequests(&network, &association, &service);
		if (cond == SRV_PEERREQUESTEDRELEASE)
		    cond = SRV_NORMAL;
		if (!singleUserMode) {
		    break;
		}
	    } else {
		logMessage(logString, "ACCEPTED", service.callingAPTitle,
			   service.callingPresentationAddress,
			   processElement.organization,
			   service.calledAPTitle,
			   service.calledPresentationAddress,
			   currentDate, currentTime,
			   pid);

		(void) DUL_DropAssociation(&association);
		processElement.pid = pid;
		(void) addChildProcess(&processElement, &processList);
		if (!silent)
		    printf("Parent\n");
	    }
	} else {
	    logMessage(logString, "REJECTED", service.callingAPTitle,
		       service.callingPresentationAddress,
		       processElement.organization,
		       service.calledAPTitle,
		       service.calledPresentationAddress,
		       currentDate, currentTime,
		       0);
	    COND_DumpConditions();
	}
    }
    COND_DumpConditions();
    if (!silent)
	printf("Exiting\n");
#ifdef MALLOC_DEBUG
    malloc_verify(0);
    malloc_shutdown();
#endif
    THR_Shutdown();
    return (0);
}

static void
usageerror()
{
    char msg[] = "\
Usage: [-f db] [-i] [-l logfile] [-n node] [-q] [-v] [-x <fac>] port\n\
\n\
    -f    Set control database (default CTNControl)\n\
    -i    Ignore some problems in Association Request\n\
    -l    Place log of association requests in <logfile>\n\
    -n    Use node as name rather than hostname\n\
    -q    Quiet mode, don't dump a lot of messages to terminal\n\
    -v    Place DUL facility in verbose mode\n\
    -x    Place <fac> in verbose mode (<fac> = TBL, SRV)\n\
\n\
    port  TCP/IP port address\n";

    fprintf(stderr, "%s", msg);
    exit(5);
}

static CONDITION
addChildProcess(PROCESS_ELEMENT * processElement, LST_HEAD ** list)
{
    PROCESS_ELEMENT
    * e;

    if ((e = malloc(sizeof(*e))) == NULL)
	return 0;
    *e = *processElement;

    if (LST_Enqueue(list, e) != LST_NORMAL)
	return 0;

    return APP_NORMAL;
}

static CONDITION
harvestChildrenProcesses(LST_HEAD ** list)
{
    int
        pid;
    PROCESS_ELEMENT
	* e;

    while ((pid = waitpid(-1, NULL, WNOHANG)) > 0) {
	e = LST_Head(list);
	if (e != NULL)
	    (void) LST_Position(list, e);
	while (e != NULL) {
	    if (!silent)
		printf("%5d %s\n", e->pid, e->calledAPTitle);
	    if (e->pid == pid) {
		(void) LST_Remove(list, LST_K_BEFORE);
		free(e);
		e = NULL;
	    } else {
		e = LST_Next(list);
	    }
	}
    }
    return 1;
}

static CONDITION
checkOrganizationConnections(char *organization,
			     LST_HEAD ** l, int connections)
{
    PROCESS_ELEMENT *e;

    e = LST_Head(l);
    (void) LST_Position(l, e);

    while (e != NULL && connections > 0) {
	if (strcmp(e->organization, organization) == 0)
	    connections--;

	e = LST_Next(l);
    }

    if (connections > 0)
	return APP_NORMAL;
    else
	return 0;
}

void
logMessage(char *controlString,...)
{
    FILE *fp;
    va_list args;

    if (logFile == NULL)
	fp = stdout;
    else
	fp = fopen(logFile, "a");

    if (fp == NULL)
	return;

/*lint -e40 -e50 */
    va_start(args, controlString);
    if (controlString == NULL)
	controlString = "NULL Control string passedto PushCondition";
    (void) vfprintf(fp, controlString, args);
    (void) fflush(fp);
    va_end(args);
/*lint +e40 +e50 */

    if (logFile != NULL)
	(void) fclose(fp);
}