File: logging.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 (550 lines) | stat: -rw-r--r-- 14,145 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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
 * $Id: logging.c,v 1.1 2005/03/31 13:13:00 baud Exp $
 */

/*
 * Copyright (C) 1993-1999 by CERN/IT/PDP/DM
 * All rights reserved
 */
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: logging.c,v $ $Revision: 1.1 $ $Date: 2005/03/31 13:13:00 $ CERN/IT/PDP/DM Fabien Collin" ;
#endif /* not lint */

/* logging.c : A facility for logging client I/O primitives 
 The logging filename is defined by RFIO/CLIENTLOG in shift.conf 
 If this variable is not defined, no client logging is done 
 To dump a client log, use dump_log */

#include "Castor_limits.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/times.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <malloc.h>
#include <syslog.h>
#include <sys/uio.h>
#include "logging.h"
#include <trace.h>

typedef struct {              /* One opened file */
  OPEN_BUFFER rec_open;
  char filename[CA_MAXPATHLEN+1];
  ERROR_BUFFER rec_error;
  RECORD_BUFFER rec_record;
  int record_table[MAX_RECORD_TABLE];
  OPERATION *rec_op;
  int cur_op;
  int last_op;
  time_t session_start_real_time;
  time_t session_end_real_time;
  clock_t session_start_user_time;
  clock_t session_start_sys_time;
  clock_t session_end_user_time;
  clock_t session_end_sys_time;
} OPENFILE;

#define UNUSED 99999

static int client_logging = FALSE ;    /* True if client logging is enabled */
static int lf;                /* File handle for logging file */
static int panic = FALSE;     /* Something wrong, stop logging */
static long incarnation_date; /* A unique number (48 bits) to identify an application */
static short incarnation_pid;
static int start = TRUE;      /* Code executed only once in an application */
static OPENFILE *file_table = NULL; /* The file table */
static int length_file_table = 0;   /* Its size */
static int *fd_table = NULL;        /* The fd table : fd_table[index in file table] = file fd */
                                    /* Same size as file_table */

char *getconfent();

void logging_panic(err)
     int err;
{
  panic = TRUE;

  switch(err)
    {
    case SYSLOG_OVERFLOW:
      syslog(LOG_ALERT, "rfio: client logging stopped : record table overflow");
      break;
    case SYSLOG_OPEN:
      syslog(LOG_ALERT, "rfio: client logging stopped : open error");
      break;
    case SYSLOG_WRONG_FD:
      syslog(LOG_ALERT, "rfio: client logging stopped : wrong fd");
      break;
    case SYSLOG_WRITE:
      syslog(LOG_ALERT, "rfio: client logging stopped : write error");
      break;
    case SYSLOG_CLOSE:
      syslog(LOG_ALERT, "rfio: client logging stopped : close error");
      break;
    case SYSLOG_MALLOC:
      syslog(LOG_ALERT, "rfio: client logging stopped : malloc error");
      break;
    }
}

/*
  Open and create (if necessary) the logging file
  See RFIO/CLIENTLOG variable in shift.conf
*/
static int open_logfile()
{
  int fd;
  static char *log_filename;
  INIT_TRACE("rfio");

  log_filename = getconfent("RFIO","CLIENTLOG",0);

  if ((fd = open(log_filename,O_WRONLY|O_APPEND|O_CREAT,0666)) == -1) {
    	TRACE(1,"log","Can't open file %s",log_filename);
	END_TRACE() ;
    	return(-1);
  }
  else {
	TRACE(1,"log", "file opened (fd=%d)",fd);
	fchmod(fd,0666);
	END_TRACE() ;
    	return(fd);
  }
}

/* 
  Find a record size in a record table
  Returns -1 if size not found
  Returns the index of the record size if found
*/
static int find_size(rt,l,size)
     int size;
     int rt[],l;
{
  int i;

  if (l >= MAX_RECORD_TABLE)
    {
      logging_panic(SYSLOG_OVERFLOW);
      return(-1);
    }

  if (l == 0)
    return(-1);
  else
    {
      for (i=0;i<l;i++)
	if (rt[i] == size)
	  return(i);
      return(-1);
    }
}

/*
  Insert a record size in a record table
*/
static int insert_size(rt,l,f,size)
     int size;
     int rt[],l,f;
{
  if (l >= MAX_RECORD_TABLE)
    {
      file_table[f].rec_error.num |= ERR_RECORD_FULL;
      return(-1);
    }
  else
    {
      file_table[f].record_table[l] = size;
      return(file_table[f].rec_record.length++);
    }
}

/* Find a file table index given its file descriptor */
static int find_fd(fd)
     int fd;
{
  int i;

  for (i=0;i<length_file_table;i++)
      if (fd_table[i] == fd)
	return(i);

  return(-1);
}

/* Client logging initialization : Executed only once in a client application */
void rfio_logst()
{
  client_logging = (getconfent("RFIO","CLIENTLOG",0) == NULL) ? FALSE : TRUE;

  if (client_logging)
    {
      /* Execute this only once */
      if (start)
	{
	  /* Open client logging file */
	  if ((lf = open_logfile()) == -1) {
	    logging_panic(SYSLOG_OPEN);
	  }

	  /* Incarnation = same number for several open of the same application */
	  incarnation_date = time(NULL);
	  incarnation_pid = (short) getpid();
	  start = FALSE;
	}
    }
}

/* Called when a file is closed */
void rfio_logcl(fd)
     int fd;
{
  struct iovec buf[8];
  int f = 0;
  short filelength;
  struct tms time_usage;

  if (client_logging) {
      if (!panic) {
	/* Find file index in file table */
	if ((f = find_fd(fd)) == -1) 
	  logging_panic(SYSLOG_WRONG_FD);

	if (!panic) {
	        /* The fd_table entry is now free */
		  fd_table[f] = UNUSED;
	
	      /* Compute different values related to execution time */
	      	  file_table[f].session_end_real_time = time(NULL);
	      	  times(&time_usage);
	          file_table[f].session_end_user_time = time_usage.tms_utime;
	          file_table[f].session_end_sys_time = time_usage.tms_stime;
	
	      	  file_table[f].rec_open.session_real_time = file_table[f].session_end_real_time - file_table[f].session_start_real_time;
	          file_table[f].rec_open.session_user_time = file_table[f].session_end_user_time - file_table[f].session_start_user_time;
	          file_table[f].rec_open.session_sys_time = file_table[f].session_end_sys_time - file_table[f].session_start_sys_time;
	
	      /* Write atomically the logging information in the logging file */
	
		  buf[0].iov_base = (caddr_t)&file_table[f].rec_open;
		  buf[0].iov_len = sizeof(OPEN_BUFFER);
		  
		  filelength = strlen(file_table[f].filename) + 1;
		  buf[1].iov_base = (caddr_t)&filelength;
		  buf[1].iov_len = sizeof(filelength);
	
		  buf[2].iov_base = (caddr_t)file_table[f].filename;
		  buf[2].iov_len = filelength;
	
		  buf[3].iov_base = (caddr_t)&file_table[f].rec_error;
		  buf[3].iov_len = sizeof(ERROR_BUFFER);
		  
		  buf[4].iov_base = (caddr_t)&file_table[f].rec_record;
		  buf[4].iov_len = sizeof(RECORD_BUFFER);
	
		  buf[5].iov_base = (caddr_t)file_table[f].record_table;
		  buf[5].iov_len = file_table[f].rec_record.length * sizeof(long);
		  
		  buf[6].iov_base = (caddr_t)&file_table[f].cur_op;
		  buf[6].iov_len = sizeof(int);
		  
		  buf[7].iov_base = (caddr_t)file_table[f].rec_op;
		  buf[7].iov_len = file_table[f].cur_op * sizeof(LSEEK_BUFFER);
		  
		  if (writev(lf,buf,8) == -1) {
		      perror("writev");
		      logging_panic(SYSLOG_WRITE); 
		  }
	/*
	 * We can't close the logging file as we don't know when to do it...
	 * We rely on exit to close all non-closed files in the client application
	 *
	 *  if (close(lf) < 0)
	 *  logging_panic(SYSLOG_CLOSE);
	 *      
	 *  if (!panic)  {
	 *  	free(fd_table);
	 *  	free(file_table);
	 *  }
	 */
	
     } /* if (!panic) */
   }  /* if (!panic) */
  }
}

/* Called when an open is done */
void rfio_logop(fd,path,machine,flags)
     int fd,flags;
     char *path;
     char *machine;
{
  long date;
  struct tms time_usage;

  if (client_logging)
    {
      if (!panic)
	{
	  /* One file more */
	  length_file_table++;
	  
	  /* Allocate a new file entry in the file table */
	  if (!file_table)
	    {
	      file_table = (OPENFILE *) malloc(sizeof(OPENFILE));
	      if (!file_table)
		logging_panic(SYSLOG_MALLOC);
	    }
	  else
	    {
	      file_table = (OPENFILE *) realloc(file_table,length_file_table * sizeof(OPENFILE));
	      if (!file_table)
		logging_panic(SYSLOG_MALLOC);
	    }
	  
	  if (!panic)
	    {
	      /* Allocate a new entry in the fd table */
	      if (!fd_table)
		{
		  fd_table = (int *) malloc(sizeof(int));
		  if (!fd_table)
		    logging_panic(SYSLOG_MALLOC);
		}
	      else
		{
		  fd_table = (int *) realloc(fd_table,length_file_table * sizeof(int));
		  if (!fd_table)
		    logging_panic(SYSLOG_MALLOC);
		}
	    }
	}
      
      if (!panic)
	{
	  int new = length_file_table - 1;
	  struct stat buf_stat;
	  
	  /* Put fd in fd table */
	  fd_table[new] = fd;

	  file_table[new].rec_open.command = LOG_OPEN;
	  file_table[new].rec_open.uid = getuid();
	    
	  if (machine == NULL)
	    file_table[new].rec_open.local = TRUE;
	  else
	    file_table[new].rec_open.local = FALSE;

	  strcpy(file_table[new].filename,path);

	  file_table[new].rec_open.flags = flags;

	  if (rfio_stat(path,&buf_stat) != -1)
	    file_table[new].rec_open.size = buf_stat.st_size;
	  else
	    file_table[new].rec_open.size = -1;
	  
	  if (!file_table[new].rec_open.local) 
	    {
	      strncpy(file_table[new].rec_open.machine,machine,8);
	      file_table[new].rec_open.machine[8]='\0' ;
	    }
	  else
	    file_table[new].rec_open.machine[0] = '\0';
	  
	  file_table[new].session_start_real_time = file_table[new].rec_open.date = time(NULL);
	  times(&time_usage);
	  file_table[new].session_start_user_time = time_usage.tms_utime;
	  file_table[new].session_start_sys_time = time_usage.tms_stime;
	  
	  file_table[new].rec_open.incarnation_date = incarnation_date;
	  file_table[new].rec_open.incarnation_pid = incarnation_pid;

	  
	  file_table[new].rec_error.command = LOG_ERRORS;
	  file_table[new].rec_error.num = 0;

	  file_table[new].rec_record.command = LOG_RECORD;
	  file_table[new].rec_record.length = 0;

	  file_table[new].rec_op = NULL;
	  file_table[new].cur_op = 0;
	  file_table[new].last_op = 0;
	  
	}
    }
}

#define EXTEND_OPERATION \
  if (!file_table[f].rec_op) \
    { \
      if ((file_table[f].rec_op = (OPERATION *) malloc(sizeof(OPERATION))) == NULL) \
	logging_panic(SYSLOG_MALLOC); \
    } \
  else \
    if ((file_table[f].rec_op = (OPERATION *) realloc(file_table[f].rec_op,(file_table[f].cur_op + 1) * sizeof(OPERATION))) == NULL) \
      logging_panic(SYSLOG_MALLOC)

/* Called when an lseek is done */
void rfio_logls(fd,offset,whence)
     off_t offset;
     int fd,whence;
{
  int f;

  if (client_logging)
    {
      if (!panic)
	if ((f = find_fd(fd)) == -1)
	  logging_panic(SYSLOG_WRONG_FD);

      if (!panic)
	{
	  EXTEND_OPERATION;
	  if (!panic)
	    {
	      switch(whence)
		{
		case SEEK_SET:
		  file_table[f].rec_op[file_table[f].cur_op].lseek.command = LOG_LSEEK_SET;
		  break;
		case SEEK_CUR:
		  file_table[f].rec_op[file_table[f].cur_op].lseek.command = LOG_LSEEK_CUR;
		  break;
		case SEEK_END:
		  file_table[f].rec_op[file_table[f].cur_op].lseek.command = LOG_LSEEK_END;
		  break;
		}
	      file_table[f].rec_op[file_table[f].cur_op].lseek.offset = offset;
	      
	      /* Last operation was a lseek */
	      file_table[f].last_op = LOG_LSEEK_SET;
	      
	      file_table[f].cur_op++;
	    }
	}
    }
}

/* Called when a read is done */
void rfio_logrd(fd,size)
     int fd,size;
{
  int record;
  int f;

  if (client_logging)
    {
      if (!panic)
	if ((f = find_fd(fd)) == -1)
	  logging_panic(SYSLOG_WRONG_FD);
  
      if (!panic)
	{
	  /* Find record size in record table */
	  if ((record = find_size(file_table[f].record_table,file_table[f].rec_record.length,size)) == -1)
	    record = insert_size(file_table[f].record_table,file_table[f].rec_record.length,f,size);
	  
	  /* Record table not full */
	  if (record != -1)
	    {
	      if (file_table[f].last_op != LOG_READ)
		{
		  /* Create a new READ operation */
		  EXTEND_OPERATION; 
		  if (!panic)
		    {
		      file_table[f].rec_op[file_table[f].cur_op].read.command = LOG_READ;
		      file_table[f].rec_op[file_table[f].cur_op].read.count = 1;
		      file_table[f].rec_op[file_table[f].cur_op++].read.record = record;
		    }
		}
	      else
		{
		  /* Last operation was a READ */
		  if (file_table[f].rec_op[file_table[f].cur_op - 1].read.record == record)
		    /* With the same record size */
		    file_table[f].rec_op[file_table[f].cur_op - 1].read.count++;
		  else
		    {
		      /* With a different record size */
		      EXTEND_OPERATION;
		      if (!panic)
			{
			  file_table[f].rec_op[file_table[f].cur_op].read.command = LOG_READ;
			  file_table[f].rec_op[file_table[f].cur_op].read.count = 1;
			  file_table[f].rec_op[file_table[f].cur_op++].read.record = record;
			}
		    }
		}
	    }
	  if (!panic)
	    file_table[f].last_op = LOG_READ;
	}
    }
}

/* Called when a write is done */
void rfio_logwr(fd,size)
     int fd,size;
{
  int record;
  int f;

  if (client_logging)
    {
      if (!panic)
	if ((f = find_fd(fd)) == -1)
	  logging_panic(SYSLOG_WRONG_FD);
      
      if (!panic)
	{
	  /* Find record size in record table */
	  if ((record = find_size(file_table[f].record_table,file_table[f].rec_record.length,size)) == -1)
	    record = insert_size(file_table[f].record_table,file_table[f].rec_record.length,f,size);
	  
	  /* Record table not full */
	  if (record != -1)
	    {
	      if (file_table[f].last_op != LOG_WRITE)
		{
		  /* Create a new WRITE operation */
		  EXTEND_OPERATION;
		  if (!panic)
		    {
		      file_table[f].rec_op[file_table[f].cur_op].write.command = LOG_WRITE;
		      file_table[f].rec_op[file_table[f].cur_op].write.count = 1;
		      file_table[f].rec_op[file_table[f].cur_op++].write.record = record;
		    }
		}
	      else
		{
		  /* Last operation was a WRITE */
		  if (file_table[f].rec_op[file_table[f].cur_op - 1].write.record == record)
		    /* With the same record size */
		    file_table[f].rec_op[file_table[f].cur_op - 1].write.count++;
		  else
		    {
		      /* With a different record size */
		      EXTEND_OPERATION;
		      if (!panic)
			{
			  file_table[f].rec_op[file_table[f].cur_op].write.command = LOG_WRITE;
			  file_table[f].rec_op[file_table[f].cur_op].write.count = 1;
			  file_table[f].rec_op[file_table[f].cur_op++].write.record = record;
			}
		    }
		}
	    }
	  if (!panic)
	    file_table[f].last_op = LOG_WRITE;
	}
    }
}