daemon: debug segv correct use of dereferencing NULL.
[libguestfs.git] / tests / c-api / test-add-libvirt-dom.c
1 /* libguestfs
2  * Copyright (C) 2010 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 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25
26 #include "xgetcwd.h"
27
28 #include <libvirt/libvirt.h>
29 #include <libvirt/virterror.h>
30
31 #include "guestfs.h"
32
33 static void
34 make_test_xml (FILE *fp, const char *cwd)
35 {
36   fprintf (fp,
37            "<?xml version=\"1.0\"?>\n"
38            "<node>\n"
39            "  <domain type='test'>\n"
40            "    <name>guest</name>\n"
41            "    <os>\n"
42            "      <type>hvm</type>\n"
43            "      <boot dev='hd'/>\n"
44            "    </os>\n"
45            "    <memory>524288</memory>\n"
46            "    <devices>\n"
47            "      <disk type='file'>\n"
48            "        <source file='%s/test1.img'/>\n"
49            "        <target dev='hda'/>\n"
50            "      </disk>\n"
51            "      <disk type='file'>\n"
52            "        <driver name='qemu' type='raw'/>\n"
53            "        <source file='%s/test2.img'/>\n"
54            "        <target dev='hdb'/>\n"
55            "      </disk>\n"
56            "      <disk type='file'>\n"
57            "        <driver name='qemu' type='qcow2'/>\n"
58            "        <source file='%s/test3.img'/>\n"
59            "        <target dev='hdc'/>\n"
60            "      </disk>\n"
61            "    </devices>\n"
62            "  </domain>\n"
63            "</node>",
64            cwd, cwd, cwd);
65 }
66
67 int
68 main (int argc, char *argv[])
69 {
70   guestfs_h *g;
71   virConnectPtr conn;
72   virDomainPtr dom;
73   virErrorPtr err;
74   int r;
75   const char *test_xml;
76   char *cwd;
77   FILE *fp;
78   char libvirt_uri[1024];
79
80   cwd = xgetcwd ();
81
82   /* Create the libvirt XML and test images in the current directory. */
83   fp = fopen ("test.xml", "w");
84   if (fp == NULL) {
85     perror ("test.xml");
86     exit (EXIT_FAILURE);
87   }
88   make_test_xml (fp, cwd);
89   fclose (fp);
90
91   fp = fopen ("test1.img", "w");
92   if (fp == NULL) {
93     perror ("test1.img");
94     exit (EXIT_FAILURE);
95   }
96   fclose (fp);
97
98   fp = fopen ("test2.img", "w");
99   if (fp == NULL) {
100     perror ("test2.img");
101     exit (EXIT_FAILURE);
102   }
103   fclose (fp);
104
105   fp = fopen ("test3.img", "w");
106   if (fp == NULL) {
107     perror ("test3.img");
108     exit (EXIT_FAILURE);
109   }
110   fclose (fp);
111
112   /* Create the guestfs handle. */
113   g = guestfs_create ();
114   if (g == NULL) {
115     fprintf (stderr, "failed to create handle\n");
116     exit (EXIT_FAILURE);
117   }
118
119   /* Create the libvirt connection. */
120   snprintf (libvirt_uri, sizeof libvirt_uri, "test://%s/test.xml", cwd);
121   conn = virConnectOpenReadOnly (libvirt_uri);
122   if (!conn) {
123     err = virGetLastError ();
124     fprintf (stderr, "could not connect to libvirt (code %d, domain %d): %s\n",
125              err->code, err->domain, err->message);
126     exit (EXIT_FAILURE);
127   }
128
129   dom = virDomainLookupByName (conn, "guest");
130   if (!dom) {
131     err = virGetLastError ();
132     fprintf (stderr,
133              "no libvirt domain called '%s': %s\n", "guest", err->message);
134     exit (EXIT_FAILURE);
135   }
136
137   r = guestfs_add_libvirt_dom (g, dom,
138                                GUESTFS_ADD_LIBVIRT_DOM_READONLY, 1,
139                                -1);
140   if (r == -1)
141     exit (EXIT_FAILURE);
142
143   guestfs_close (g);
144
145   unlink ("test.xml");
146   unlink ("test1.img");
147   unlink ("test2.img");
148   unlink ("test3.img");
149
150   exit (EXIT_SUCCESS);
151 }