summaryrefslogtreecommitdiff
path: root/qa/src/chkopenlog.c
blob: eb5c22c521dfdd73304b1b4de1a367dbd1253082 (plain)
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
/*
 * Copyright (c) 1997-2001 Silicon Graphics, Inc.  All Rights Reserved.
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pcp/pmapi.h>
#include <pcp/impl.h>

int
main(int argc, char *argv[])
{
    int		sts;
    FILE	*f;
    FILE	*fout;
    int		fd;
    int		nextfd;

    close(3);		/* some stdio versions start with this open */
    close(4);		/* some stdio versions start with this open */
    fd = atoi(argv[1]);
    if (fd == 1) fout = stdout;
    else if (fd == 2) fout = stderr;
    else fout = fopen("/tmp/chk.fout", "w");
    if (fout == NULL) {
	fprintf(stderr, "chkopenlog: botched open ... fd=%d\n", fd);
	sts = system("ls -l /tmp/chk.fout");
	exit(sts == 0 ? 1 : sts);
    }

    fprintf(fout, "This message on oldstream before __pmOpenLog() called\n");


#define whatis(f) (f == (stderr) ? " (stderr)" : (f == (stdout) ? " (stdout)" : (f == NULL ? " (NULL)" : "")))

    nextfd = open("/dev/null", 0);
    if (nextfd < 0) {
	fprintf(stderr, "chkopenlog: failed /dev/null open\n");
	exit(2);
    }
    fprintf(stderr, "Starting with oldstream%s fd=%d, nextfd=%d\n", whatis(fout), fileno(fout), nextfd);
    close(nextfd);

    nextfd = open("/dev/null", 0);
    f = __pmOpenLog("chkopenlog", argv[2], fout, &sts);
    fprintf(stderr, "__pmOpenLog -> sts=%d, log %s newstream%s fd=%d, nextfd=%d\n",
	sts, argv[2], whatis(f), f != NULL ? fileno(f) : -1, nextfd);
    if (f != NULL)
	fprintf(f, "[a helpful little message]\n");

    exit(0);
}