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
|
/*
* Copyright (C) 2005-2006 by CERN/IT/GD/CT
* All rights reserved
*/
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: dli.c,v $ $Revision: 1.12 $ $Date: 2008/02/14 09:06:17 $ 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 "dli_server.h"
#include "dliH.h"
#include "lfc_api.h"
#include "serrno.h"
#include "DataLocationInterface.nsmap"
struct dli_srv_thread_info dli_srv_thread_info[DLI_NBTHREADS];
char func[16];
int jid;
char logfile[CA_MAXPATHLEN+1];
static int na_key = -1;
dli_main(main_args)
struct main_args *main_args;
{
int c;
void *process_request(void *);
char *getconfent();
int i;
int ipool;
char *p;
int port;
struct soap soap;
int thread_index;
struct soap *tsoap;
jid = getpid();
strcpy (func, "dli");
strcpy (logfile, LOGFILE);
/* process command line options if any */
while ((c = getopt (main_args->argc, main_args->argv, "l:")) != EOF) {
switch (c) {
case 'l':
strncpy (logfile, optarg, sizeof(logfile));
logfile[sizeof(logfile) - 1] = '\0';
break;
}
}
dlilogit (func, "started\n");
/* Create a pool of threads */
if ((ipool = Cpool_create (DLI_NBTHREADS, NULL)) < 0) {
dlilogit (func, DLI02, "Cpool_create", sstrerror (serrno));
return (SYERR);
}
for (i = 0; i < DLI_NBTHREADS; i++) {
dli_srv_thread_info[i].s = -1;
}
#if ! defined(_WIN32)
signal (SIGPIPE, SIG_IGN);
signal (SIGXFSZ, SIG_IGN);
#endif
soap_init (&soap);
soap.recv_timeout = DLI_TIMEOUT;
soap.bind_flags |= SO_REUSEADDR;
if ((p = getenv ("DLI_PORT")) || (p = getconfent ("DLI", "PORT", 0))) {
port = atoi (p);
} else {
port = DLI_PORT;
}
if (soap_bind (&soap, NULL, port, BACKLOG) < 0) {
dlilogit (func, DLI02, "soap_bind", strerror (soap.errnum));
soap_done (&soap);
return (SYERR);
}
/* main loop */
while (1) {
if (soap_accept (&soap) < 0) {
dlilogit (func, DLI02, "soap_accept", strerror (soap.errnum));
soap_done (&soap);
return (1);
}
if ((tsoap = soap_copy (&soap)) == NULL) {
dlilogit (func, DLI02, "soap_copy", strerror (ENOMEM));
soap_done (&soap);
return (1);
}
if ((thread_index = Cpool_next_index (ipool)) < 0) {
dlilogit (func, DLI02, "Cpool_next_index",
sstrerror (serrno));
return (SYERR);
}
tsoap->user = &dli_srv_thread_info[thread_index];
if (Cpool_assign (ipool, &process_request, tsoap, 1) < 0) {
free (tsoap);
dli_srv_thread_info[thread_index].s = -1;
dlilogit (func, DLI02, "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 ("dli", NULL) < 0)
exit (SYERR);
main_args.argc = argc;
main_args.argv = argv;
exit (dli_main (&main_args));
#else
if (Cinitservice ("dli", &dli_main))
exit (SYERR);
#endif
}
void *
process_request(void *soap)
{
const char *clientip;
const char *clientname;
size_t errlen;
char *errstr;
const char *s;
struct dli_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);
dlilogit (func, DLI02, "soap_serve", errstr);
free (errstr);
}
}
thip->s = -1;
soap_end ((struct soap *)soap);
soap_done ((struct soap *)soap);
free (soap);
return (NULL);
}
/* dli_logreq - log a request */
/* Split the message into lines so they don't exceed LOGBUFSZ-1 characters
* A backslash is appended to a line to be continued
* A continuation line is prefixed by '+ '
*/
void
dli_logreq(func, logbuf)
char *func;
char *logbuf;
{
int n1, n2;
char *p;
char savechrs1[2];
char savechrs2[2];
n1 = LOGBUFSZ - strlen (func) - 36;
n2 = strlen (logbuf);
p = logbuf;
while (n2 > n1) {
savechrs1[0] = *(p + n1);
savechrs1[1] = *(p + n1 + 1);
*(p + n1) = '\\';
*(p + n1 + 1) = '\0';
dlilogit (func, DLI98, p);
if (p != logbuf) {
*p = savechrs2[0];
*(p + 1) = savechrs2[1];
}
p += n1 - 2;
savechrs2[0] = *p;
savechrs2[1] = *(p + 1);
*p = '+';
*(p + 1) = ' ';
*(p + 2) = savechrs1[0];
*(p + 3) = savechrs1[1];
n2 -= n1;
}
dlilogit (func, DLI98, p);
if (p != logbuf) {
*p = savechrs2[0];
*(p + 1) = savechrs2[1];
}
}
int ns1__listReplicas (struct soap *soap, char *type, char *data, struct ns1__listReplicasResponse *rep)
{
const char *clienthost;
char func[16];
char *guid = NULL;
int i = 0;
char *lfn = NULL;
char logbuf[CA_MAXPATHLEN+19];
int nbsurls = 0;
struct Cns_filestatg statbuf;
struct Cns_filereplica *rep_entries;
strcpy (func, "listReplicas");
clienthost = Cgetnetaddress (-1, &soap->peer, soap->peerlen, &na_key, NULL, NULL, 0, 0);
if (!clienthost) clienthost = "(unknown)";
dlilogit (func, "request from %s\n", clienthost);
if (! type) {
soap_sender_fault (soap, "InvalidInputDataType", NULL);
RETURN (SOAP_FAULT);
}
if (! data) {
soap_sender_fault (soap, "InputData", NULL);
RETURN (SOAP_FAULT);
}
if (strcmp (type, "lfn") == 0) {
lfn = data;
if (strncmp (lfn, "lfn:", 4) == 0) lfn += 4;
} else if (strcmp (type, "guid") == 0) {
guid = data;
if (strncmp (guid, "guid:", 5) == 0) guid += 5;
} else {
soap_sender_fault (soap, "InvalidInputDataType", NULL);
RETURN (SOAP_FAULT);
}
if ((lfn && strlen (lfn) > CA_MAXPATHLEN) ||
(guid && strlen (guid) > CA_MAXGUIDLEN)) {
soap_sender_fault (soap, "InputData", NULL);
RETURN (SOAP_FAULT);
}
sprintf (logbuf, "listReplicas %s:%s", type, lfn ? lfn : guid);
dli_logreq (func, logbuf);
serrno = 0;
if (lfc_statg (lfn, guid, &statbuf) < 0) {
if (serrno == SENOSHOST || serrno == SECOMERR)
soap_sender_fault (soap, "Catalog", NULL);
else
soap_sender_fault (soap, "InputData", NULL);
RETURN (SOAP_FAULT);
}
serrno = 0;
if (lfc_getreplica(lfn, guid, NULL, &nbsurls, &rep_entries) < 0) {
if (serrno == ENOMEM)
RETURN (SOAP_EOM);
if (serrno == SENOSHOST || serrno == SECOMERR)
soap_sender_fault (soap, "Catalog", NULL);
else
soap_sender_fault (soap, "NoURLFound", NULL);
RETURN (SOAP_FAULT);
}
if (nbsurls == 0) {
soap_sender_fault (soap, "NoURLFound", NULL);
RETURN (SOAP_FAULT);
}
if ((rep->urlList = soap_malloc (soap, sizeof(struct ArrayOfstring))) == NULL ||
(rep->urlList->__ptritem = soap_malloc (soap, nbsurls * sizeof(char *))) == NULL) {
free (rep_entries);
RETURN (SOAP_EOM);
}
for (i = 0; i < nbsurls; i++) {
if ((rep->urlList->__ptritem[i] = soap_strdup (soap, (rep_entries+i)->sfn)) == NULL) {
free(rep_entries);
RETURN (SOAP_EOM);
}
}
rep->urlList->__size = nbsurls;
free (rep_entries);
RETURN (SOAP_OK);
}
|