daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / tests / c-api / test-debug-to-file.c
1 /* libguestfs
2  * Copyright (C) 2011 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 /* Test that we can use the new event API to capture all debugging
20  * messages to a file.
21  */
22
23 #include <config.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "ignore-value.h"
31
32 #include "guestfs.h"
33
34 static void
35 debug_to_file (guestfs_h *g,
36                void *opaque,
37                uint64_t event,
38                int event_handle,
39                int flags,
40                const char *buf, size_t buf_len,
41                const uint64_t *array, size_t array_len)
42 {
43   FILE *fp = opaque;
44
45   ignore_value (fwrite (buf, 1, buf_len, fp));
46 }
47
48 int
49 main (int argc, char *argv[])
50 {
51   guestfs_h *g;
52   const char *filename = "test.log";
53   FILE *debugfp;
54
55   debugfp = fopen (filename, "w");
56   if (debugfp == NULL) {
57     perror (filename);
58     exit (EXIT_FAILURE);
59   }
60
61   g = guestfs_create ();
62   if (g == NULL) {
63     fprintf (stderr, "failed to create handle\n");
64     exit (EXIT_FAILURE);
65   }
66
67   if (guestfs_set_event_callback
68       (g, debug_to_file,
69        GUESTFS_EVENT_LIBRARY|GUESTFS_EVENT_APPLIANCE|GUESTFS_EVENT_TRACE,
70        0, debugfp) == -1)
71     exit (EXIT_FAILURE);
72
73   if (guestfs_set_verbose (g, 1) == -1)
74     exit (EXIT_FAILURE);
75
76   if (guestfs_set_trace (g, 1) == -1)
77     exit (EXIT_FAILURE);
78
79   if (guestfs_add_drive_opts (g, "/dev/null",
80                               GUESTFS_ADD_DRIVE_OPTS_FORMAT, "raw",
81                               GUESTFS_ADD_DRIVE_OPTS_READONLY, 1,
82                               -1) == -1)
83     exit (EXIT_FAILURE);
84
85   if (guestfs_launch (g) == -1)
86     exit (EXIT_FAILURE);
87
88   guestfs_close (g);
89
90   exit (EXIT_SUCCESS);
91 }