aboutsummaryrefslogtreecommitdiffstats
path: root/lib/runtime_tools/c_src/trace_file_drv.c
blob: 8863b0d6acec0b9ec08dd63c456bc600dc3641e8 (plain) (blame)
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
/*
 * %CopyrightBegin%
 * 
 * Copyright Ericsson AB 1999-2011. All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * %CopyrightEnd%
 */

/*
 * Purpose: Send trace messages to a file.
 */

#ifdef __WIN32__
#include <windows.h>
#endif
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef __WIN32__
#  include <io.h>
#  define write _write
#  define close _close
#  define unlink _unlink
#else
#  include <unistd.h>
#endif
#include <errno.h>
#include <sys/types.h>
#include <fcntl.h>

/*
 * Deduce MAXPATHLEN, which is the one to use in this file, 
 * from any available definition.
 */
#ifndef MAXPATHLEN
#  ifdef PATH_MAX /* Posix */
#    define MAXPATHLEN PATH_MAX
#  else
#    ifdef _POSIX_PATH_MAX /* Posix */
#      define MAXPATHLEN _POSIX_PATH_MAX
#    else
#      ifdef MAXPATH
#        define MAXPATHLEN MAXPATH
#      else
#        ifdef MAX_PATH
#          define MAXPATHLEN MAX_PATH
#        else
#          ifdef _MAX_PATH
#            define MAXPATHLEN _MAX_PATH
#         else
#            error Could not define MAXPATHLEN
#          endif
#        endif
#      endif
#    endif
#  endif
#endif



#ifdef DEBUG
#include <assert.h>
#define ASSERT(X) assert(X)
#else
#define ASSERT(X)
#endif



#include "erl_driver.h"



/*
** Protocol from driver:
** '\0' -> ok
** '\1' ++ String -> {error, Atom}
**
** Protocol when opening (arguments to start):
** ["w <WrapSize> <WrapCnt> <TailIndex> "] "n <Filename>"
** Where...
** <Filename>, a string ('\0' terminated):
**    The filename where the trace output is to be written.
** "w ...", if present orders a size limited wrapping log.
** <WrapSize>, an unsigned integer:
**    The size limit of each log file.
** <WrapCnt>, an unsigned integer:
**    The number of log files.
** <TailIndex>, an unsigned integer:
**    The (zero based) index of where to insert the filename
**    sequence count "a".."z","aa".."az","ba".."zz","aaa"...
**
** Port control messages handled:
** 'f' -> '\0' (ok) | '\1' ++ String (error) : Flush file.
**
** The package written to the file looks like this:
** +--+--------+-----------------------------------+
** |Op|Size NBO|Term in external format or empty   |
** +--+--------+-----------------------------------+
** Op, a char, for conformance with the IP driver:
**    0 = binary, 1 = drop
**    If Op is 1, then Size reflects the number of dropped messages. The 
**    op 1 is never used in this driver.
** Size, a 32 bit interger in network byte order:
**    Either the size of the binary term, or the number of packet's dropped.
** Term, an array of bytes:
**    An erlang term in the external format or simply empty if Op == 1, the
**    term is Size long.
*/ 

typedef int FILETYPE;

#define BUFFER_SIZE (BUFSIZ*8)

#define OP_BINARY 0
#define OP_DROP   1

/*
** State structures
*/

typedef struct trace_file_name {
    char name[MAXPATHLEN+1]; /* Incl. space for terminating '\0' */
    unsigned suffix;         /* Index of suffix start */
    unsigned tail;           /* Index of tail start */
    unsigned len;            /* Total length (strlen) */
    unsigned cnt;            /* Current file count 0 <= cnt <= n */
    unsigned n;              /* Number of files */
} TraceFileName;

typedef struct trace_file_wrap_data {
    TraceFileName cur;  /* Current trace file */
    TraceFileName del;  /* Next file to delete when wrapping */
    unsigned      size; /* File max size */
    int           cnt;  /* How many remains before starting to wrap */
    unsigned long time; /* Time to pass until starting to delete old files */
    unsigned      len;  /* Current file len */
} TraceFileWrapData;

typedef struct trace_file_data {
    FILETYPE fd;
    ErlDrvPort port;
    struct trace_file_data *next, *prev;
    TraceFileWrapData *wrap; /* == NULL => no wrap */
    int buff_siz;
    int buff_pos;
    unsigned char buff[1]; /* You guessed it, will be longer... */
} TraceFileData;

static TraceFileData *first_data; 

/*
** Interface routines
*/
static ErlDrvData trace_file_start(ErlDrvPort port, char *buff);
static void trace_file_stop(ErlDrvData handle);
static void trace_file_output(ErlDrvData handle, char *buff,
			      ErlDrvSizeT bufflen);
static void trace_file_finish(void);
static ErlDrvSSizeT trace_file_control(ErlDrvData handle,
				      unsigned int command, 
				      char* buff, ErlDrvSizeT count, 
				      char** res, ErlDrvSizeT res_size);
static void trace_file_timeout(ErlDrvData handle);

/*
** Internal routines
*/
static unsigned digits(unsigned n);
static void next_name(TraceFileName *tfn);
static void *my_alloc(size_t size);
static int my_write(TraceFileData *data, unsigned char *buff, int siz);
static int my_flush(TraceFileData *data);
static void put_be(unsigned n, unsigned char *s);
static void close_unlink_port(TraceFileData *data); 
static int wrap_file(TraceFileData *data);
#ifdef __WIN32__
static int win_open(char *path, int flags, int mask);
#define open win_open
#else
ErlDrvEntry *driver_init(void);
#endif

/*
** The driver struct
*/
ErlDrvEntry trace_file_driver_entry = {
    NULL,		   /* F_PTR init, N/A */
    trace_file_start,      /* L_PTR start, called when port is opened */
    trace_file_stop,       /* F_PTR stop, called when port is closed */
    trace_file_output,     /* F_PTR output, called when erlang has sent */
    NULL,                  /* F_PTR ready_input, called when input descriptor 
			      ready */
    NULL,                  /* F_PTR ready_output, called when output 
			      descriptor ready */
    "trace_file_drv",      /* char *driver_name, the argument to open_port */
    trace_file_finish,     /* F_PTR finish, called when unloaded */
    NULL,                  /* void * that is not used (BC) */
    trace_file_control,    /* F_PTR control, port_control callback */
    trace_file_timeout,    /* F_PTR timeout, driver_set_timer callback */
    NULL,                  /* F_PTR outputv, reserved */
    NULL, /* ready_async */
    NULL, /* flush */
    NULL, /* call */
    NULL, /* event */
    ERL_DRV_EXTENDED_MARKER,
    ERL_DRV_EXTENDED_MAJOR_VERSION,
    ERL_DRV_EXTENDED_MINOR_VERSION,
    0,
    NULL,
    NULL,
    NULL,
};

/*
** Driver initialization routine
*/
DRIVER_INIT(trace_file_drv)
{
    first_data = NULL;
    return &trace_file_driver_entry;
}

/*
** Driver interface routines
*/

/*
** Open a port
*/
static ErlDrvData trace_file_start(ErlDrvPort port, char *buff)
{
    unsigned size, cnt, time, tail, len;
    char *p;
    TraceFileData     *data;
    TraceFileWrapData *wrap;
    FILETYPE fd;
    int n, w;
    static const char name[] = "trace_file_drv";


#ifdef HARDDEBUG
    fprintf(stderr,"hello (%s)\r\n", buff);
#endif
    w = 0; /* Index of where sscanf gave up */
    size = 0; /* Warning elimination */
    cnt = 0;  /* -""- */
    time = 0;  /* -""- */
    tail = 0; /* -""- */
    n = sscanf(buff, "trace_file_drv %n w %u %u %u %u %n",
	       &w, &size, &cnt, &time, &tail, &w);

    if (w < sizeof(name) || (n != 0 && n != 4))
	return ERL_DRV_ERROR_BADARG;

    /* Search for "n <Filename>" in the rest of the string */
    p = buff + w;
    for (p = buff + w; *p == ' '; p++); /* Skip space (necessary?) */
    if (*p++ != 'n')
	return ERL_DRV_ERROR_BADARG;
    if (*p++ != ' ')
	return ERL_DRV_ERROR_BADARG;
    /* Here we are at the start of the filename; p */
    len = strlen(p);
    if (tail >= len)
	/* Tail must start within filename */
	return ERL_DRV_ERROR_BADARG;

    data = my_alloc(sizeof(TraceFileData) - 1 + BUFFER_SIZE);

    /* We have to check the length in case we are running on 
     * VxWorks since too long pathnames may cause bus errors
     * instead of error return from file operations.
     */
    if (n == 4) {
	/* Size limited wrapping log */
	unsigned d = digits(cnt); /* Nof digits in filename counter */
	if (len+d >= MAXPATHLEN) {
	    errno = ENAMETOOLONG; 
	    return ERL_DRV_ERROR_ERRNO;
	}
	wrap = my_alloc(sizeof(TraceFileWrapData));
	wrap->size = size;
	wrap->cnt = cnt;
	wrap->time = time;
	wrap->len = 0;
	strcpy(wrap->cur.name, p);
	wrap->cur.suffix = tail;
	wrap->cur.tail = tail;
	wrap->cur.len = len;
	wrap->cur.cnt = cnt;
	wrap->cur.n = cnt;
	next_name(&wrap->cur); /* Incr to suffix "0" */
	wrap->del = wrap->cur; /* Struct copy! */
	p = wrap->cur.name; /* Use new name for open */
    } else {
	/* Regular log */
	if (len >= MAXPATHLEN) {
	    errno = ENAMETOOLONG; 
	    return ERL_DRV_ERROR_ERRNO;
	}
	wrap = NULL;
    }

    if ((fd = open(p, O_WRONLY | O_TRUNC | O_CREAT
#ifdef O_BINARY
		   | O_BINARY
#endif
		   , 0777)) < 0) {
	int saved_errno = errno;
	if (wrap)
	    driver_free(wrap);
	driver_free(data);
	errno = saved_errno;
	return ERL_DRV_ERROR_ERRNO;
    } 

    data->fd = fd;
    data->port = port;
    data->buff_siz = BUFFER_SIZE;
    data->buff_pos = 0;
    data->wrap = wrap;

    if (first_data) {
	data->prev = first_data->prev;
	first_data->prev = data;
    } else
	data->prev = NULL;
    data->next = first_data;
    first_data = data;

    if (wrap && wrap->time > 0)
	driver_set_timer(port, wrap->time);

    return (ErlDrvData) data;
}


/*
** Close a port
*/
static void trace_file_stop(ErlDrvData handle)
{
    close_unlink_port((TraceFileData *) handle);
}

/*
** Data sent from erlang to port.
*/
static void trace_file_output(ErlDrvData handle, char *buff,
			      ErlDrvSizeT bufflen)
{
    int heavy = 0;
    TraceFileData *data = (TraceFileData *) handle;
    unsigned char b[5] = "";
    put_be((unsigned) bufflen, b + 1);
    switch (my_write(data, (unsigned char *) b, sizeof(b))) {
    case 1:
	heavy = !0;
    case 0:
	switch (my_write(data, (unsigned char *) buff, bufflen)) {
	case 1:
	    heavy = !0;
	case 0:
	    break;
	case -1:
	    driver_failure_posix(data->port, errno); /* XXX */
	    return;
	}
	break;
    case -1:
	driver_failure_posix(data->port, errno); /* XXX */
	return;
    }
    if (data->wrap) {
	TraceFileWrapData *wrap = data->wrap;
	/* Size limited wrapping log files */
	wrap->len += sizeof(b) + bufflen;
	if (wrap->time == 0 && wrap->len >= wrap->size) {
	    if (wrap_file(data) < 0) {
		driver_failure_posix(data->port, errno); /* XXX */
		return;
	    }
	    heavy = !0;
	}
    }
    if (heavy) {
	set_port_control_flags(data->port, PORT_CONTROL_FLAG_HEAVY);
    }
}

/*
** Control message from erlang, we handle $f, which is flush.
*/
static ErlDrvSSizeT trace_file_control(ErlDrvData handle,
				       unsigned int command, 
				       char* buff, ErlDrvSizeT count, 
				       char** res, ErlDrvSizeT res_size)
{
    if (command == 'f') {
	TraceFileData *data = (TraceFileData *) handle;
	if (my_flush(data) < 0) {
	    driver_failure_posix(data->port, errno); /* XXX */
	}
	if (res_size < 1) {
	    *res = my_alloc(1);
	}
	**res = '\0';
	return 1;
    } 
    return -1;
}

/*
** Timeout from driver_set_timer.
*/
static void trace_file_timeout(ErlDrvData handle) {
    TraceFileData *data = (TraceFileData *) handle;
    if (data->wrap) {
	if (wrap_file(data) < 0) {
	    driver_failure_posix(data->port, errno); /* XXX */
	    return;
	} else {
	    driver_set_timer(data->port, data->wrap->time);
	}
    }
}

/*
** Driver unloaded
*/
static void trace_file_finish(void)
{
    while (first_data != NULL) {
	close_unlink_port(first_data);
    }
}

/*
** Internal helpers
*/

/* Calculate needed number of digits in filename counter.
**/
static unsigned digits(unsigned n) {
    unsigned m, i;
    for (m = 10, i = 1;  n >= m;  i++, m *= 10) ;
    return i;
}

/*
** Increment filename.
**
** The filename counter counts "0"-"9","10"-"19"..."[n->n]","0"...,
** but also "","0" which is used for initialization.
*/
static void next_name(TraceFileName *n) {
    if (n->cnt >= n->n) {
	n->cnt = 0;
	/* Circular count from "[n->n]" to "0", or from "" to "0" */
	memmove(&n->name[n->suffix+1], 
		&n->name[n->tail], 
		n->len+1 - n->tail); /* Including '\0' */
	n->name[n->suffix] = '0';
	n->len -= n->tail - n->suffix - 1;
	n->tail = n->suffix + 1;
    } else {
	int i = n->tail;
	n->cnt++;
	do {
	    i--;
	    /* Increment from the end, 
	     * '0'..'1', carry propagate forward */
	    if (n->name[i] < '9') {
		n->name[i]++;
		return;
	    } else
		n->name[i] = '0';
	} while (i > n->suffix);
	/* Wrapped around from "99..99" to "00..00", 
	 * need one more character */
	memmove(&n->name[n->tail+1],
		&n->name[n->tail],
		n->len+1 - n->tail); /* Incl '\0' */
	n->name[n->tail++] = '0';
	n->name[n->suffix] = '1';
	n->len++;
    }
}

/*
** Yet another malloc wrapper
*/
static void *my_alloc(size_t size) 
{
    void *ret;
    if ((ret = (void *) driver_alloc(size)) == NULL) {
	/* May or may not work... */
	fprintf(stderr, "Could not allocate %d bytes of memory in %s.",
		(int) size, __FILE__);
	exit(1);
    }
    return ret;
}

/*
** A write wrapper that regards it as an error if not all data was written.
*/
static int do_write(FILETYPE fd, unsigned char *buff, int siz) {
    int w;
    while (1) {
	w = write(fd, buff, siz);
	if (w < 0 && errno == EINTR)
	    continue;
	else if (w != siz) {
	    if (w >= 0) {
		errno = ENOSPC;
	    }
	    return -1;
	}
	return siz;
    }
}

/*
** Returns 0 if write to cache, 1 i write to file, and -1 if write failed.
*/
static int my_write(TraceFileData *data, unsigned char *buff, int siz) 
{
    int wrote;

    if (data->buff_siz - data->buff_pos >= siz) {
	memcpy(data->buff + data->buff_pos, buff, siz);
	data->buff_pos += siz; 
	return 0;
    }
    
    wrote = data->buff_siz - data->buff_pos;
    memcpy(data->buff + data->buff_pos, buff, wrote);
    if (do_write(data->fd, data->buff, data->buff_siz) < 0) {
	return -1;
    }
    data->buff_pos = 0;
    if (siz - wrote >= data->buff_siz) {
	/* Write directly, no need to buffer... */
	if (do_write(data->fd, buff + wrote, siz - wrote) < 0) {
	    return -1;
	}
	return 1;
    } 
    memcpy(data->buff, buff + wrote, siz - wrote);
    data->buff_pos = siz - wrote;
    set_port_control_flags(data->port, PORT_CONTROL_FLAG_HEAVY);
    return 1;
}

/* 
** Returns negative if it failed to write.
 */
static int my_flush(TraceFileData *data)
{
    if (do_write(data->fd, data->buff, data->buff_pos) < 0) {
	return -1;
    }
    data->buff_pos = 0;
    return 0;
}

/*
** Write unsigned to buffer in big endian
*/
static void put_be(unsigned n, unsigned char *s)
{
    s[0] = n >> 24;
    s[1] = (n >> 16) & 0xFFFFU;
    s[2] = (n >> 8) & 0xFFFFU;
    s[3] = n & 0xFFFFU;
}

/*
** Wrapper that only closes non-negative filehandles
*/
static void do_close(FILETYPE fd) {
    if (fd != -1) {
	close(fd);
    }
}

/*
** Close the whole port and clean up
*/
static void close_unlink_port(TraceFileData *data) 
{
    my_flush(data);
    do_close(data->fd);

    if (data->next)
	data->next->prev = data->prev;
    if (data->prev)
	data->prev->next = data->next;
    else
	first_data = data->next;

    if (data->wrap)
	driver_free(data->wrap);
    driver_free(data);
}

/*
** Wrap to new file - close the current, open a new and
** perhaps delete a too old one
**
** Returns negative if something failed.
*/
static int wrap_file(TraceFileData *data) {
    if (my_flush(data) < 0) {
	int saved_errno = errno;
	close(data->fd);
	data->fd = -1;
	errno = saved_errno;
	return -1;
    }
    close(data->fd);
    data->fd = -1;
    data->buff_pos = 0;
    data->wrap->len = 0;
    /* Count down before starting to remove old files */
    if (data->wrap->cnt > 0)
	data->wrap->cnt--;
    if (data->wrap->cnt == 0) {
	/* Remove an old file */
	unlink(data->wrap->del.name);
	next_name(&data->wrap->del);
    }
    next_name(&data->wrap->cur);
try_open:
    data->fd = open(data->wrap->cur.name, O_WRONLY | O_TRUNC | O_CREAT
#ifdef O_BINARY
	      | O_BINARY
#endif
	      , 0777);
    if (data->fd < 0) {
	if (errno == EINTR)
	    goto try_open;
	data->fd = -1;
	return -1;
    }
    return 0;
}

#ifdef __WIN32__
static int win_open(char *path, int flags, int mask)
{
  DWORD access = 0;
  DWORD creation = 0;
  HANDLE fd;
  int ret;
  if (flags & O_WRONLY) {
    access =  GENERIC_WRITE;
  } else if (flags & O_RDONLY) {
    access = GENERIC_READ;
  } else {
    access = (GENERIC_READ | GENERIC_WRITE);
  } 
  
  if (flags & O_CREAT) {
    creation |= CREATE_ALWAYS;
  }  else {
     creation |= OPEN_ALWAYS;
  }

  fd = CreateFileA(path, access,  
		   FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 
		   NULL, creation, FILE_ATTRIBUTE_NORMAL, NULL);
  if (fd == INVALID_HANDLE_VALUE) {
    
    return -1;
  }
  
  if ((ret = _open_osfhandle((intptr_t)fd, (flags & O_RDONLY) ? O_RDONLY : 0))
      < 0) {
    CloseHandle(fd);
  }

  return ret;
}
#endif