File: rfio_fcalls.c

package info (click to toggle)
lcgdm 1.10.0-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 17,532 kB
  • sloc: ansic: 226,887; sh: 13,562; perl: 11,575; python: 11,572; cpp: 5,716; sql: 1,824; makefile: 1,301; fortran: 113
file content (457 lines) | stat: -rw-r--r-- 13,903 bytes parent folder | download | duplicates (2)
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/*
 * $Id: rfio_fcalls.c 9755 2013-11-28 14:58:24Z dhsmith $
 */

/*
 * Copyright (C) 1994-2010 by CERN/IT/PDP/DM
 * All rights reserved
 */

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: rfio_fcalls.c,v $ $Revision: 9755 $ $Date: 2013-11-28 15:58:24 +0100 (Thu, 28 Nov 2013) $ CERN/IT/PDP/DM Felix Hassine" ;
#endif /* not lint */

/* rfio_fcalls.c        - Remote file I/O - server FORTRAN calls        */

#define DEBUG           0               /* Debugging flag               */
#define RFIO_KERNEL     1               /* KERNEL part of the programs  */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <Castor_limits.h>
#include "rfio.h"                       /* Remote file I/O              */
#include <log.h>                        /* Genralized error logger      */
#if defined(_WIN32)
#include "syslog.h"
#else
#include <sys/param.h>                  /* System parameters            */
#endif
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
#include <pwd.h>

/*
 * External declarations for error handling
 * and space allocation.
 */
extern char     *getconfent();
extern int      checkkey();         /* To check passwd provided     */

/*
 *External functions for RFIOs
 */
extern int	usf_open();
extern int	udf_open();
extern int	usf_write();
extern int	udf_write();
extern int	usf_read();
extern int 	udf_read();
extern int	uf_close();	
extern void	uf_cread();

#if defined(_WIN32)

#if !defined (MAX_THREADS)
#define MAX_THREADS 64
#endif /* MAX_THREADS */
#if !defined (RFIO_MAX_PORTS)
#define RFIO_MAX_PORTS 10
#endif /* RFIO_MAX_PORTS */

extern /*__declspec( thread ) */ DWORD tls_i;  
extern struct thData  {
  SOCKET ns;    /* control socket */
  struct sockaddr_storage from;
  int fromlen;
  int mode;
  int _is_remote;
  int fd;  
/* globals, which have to be local for thread... */
  char *rqstbuf;        /* Request buffer		*/
  char *filename;        /* file name            	*/  
  char *iobuffer; 		/* Data communication buffer    */
  int  iobufsiz;		/* Current io buffer size       */
  SOCKET data_s;     	/* Data listen socket (v3) */
  SOCKET data_sock;  	/* Data accept socket (v3) */
  SOCKET ctrl_sock;  	/* the control socket (v3) */
  int first_write;
  int first_read;
  int byte_read_from_network;
  struct rfiostat myinfo;
  char	from_host[MAXHOSTNAMELEN];
} *td;

#define rqstbuf td->rqstbuf
#define filename td->filename
#define iobuffer td->iobuffer
#define iobufsiz td->iobufsiz
#define data_s td->data_s
#define data_sock td->data_sock
#define ctrl_sock td->ctrl_sock
#define first_write td->first_write
#define first_read td->first_read
#define byte_read_from_network td->byte_read_from_network
#define is_remote td->_is_remote
#define myinfo td->myinfo

#else /* ! _WIN32 */
/*
 * Data buffer.
 */
static char     * iobuffer ;
static int      iobufsiz= 0;
/*
 * External declaration of request buffer.
 * ( Defined in rfio_calls.c ) 
 */
extern char rqstbuf[] ;   
#endif /* _WIN32 */

extern int switch_open();
extern int switch_close();
extern int switch_write();
extern int switch_read();
extern int srchkreqsize _PROTO((SOCKET, char *, int));

/************************************************************************/
/*                                                                      */
/*                              IO HANDLERS                             */
/*                                                                      */
/************************************************************************/

int   srxyopen(s, rlun,access, rt,host, bet)
int     s;
int     *rlun;
int     *access;
int     rt;
char    *host;
int 	bet ;

{
	char    *p ;				/* Msg buffer pointer	*/
	int	len ; 				/* Msg length		*/
	WORD    uid, gid, mask, ftype, format ;	/* Open flags		*/
	LONG    lun, lrecl, openopt ;		/* Open flags		*/
	int     append ;			/* Open flag		*/
	int 	trunc;				/* Open flag		*/
	char	account[MAXACCTSIZE] ;		/* client account 	*/
#if !defined(_WIN32)
	char	filename[MAXFILENAMSIZE] ; 	/* file name            */
#endif
	int	filen ;				/* file name length	*/
	int     status ;			/* Status		*/
        char    user[CA_MAXUSRNAMELEN+1];
        int     passwd,mapping;
	char    reqhost[MAXHOSTNAMELEN];
	int	sock ;

	*rlun= 0 ;
	p= rqstbuf + 2*WORDSIZE ;
	unmarshall_LONG(p,len) ;
   if ( (status = srchkreqsize(s,p,len)) == -1 ) {
     status = errno;
   } else {
     log(LOG_DEBUG,"rxyopen: betel protocol ? %s\n",(bet ? "yes":"no"));
     /*
      * Reading xyopen request.
      */
     log(LOG_DEBUG,"rxyopen: reading %d bytes\n",len) ;
     if ((status = netread_timeout(s,rqstbuf,len,RFIO_CTRL_TIMEOUT)) != len) {
       log(LOG_ERR,"rxyopen: read(): %s\n",strerror(errno));
       return -1 ;
     }
     p= rqstbuf ; 
     status = 0;
     *account = *filename = '\0';
     unmarshall_WORD(p, uid);
     unmarshall_WORD(p, gid);
     unmarshall_WORD(p, mask);
     unmarshall_WORD(p, ftype);
     unmarshall_LONG(p, lun);
     unmarshall_WORD(p, format);
     unmarshall_WORD(p, *access);
     unmarshall_LONG(p, lrecl);
     unmarshall_LONG(p, openopt);
     if ( (status == 0) &&
          (status = unmarshall_STRINGN(p, account, MAXACCTSIZE)) == -1 )
       status = E2BIG;
     if ( (status == 0) &&
          (status = unmarshall_STRINGN(p, filename, MAXFILENAMSIZE)) == -1 )
       status = SENAMETOOLONG;
     if (bet) {
       if ( (status == 0) &&
            (status = unmarshall_STRINGN(p, user, CA_MAXUSRNAMELEN+1)) == -1 )
         status = E2BIG;
       if ( (status == 0) &&
            (status = unmarshall_STRINGN(p, reqhost, MAXHOSTNAMELEN)) == -1 )
         status = E2BIG;
       unmarshall_LONG(p, passwd);
       unmarshall_WORD(p, mapping);
     }
     
     if (rt) {
       log(LOG_DEBUG, "Mapping: %s\n", (mapping ? "yes" : "no") );
       openlog("rfio",LOG_PID, LOG_USER) ;
       syslog(LOG_ALERT, "rfio: connection %s mapping by %s(%d,%d) from %s",(mapping ?"with" : "without"),user,uid,gid,host) ;
       closelog() ;
     }
     
     if (rt && !mapping) {
       log(LOG_DEBUG, "passwd: %d\n",passwd);
       log(LOG_DEBUG, "reqhost: %s\n",reqhost);
     }
     /*
      * Someone in the site has tried to specify (uid,gid) directly !
      */
     if (bet && !mapping && !rt) {
       log(LOG_INFO,"attempt to make non-mapped I/O and modify uid or gid !\n");
       status=EACCES ;
     }
     
     if ( bet )
       log(LOG_DEBUG,"Opening file %s for remote user: %s\n",filename,user);
     /*
      * Do not authorize any request to create file to
      * a user whose uid < 100
      */
     if ( uid < 100 ) {
       log(LOG_INFO,"attempt to start rfiod with (uid,gid)=(%d,%d) rejected\n",uid,gid);
       status=EACCES;
     }
     /*
      * MAPPED mode: user will be mapped to user "to"
      */
     if ( !status && rt && mapping )  {
       char to[100];
       int rcd,to_uid,to_gid ;
       
       if ( (rcd = get_user(host,user,uid,gid,to,&to_uid,&to_gid)) == -ENOENT ) {
         log(LOG_ERR,"get_user(): Error opening mapping file\n") ;
         status=EINVAL ;
       }
       
       if ( abs(rcd) == 1 ) {
         log(LOG_ERR,"No entry found in mapping file for (%s,%s,%d,%d)\n",
             host,user,uid,gid);
         status = EACCES;
       }
       else {
         log(LOG_DEBUG,"(%s,%s,%d,%d) mapped to %s(%d,%d)\n",
             host,user,uid,gid,to,to_uid,to_gid) ;
         uid = to_uid ;
         gid = to_gid ;
       }
     }
     /*
      * DIRECT access: the user specifies uid & gid by himself
      */
     if ( !status && rt && !mapping ) {
	     char *pr ;
	     if ( (pr= getconfent ("RTUSER","CHECK",0)) == NULL || !strcmp ( pr,"YES") ) { 
         log(LOG_INFO ,"Connecting %s for passwd check ...\n",reqhost);
         if ((sock=connecttpread(reqhost,passwd))>=0 && !checkkey(sock,passwd)) {
           status= EACCES;
           log(LOG_ERR,"ropen: DIRECT mapping : permission denied\n");
         }
         if (sock < 0) {
           status= EACCES ;
           log(LOG_ERR,"ropen: DIRECT mapping failed: Couldn't connect %s\n",reqhost);
         }
       }
	     else 
         log(LOG_INFO ,"Any DIRECT rfio request from out of site is authorized\n");
     }
     
     if ( !status ) {
       filen= strlen(filename) ; 
       log(LOG_DEBUG, "rxyopen: uid %d gid %d mask %o ftype %d \n",uid, gid, mask, ftype);
       log(LOG_DEBUG, "rxyopen: lun %d format %d access %d lrecl %d openopt %x\n",
           lun, format, *access, lrecl, openopt);
       log(LOG_DEBUG, "rxyopen: account: %s\n", account);
       log(LOG_DEBUG, "rxyopen: filename: %s\n", filename);
       log(LOG_INFO, "rxyopen(%s) for (%d,%d)\n",filename,uid,gid);
       *rlun = lun;
       (void) umask(mask);
       append = openopt & FFOOPT_A;
       trunc = openopt & FFOOPT_T;
#if !defined(_WIN32)
       (void) setgroups(0, NULL);
       if (gid == -1 || uid == -1) {
         status = EACCES;
         log(LOG_ERR,"rxyopen: denied\n");
       } else if ((setgid(gid)<0) || (setuid(uid)<0))  {
         status = errno;
         log(LOG_ERR, "rxyopen: unable to setuid,gid(%d,%d): %s\n", uid, gid, strerror(errno));
       }
       else
#endif	     
       {
         status=switch_open(access, &lun, filename, &filen, &lrecl, (LONG *)&append,(LONG *)&trunc,LLTM);
         log(LOG_DEBUG, "rxyopen: %d\n", status);
       }
     }
   }
   
   p= rqstbuf;
   marshall_LONG(p, status);
   log(LOG_DEBUG, "rxyopen: sending back %d\n", status);
   if (netwrite_timeout(s, rqstbuf, LONGSIZE, RFIO_CTRL_TIMEOUT) != LONGSIZE)  {
     log(LOG_ERR, "rxyopen: write(): %s\n", strerror(errno));
     return(-1);
   }
   log(LOG_INFO, "rxyopen(%s): status is: %d\n",filename,status);
   return(status);
}

int   srxyclos(s, infop, lun)
int     s;
int     lun;
struct rfiostat * infop ;
{
	int     status=0;
	char    *p ;
	int     irc;

	log(LOG_INFO, "%d read, %d readahead, %d write, %d flush, %d stat, %d lseek and %d preseek\n",
	infop->readop,infop->aheadop,infop->writop,infop->flusop,infop->statop,infop->seekop,infop->presop); 
	log(LOG_INFO, "%d bytes read and %d bytes written\n",infop->rnbr,infop->wnbr) ; 
	log(LOG_DEBUG, "rxyclos(%d)\n",lun);
	irc=switch_close(&lun);
	if (iobufsiz > 0)       {
		log(LOG_DEBUG, "rxyclos(): freeing %x\n",iobuffer);
		(void) free(iobuffer);
	}
	iobufsiz= 0;
	if (irc != 0){
		status = serrno ? serrno : errno;
	}
	p= rqstbuf ; 
	marshall_LONG(p,status) ;
	if (netwrite_timeout(s,rqstbuf,LONGSIZE,RFIO_CTRL_TIMEOUT) != LONGSIZE)  {
		log(LOG_ERR, "rxyclos: write(): %s\n", strerror(errno));
		return(-1);
	}
	log(LOG_INFO, "rxyclos(%d): status is: %d\n",lun,status);
	return(status);
}

int srxywrit(s, infop, lun, access)
int     s, lun, access ;
struct rfiostat * infop ;
{
	int     status=0, rcode=0;
	char    *ptr;
	char    *p ;
	int     optval;
	int     nrec, nwrit;

	p = rqstbuf + 2*WORDSIZE ;
	unmarshall_LONG(p, nrec);
	unmarshall_LONG(p, nwrit);
	log(LOG_DEBUG, "rxywrit(%d,%d): nrec %d nwrit %d\n",s,lun,nrec,nwrit) ;
	if (iobufsiz < nwrit)     {
		if (iobufsiz > 0)       {
			log(LOG_DEBUG, "rxywrit(): freeing %x\n",iobuffer);
			(void) free(iobuffer);
		}
		if ((iobuffer = malloc(nwrit)) == NULL)    {
			log(LOG_ERR, "rxywrit: malloc(): %s\n", strerror(errno));
			return(-1);
		}
		iobufsiz = nwrit;
		log(LOG_DEBUG, "rxywrit(): allocated %d bytes at %x\n",nwrit,iobuffer);
		optval = iobufsiz;
		if (setsockopt(s, SOL_SOCKET, SO_RCVBUF, (char *)&optval, sizeof(optval)) == -1)    {
			log(LOG_ERR, "rxywrit(): setsockopt(SO_RCVBUF): %s\n",strerror(errno));
		}
		log(LOG_DEBUG, "rxywrit(): setsockopt(SO_RCVBUF): %d\n",nwrit);
	}
	ptr = iobuffer;
	if (netread_timeout(s, ptr, nwrit, RFIO_DATA_TIMEOUT) != nwrit)       {
		log(LOG_ERR, "rxywrit: read(): %s\n", strerror(errno));
		return(-1);
	}
	status=switch_write(access,&lun,ptr,&nwrit,&nrec,LLTM);
	if (status != 0)        {
		rcode = errno;
	}
	else	{
		infop->wnbr+= nwrit ;
	}

	log(LOG_DEBUG, "rxywrit: status %d, rcode %d\n", status, rcode);
	p = rqstbuf;
	marshall_LONG(p, status);
	marshall_LONG(p, rcode);
	if (netwrite_timeout(s, rqstbuf, 2*LONGSIZE, RFIO_CTRL_TIMEOUT) != 2*LONGSIZE)  {
		log(LOG_ERR, "rxywrit: write(): %s\n", strerror(errno));
		return(-1);
	}
	return(status);
}


int srxyread(s, infop, lun, access)
int     s, lun;
struct rfiostat * infop ;
{
	int     status=0, rcode=0;
	char    *ptr;
	char    *p ;
	int     optval;
	int     nrec, nwant, ngot;
	int     readopt;

	p= rqstbuf + 2*WORDSIZE ; 
	unmarshall_LONG(p, readopt);
	unmarshall_LONG(p, nrec);
	unmarshall_LONG(p, nwant);
	log(LOG_DEBUG, "rxyread(%d,%d): readopt %x nrec %d nwant %d\n",s,lun,readopt,nrec,nwant) ;
	if (iobufsiz < nwant ) 	{
		if (iobufsiz > 0)       {
			log(LOG_DEBUG, "rxyread(): freeing %x\n",iobuffer);
			(void) free(iobuffer);
		}
		if ((iobuffer = malloc(nwant)) == NULL)    {
			log(LOG_ERR, "rxyread: malloc(): %s\n", strerror(errno));
			return(-1);
		}
		iobufsiz = nwant ;
		log(LOG_DEBUG, "rxyread(): allocated %d bytes at %x\n",nwant,iobuffer);
		optval = iobufsiz;
		if (setsockopt(s, SOL_SOCKET, SO_SNDBUF, (char *)&optval, sizeof(optval)) == -1)    {
			log(LOG_ERR, "rxyread(): setsockopt(SO_SNDBUF): %s\n",strerror(errno));
		}
		log(LOG_DEBUG, "rxyread(): setsockopt(SO_SNDBUF): %d\n",nwant);
	}
	ptr = iobuffer;
	serrno = 0 ;
	status = switch_read(access, &lun, ptr, &nwant, &nrec, readopt, &ngot,LLTM);  

	if (status != 0){
		rcode = serrno ? serrno : errno;
	}
	else	{
		infop->rnbr+= ngot ;
	}

	p = rqstbuf;
	marshall_LONG(p, status);
	marshall_LONG(p, rcode);
	marshall_LONG(p, ngot);
	if (netwrite_timeout(s, rqstbuf, 3*LONGSIZE, RFIO_CTRL_TIMEOUT) != (3*LONGSIZE)) {
		log(LOG_ERR, "rxyread: write(): %s\n", strerror(errno));
		return(-1);
	}
	if ( ngot ) {
		if (netwrite_timeout(s, ptr, ngot, RFIO_DATA_TIMEOUT) != ngot) {
			log(LOG_ERR, "rxyread: read(): %s\n", strerror(errno));
			return(-1);
		}
	}
	log(LOG_DEBUG, "rxyread: status %d, rcode %d\n", status, rcode);
	return status ;
}