File: xyopen.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 (435 lines) | stat: -rw-r--r-- 12,464 bytes parent folder | download | duplicates (8)
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
/*
 * $Id: xyopen.c 3172 2010-02-15 08:51:54Z baud $
 */

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

#ifndef lint
static char sccsid[] = "@(#)$RCSfile: xyopen.c,v $ $Revision: 3172 $ $Date: 2010-02-15 09:51:54 +0100 (Mon, 15 Feb 2010) $ CERN/IT/PDP/DM Frederic Hemmer, F. Hassine";
#endif /* not lint */

/* xyopen.c     Remote File I/O - Open a Fortran Logical Unit           */

/*
 * C bindings :
 *
 * rfio_xyopen(char *filename, char *host, int lun, int lrecl,
 *            char *chopt, int *irc)
 *
 * FORTRAN bindings :
 *
 * XYOPEN(INTEGER*4 LUN, INTEGER*4 LRECL, CHARACTER*(*) CHOPT, INTEGER*4 IRC)
 * XYOPN(CHARACTER*(*) FILENAME, CHARACTER*(*)HOST, INTEGER*4 LUN,
 *       INTEGER*4 LRECL, CHARACTER*(*) CHOPT, INTEGER*4 IRC)
 */

/*
 * System remote file I/O definitions
 */
#define RFIO_KERNEL     1  
#include "rfio.h" 
#if defined(_WIN32)
#define MAXHOSTNAMELEN 64
#else 
#include <sys/param.h>
#endif
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
#endif
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <Cpwd.h>

RFILE DLL_DECL *ftnlun[MAXFTNLUN];       /* Fortran logical units       */

extern char *getacct();
extern int DLL_DECL switch_open();

int DLL_DECL rfio_xysock(lun) 
int lun ;
{
   if ( ftnlun[lun] == NULL ) 
      return -1 ;
   else
      return ftnlun[lun]->s ;
}
		 
int DLL_DECL rfio_xyopen(name,node,lun,lrecl,chopt,irc)
char    *name, *node, *chopt;
int     lun, lrecl;
int     *irc;
{
   char rh[1];
   rh[0]='\0';
   return( rfio_xyopen_ext(name,node,lun,lrecl,chopt,irc,(uid_t)0,(gid_t)0,0,rh) );
}

int
rfio_xyopen_ext(name,node,lun,lrecl,chopt,irc,uid,gid,key,reqhost)
char    *name, *node, *chopt;
int     lun, lrecl;
int     *irc;
uid_t	uid;
gid_t	gid;
int	key;
char *reqhost;
{
   register int    s;              /* socket descriptor            */
   int             status;         /* remote fopen() status        */
   int     len;                    /* total string length          */
   int     i;                      /* general purpose index        */
   char    * p ;                   /* buffer pointer               */
   WORD    access, format;         /* Fortran file characteristics */
   LONG    openopt;                /* Fortran file open options    */
   LONG 	append ;
   LONG    trunc  ;                /* Trunc mode			*/
   int 	filen  ;
   RFILE 	*fd    ;                /* remote file desciptor        */
   char    *host  ;                /* actual host to be used       */
   char    *filepath;              /* actual file path to be used  */
   char    *account;               /* account string               */
   char    localhost[MAXHOSTNAMELEN];
   int 	acc, parserc;
   WORD 	uid_ext;
   WORD	gid_ext;
   struct 	passwd *pw;
   int 	rt ;
   char     rfio_buf[BUFSIZ];

   INIT_TRACE("RFIO_TRACE");
   *irc = 0;
   TRACE(2,"--->","OPTIONS: %s , %s, %d,%d,%s,%d,%d,%d,%d,%s",name,node,lun,lrecl,chopt,*irc,uid,gid,key,reqhost);
/*
 * First allocate a remote file descriptor
 */
   if ((fd = (RFILE *)malloc(sizeof(RFILE))) == NULL)        {
      END_TRACE();
      return(errno);
   }

/*
 * Analyze options
 */

   access = FFFACC_S;              /* Default is access SEQUENTIAL */
   format = FFFFORM_U;             /* Default is unformatted       */
   openopt = FFOOPT_0;             /* Default is no options        */
   for (i=0;i< (int)strlen(chopt);i++)   {
      switch (chopt[i])       {
       case 'u':
       case 'U':
	  format = FFFFORM_U; break;
       case 'f':
       case 'F':
	  free((char *)fd);
	  END_TRACE();
	  return(SEBADFFORM);
       case 's':
       case 'S':
	  access = FFFACC_S; break;
       case 'd':
       case 'D':
	  access = FFFACC_D; break;
       case 'a':
       case 'A':
	  openopt |= FFOOPT_A;
	  break;
       case 'e':
       case 'E':
	  openopt |= FFOOPT_E;
	  break;
       case ' ': break;
       default :
	  free((char *)fd);
	  END_TRACE();
	  return(SEBADFOPT);
      }
   }
   if ((openopt & FFOOPT_A) && (access == FFFACC_D))       {
      free((char *)fd);
      END_TRACE();
      return(SEINCFOPT);
   }

   fd->unit = lun;
   fd->access = access;
   fd->format = format;
   fd->recl = lrecl;
   fd->blank = FFFBLNK_N;          /* currently unsupported        */
   fd->opnopt = openopt;
   fd->ftype = FFTYPE_F;
   fd->passwd = key ;


/*
 *  File name parsing
 */
   host = node;
   filepath = name;
   striptb(node);
   if (strcmp(node,"") == 0)       {
      /* empty node, so, it may be a remote file syntax       */
      if (strcmp(name, "") == 0)      {
	 /* empty node & name, it may be assigned        */
	 if ((name=lun2fn(lun)) == NULL)        {
	    free((char *)fd);
	    END_TRACE();
	    return(errno);
	 }
      }
   }

   if (gethostname(localhost, sizeof(localhost)) < 0)      {
      TRACE(2, "rfio", "gethostname () failed.");
      return(-1);
   }

   if  ( (strcmp(node,"") == 0) || (strcmp(localhost, node)==0 ) || (strcmp(node,"localhost") == 0))      {
      /*
       * The file is local 
       */
      if (!(parserc = rfio_parse(name,&host,&filepath))) {
		  strcpy( fd->host , "localhost" );
		  ftnlun[lun]=fd;
		  filen= strlen(filepath) ;
		  append = openopt & FFOOPT_A;
		  trunc = openopt & FFOOPT_T ;
		  acc=(int)access;
		  *irc=switch_open(&acc,&lun,filepath, &filen, &lrecl, &append,&trunc,LLM);
		  TRACE(2, "rfio", "rxyopen (local) : %d", *irc);
		  END_TRACE();
		  rfio_errno = 0;
		  return(*irc);
      } else {
		  if (parserc < 0) {
			  END_TRACE();
			  return(-1);
		  }
	  }
      TRACE(3, "rfio", "rfio_xyopen: name %s host %s filepath %s", name, host, filepath);
   }

   /*
    * The only user allowed to make requests with uid !=0 or gid !=0
    * is root.
    */
   if (uid!=0 || gid!=0) 
      /*
       * DIRECT ACCESS: user specifies target
       */
      fd->mapping=0;
   /* 
    * MAPPED ACCESS: user will be mapped
    */
   else
      fd->mapping=1;

   if ( (int)strlen(filepath) > MAXFILENAMSIZE)       {
      free((char *)fd);
      END_TRACE();
      return(SEFNAM2LONG);
   }
   strncpy( fd->host , host, RESHOSTNAMELEN );

   uid_ext=(WORD)uid;
   gid_ext=(WORD)gid;
/*
 * Initialize RFILE structure
 */
   fd->magic = RFIO_MAGIC;
   fd->s = -1;
   fd->uid = (uid_ext==0 ? geteuid(): uid_ext);
   fd->gid = (gid_ext==0 ? getegid(): gid_ext);
   (void) umask(fd->umask=umask(0));
   fd->bufsize = 0;
   fd->ftype = FFTYPE_F;
   fd->binary = 0;                 /* no translation needed        */
   fd->unit = lun;
   fd->access = access;
   fd->format = format;
   fd->recl = lrecl;
   fd->blank = FFFBLNK_N;          /* currently unsupported        */
   fd->opnopt = openopt;

   s = rfio_connect(host,&rt);
   if (s < 0)      {
      free((char *)fd);
      END_TRACE();
      if (serrno) return(serrno); else return(errno);
   }
   TRACE(2, "rfio", "rfio_xyopen: setsockopt(SOL_SOCKET, SO_KEEPALIVE)");
   status = 1 ;
   if (setsockopt(fd->s, SOL_SOCKET, SO_KEEPALIVE,(char *)&status , sizeof (int) ) == -1) 
      TRACE(2, "rfio" ,"rfio_xyopen: setsockopt(SO_KEEPALIVE) failed");
   if ( !rt && !fd->mapping ) {
      fd->uid=geteuid() ;
      fd->gid=getegid() ;
      TRACE(3,"rfio", "re-setting (uid,gid) to %d,%d",fd->uid,fd->gid) ;
      fd->mapping = 1 ;
   }

   fd->s = s;
   ftnlun[lun]=fd;

   if ( (pw = Cgetpwuid(geteuid()) ) == NULL ) {
      TRACE(2, "rfio" ,"rfio_open: Cgetpwuid() error %s",strerror(errno));
      free ((char *)fd);
      END_TRACE();
      return(errno);
   }

   /* if ((account = getacct()) == NULL) */ account = "";

   TRACE(3, "rfio", "rfio_xyopen: uid %d gid %d umask %o ftype %d user %s",
	 fd->uid, fd->gid, fd->umask, fd->ftype,pw->pw_name);
   TRACE(3, "rfio", "rfio_xyopen: %d lun %d format %d access %d lrecl",
	 fd->unit, fd->format, fd->access, fd->recl);
   TRACE(3, "rfio", "rfio_xyopen: account: %s", account);
   TRACE(3, "rfio", "rfio_xyopen: filepath: %s", filepath);
   len = 7*WORDSIZE + 4*LONGSIZE + strlen(account) + strlen(filepath) + strlen(pw->pw_name) + strlen(reqhost) + 4 ;
   p= rfio_buf ;
   marshall_WORD(p, B_RFIO_MAGIC) ;
   marshall_WORD(p, RQST_XYOPEN) ;
   marshall_LONG(p, len) ;
   p= rfio_buf + RQSTSIZE ;
   marshall_WORD(p, fd->uid) ;
   marshall_WORD(p, fd->gid) ;
   marshall_WORD(p, fd->umask) ;
   marshall_WORD(p, fd->ftype) ;
   marshall_LONG(p, fd->unit) ;
   marshall_WORD(p, fd->format) ;
   marshall_WORD(p, fd->access) ;
   marshall_LONG(p, fd->recl) ;
   marshall_LONG(p, fd->opnopt) ;
   marshall_STRING(p, account) ;
   marshall_STRING(p, filepath) ;
   marshall_STRING(p,pw->pw_name) ;
   marshall_STRING(p,reqhost);
   marshall_LONG(p, fd->passwd);
   marshall_WORD(p, fd->mapping);
   TRACE(3,"rfio","rfio_xyopen: sending %d bytes",RQSTSIZE+len) ;
   if (netwrite_timeout(s,rfio_buf,RQSTSIZE+len,RFIO_CTRL_TIMEOUT) != (RQSTSIZE+len)) {
      TRACE(3, "rfio", "rfio_xyopen: write(): ERROR occured (errno=%d)", errno);
      free((char *)fd); ftnlun[lun]=(RFILE *) NULL;
      return( (serrno ? serrno : errno) );
   }
   TRACE(3, "rfio", "rfio_xyopen: reading %d bytes", LONGSIZE);
   if (netread_timeout(s,rfio_buf,LONGSIZE,RFIO_CTRL_TIMEOUT) != LONGSIZE) {
      TRACE(3, "rfio", "rfio_xyopen: read(): ERROR occured (errno=%d)", errno);
      free((char *)fd); ftnlun[lun]=(RFILE *) NULL;
      return( (serrno ? serrno : errno) );
   }
   p= rfio_buf ;
   unmarshall_LONG(p, status);
   TRACE(2, "rfio", "rfio_xyopen: return %d ",status);
   if (status)     {
      close(s);
      free((char *)fd); ftnlun[lun]=(RFILE *) NULL;
      rfio_errno = status ;
   }
   *irc = status;
   TRACE(1, "rfio", "rfio_xyopen: status: %d, irc: %d",status,*irc);
   END_TRACE();
   return(status);
}

/*
 * Fortran wrapper
 */

#if (defined(hpux) && !defined(PPU)) || (defined(_AIX) && !defined(EXTNAME))
#define xyopen_		xyopen
#endif  /* hpux && !PPU || AIX && !EXTNAME */

#if defined(_WIN32)
void DLL_DECL _stdcall XYOPEN(flun, flrecl, fchopt, fchoptl, firc)
#else
void xyopen_(flun, flrecl, fchopt, firc, fchoptl)
#endif
int     *flun, *flrecl, *firc;
char    *fchopt;
int     fchoptl;
{
   char    *chopt;                 /* "C" character strings        */
   int     status;                 /* xyopen return status         */

   INIT_TRACE("RFIO_TRACE");       /* initialize trace if any      */

   /*
    * convert fortran arguments
    */
   if ((chopt = malloc(fchoptl+1)) == NULL)        {
      *firc = -errno;
      return;
   }
   strncpy(chopt, fchopt, fchoptl); chopt[fchoptl] = '\0';

   TRACE(1,"rfio","XYOPEN(%d,%d,%s,%d)",*flun,*flrecl,chopt,*firc);

   /* 
    * Here comes real code 
    */
   TRACE(1,"rfio","xyopen will return with code %d",*firc);
   status = rfio_xyopen("","",*flun,*flrecl,chopt,firc);
   TRACE(1, "rfio", "XYOPEN: status: %d, irc: %d",status,*firc);
   END_TRACE();
   if (status) *firc = -status;    /* system errors have precedence */
   free(chopt);
   return;
}

#if (defined(hpux) && !defined(PPU)) || (defined(_AIX) && !defined(EXTNAME))
#define xyopn_		xyopn
#endif  /* hpux && !PPU || AIX && !EXTNAME */

#if defined(_WIN32)
void DLL_DECL _stdcall XYOPN(fname, fnamel, fnode, fnodel, flun, flrecl, fchopt, fchoptl, firc)
#else
void xyopn_(fname, fnode, flun, flrecl, fchopt, firc, fnamel, fnodel, fchoptl)
#endif
int     *flun, *flrecl, *firc;
char    *fname, *fnode, *fchopt;
int     fnamel, fnodel, fchoptl;
{
   char    *name, *node, *chopt;   /* "C" character strings        */
   int     status;                 /* xyopn return status          */

   INIT_TRACE("RFIO_TRACE");       /* initialize trace if any      */

   /*
    * convert fortran arguments
    */
   if ((name = malloc((unsigned) fnamel+1)) == NULL) {
      *firc = -errno;
      return;
   }
   if ((node = malloc((unsigned) fnodel+1)) == NULL) {
      *firc = -errno;
      return;
   }
   if ((chopt = malloc((unsigned) fchoptl+1)) == NULL) {
      *firc = -errno;
      return;
   }

   strncpy(name, fname, fnamel); name[fnamel] = '\0';
   strncpy(node, fnode, fnodel); node[fnodel] = '\0';
   strncpy(chopt, fchopt, fchoptl); chopt[fchoptl] = '\0';
   striptb(name);
   striptb(node);
   striptb(chopt);
   TRACE(1,"rfio","XYOPN(%s, %s, %d, %d, %s, %d)",
	 name,node,*flun,*flrecl,chopt,*firc);

   status = rfio_xyopen(name,node,*flun,*flrecl,chopt,firc);
   TRACE(1, "rfio", "XYOPN: status: %d, irc: %d",status,*firc);
   END_TRACE();
   if (status) *firc = -status;    /* system errors have precedence */
   free(name); free(node); free(chopt);
   return;
}