File: srmv1.c

package info (click to toggle)
dpm-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,788 kB
  • ctags: 10,782
  • sloc: ansic: 146,136; sh: 13,362; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 955; fortran: 113
file content (249 lines) | stat: -rw-r--r-- 6,221 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
/*
 * Copyright (C) 2004-2010 by CERN/IT/GD/CT
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: srmv1.c,v $ $Revision: 3181 $ $Date: 2010-02-22 07:49:38 +0100 (Mon, 22 Feb 2010) $ CERN IT-GD/CT Jean-Philippe Baud";
#endif /* not lint */

#include <signal.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include "Castor_limits.h"
#include "Cinit.h"
#include "Cnetdb.h"
#include "Cpool_api.h"
#include "cgsi_plugin.h"
#include "dpm.h"
#include "dpm_server.h"
#include "patchlevel.h"
#include "serrno.h"
#include "srm_server.h"
#include "srmv1H.h"
#include "ISRM.nsmap"

char db_name[33];
char db_pwd[33];
char db_srvr[33];
char db_user[33];
struct srm_srv_thread_info srm_srv_thread_info[SRMV1_NBTHREADS];
char func[16];
int jid;
char localdomain[CA_MAXHOSTNAMELEN+1];
char localhost[CA_MAXHOSTNAMELEN+1];
char logfile[CA_MAXPATHLEN+1];
int nb_supported_protocols;
char **supported_protocols;
extern int Cdomainname (char *, int);
static int na_key = -1; 

srm_main(main_args)
struct main_args *main_args;
{
	int c;
	FILE *cf;
	char cfbuf[80];
	void *process_request(void *);
	char dpmconfigfile[CA_MAXPATHLEN+1];
	int flags;
	char *getconfent();
	int i;
	int ipool;
	char *p;
	char *p_n, *p_p, *p_s, *p_u;
	int port;
	struct soap soap;
	int thread_index;
	struct soap *tsoap;

	jid = getpid();
	strcpy (func, "srmv1");
	dpmconfigfile[0] = '\0';
	strcpy (logfile, LOGFILE);

	/* process command line options if any */

	while ((c = getopt (main_args->argc, main_args->argv, "c:l:")) != EOF) {
		switch (c) {
		case 'c':
			strncpy (dpmconfigfile, optarg, sizeof(dpmconfigfile));
			dpmconfigfile[sizeof(dpmconfigfile) - 1] = '\0';
			break;
		case 'l':
			strncpy (logfile, optarg, sizeof(logfile));
			logfile[sizeof(logfile) - 1] = '\0';
			break;
		}
	}

	srmlogit (func, "started (srmv1 %s-%d)\n", BASEVERSION, PATCHLEVEL);
	gethostname (localhost, CA_MAXHOSTNAMELEN+1);
	if (Cdomainname (localdomain, sizeof(localdomain)) < 0) {
		srmlogit (func, "Unable to get local domain name\n");
		return (2);
	}
	if (strchr (localhost, '.') == NULL) {
		strcat (localhost, ".");
		strcat (localhost, localdomain);
	}

	/* Get list of supported protocols */

	if ((nb_supported_protocols = get_supported_protocols (&supported_protocols)) < 0) {
		srmlogit (func, SRM02, "get_supported_protocols", strerror (ENOMEM));
		return (SYERR);
	}

	/* get DB login info from the Disk Pool Manager server config file */

	if (! *dpmconfigfile) {
		if (strncmp (DPMCONFIG, "%SystemRoot%\\", 13) == 0 &&
		    (p = getenv ("SystemRoot")))
			sprintf (dpmconfigfile, "%s%s", p, strchr (DPMCONFIG, '\\'));
		else
			strcpy (dpmconfigfile, DPMCONFIG);
	}
	if ((cf = fopen (dpmconfigfile, "r")) == NULL) {
		srmlogit (func, SRM23, dpmconfigfile);
		return (CONFERR);
	}
	if (fgets (cfbuf, sizeof(cfbuf), cf) &&
	    strlen (cfbuf) >= 5 && (p_u = strtok (cfbuf, "/\n")) &&
	    (p_p = strtok (NULL, "@\n")) && (p_s = strtok (NULL, "/\n"))) {
		if ((p_n = strtok (NULL, "\n")))
			strcpy (db_name, p_n);
		else
			strcpy (db_name, "dpm_db");
		strcpy (db_user, p_u);
		strcpy (db_pwd, p_p);
		strcpy (db_srvr, p_s);
	} else {
		srmlogit (func, SRM09, dpmconfigfile, "incorrect");
		return (CONFERR);
	}
	(void) fclose (cf);

	(void) dpm_init_dbpkg ();

	/* Create a pool of threads */

	if ((ipool = Cpool_create (SRMV1_NBTHREADS, NULL)) < 0) {
		srmlogit (func, SRM02, "Cpool_create", sstrerror (serrno));
		return (SYERR);
	}
	for (i = 0; i < SRMV1_NBTHREADS; i++) {
		srm_srv_thread_info[i].s = -1;
		srm_srv_thread_info[i].dbfd.idx = i;
	}

#if ! defined(_WIN32)
	signal (SIGPIPE, SIG_IGN);
	signal (SIGXFSZ, SIG_IGN);
#endif

	soap_init (&soap);
	soap.recv_timeout = SRM_TIMEOUT;
	flags = CGSI_OPT_DELEG_FLAG;
#ifdef VIRTUAL_ID
	flags |= CGSI_OPT_DISABLE_MAPPING;
#endif
	soap_register_plugin_arg (&soap, server_cgsi_plugin, &flags);
	soap.bind_flags |= SO_REUSEADDR;

	if ((p = getenv ("SRMV1_PORT")) || (p = getconfent ("SRMV1", "PORT", 0))) {
		port = atoi (p);
	} else {
		port = SRMV1_PORT;
	}
	if (soap_bind (&soap, NULL, port, BACKLOG) < 0) {
		srmlogit (func, SRM02, "soap_bind", strerror (soap.errnum));
		soap_done (&soap);
		return (SYERR);
	}

	/* main loop */

	while (1) {
		if (soap_accept (&soap) < 0) {
			srmlogit (func, SRM02, "soap_accept", strerror (soap.errnum));
			soap_done (&soap);
			return (1);
		}
		if ((tsoap = soap_copy (&soap)) == NULL) {
			srmlogit (func, SRM02, "soap_copy", strerror (ENOMEM));
			soap_done (&soap);
			return (1);
		}
		if ((thread_index = Cpool_next_index (ipool)) < 0) {
			srmlogit (func, SRM02, "Cpool_next_index",
			    sstrerror (serrno));
			return (SYERR);
		}
		tsoap->user = &srm_srv_thread_info[thread_index];
		if (Cpool_assign (ipool, &process_request, tsoap, 1) < 0) {
			free (tsoap);
			srm_srv_thread_info[thread_index].s = -1;
			srmlogit (func, SRM02, "Cpool_assign", sstrerror (serrno));
			return (SYERR);
		}
	}
	soap_done (&soap);
	return (0);
}

main(argc, argv)
int argc;
char **argv;
{
#if ! defined(_WIN32)
	struct main_args main_args;

	if (Cinitdaemon ("srmv1", NULL) < 0)
		exit (SYERR);
	main_args.argc = argc;
	main_args.argv = argv;
	exit (srm_main (&main_args));
#else
	if (Cinitservice ("srmv1", &srm_main))
		exit (SYERR);
#endif
}

void *
process_request(void *soap)
{
	const char *clientip;
	const char *clientname;
	size_t errlen;
	char *errstr;
	const char *s;
	struct srm_srv_thread_info *thip = ((struct soap *)soap)->user;

	if (soap_serve ((struct soap *)soap)) {
		(void) Cgetnetaddress (-1, &((struct soap *)soap)->peer,
		    ((struct soap *)soap)->peerlen, &na_key, &clientip, &clientname, 0, 0);
		if (clientip == NULL)
			clientip = "unknown";
		if (clientname == NULL)
			clientname = "unknown";
		s = *soap_faultstring ((struct soap *)soap);
		if (s == NULL)
			s = "unknown failure";
		errlen = strlen (clientip) + 5 + strlen (s) + 1;
		errlen += strlen (clientname) + 3;
		errstr = malloc (errlen);
		if (errstr != NULL) {
			snprintf (errstr, errlen, "[%s] (%s) : %s", clientip, clientname, s);
			srmlogit (func, SRM02, "soap_serve", errstr);
			free (errstr);
		}
	}

	thip->s = -1;
	soap_end ((struct soap *)soap);
	soap_done ((struct soap *)soap);
	free (soap);
	return (NULL);
}