2 * Copyright (C) 2010 Red Hat Inc.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
36 #include "ignore-value.h"
39 #include "guestfs-internal.h"
40 #include "guestfs-internal-actions.h"
41 #include "guestfs_protocol.h"
43 #if defined(HAVE_PCRE) && defined(HAVE_LIBMAGIC)
45 static pcre *re_file_elf;
46 static pcre *re_elf_ppc64;
48 static void compile_regexps (void) __attribute__((constructor));
49 static void free_regexps (void) __attribute__((destructor));
52 compile_regexps (void)
57 #define COMPILE(re,pattern,options) \
59 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
61 ignore_value (write (2, err, strlen (err))); \
67 "ELF.*(?:executable|shared object|relocatable), (.+?),", 0);
68 COMPILE (re_elf_ppc64, "64.*PowerPC", 0);
74 pcre_free (re_file_elf);
75 pcre_free (re_elf_ppc64);
78 /* Convert output from 'file' command on ELF files to the canonical
79 * architecture string. Caller must free the result.
82 canonical_elf_arch (guestfs_h *g, const char *elf_arch)
86 if (strstr (elf_arch, "Intel 80386"))
88 else if (strstr (elf_arch, "Intel 80486"))
90 else if (strstr (elf_arch, "x86-64"))
92 else if (strstr (elf_arch, "AMD x86-64"))
94 else if (strstr (elf_arch, "SPARC32"))
96 else if (strstr (elf_arch, "SPARC V9"))
98 else if (strstr (elf_arch, "IA-64"))
100 else if (match (g, elf_arch, re_elf_ppc64))
102 else if (strstr (elf_arch, "PowerPC"))
107 char *ret = safe_strdup (g, r);
112 is_regular_file (const char *filename)
116 return lstat (filename, &statbuf) == 0 && S_ISREG (statbuf.st_mode);
119 /* Download and uncompress the cpio file to find binaries within.
121 * (1) Two lists must be identical.
122 * (2) Implicit limit of 31 bytes for length of each element (see code
125 #define INITRD_BINARIES1 "bin/ls bin/rm bin/modprobe sbin/modprobe bin/sh bin/bash bin/dash bin/nash"
126 #define INITRD_BINARIES2 {"bin/ls", "bin/rm", "bin/modprobe", "sbin/modprobe", "bin/sh", "bin/bash", "bin/dash", "bin/nash"}
129 cpio_arch (guestfs_h *g, const char *file, const char *path)
131 TMP_TEMPLATE_ON_STACK (dir);
132 #define dir_len (strlen (dir))
133 #define initrd_len (dir_len + 16)
134 char initrd[initrd_len];
135 #define cmd_len (dir_len + 256)
137 #define bin_len (dir_len + 32)
143 if (strstr (file, "gzip"))
145 else if (strstr (file, "bzip2"))
150 /* Security: Refuse to download initrd if it is huge. */
151 int64_t size = guestfs_filesize (g, path);
152 if (size == -1 || size > 100000000) {
153 error (g, _("size of %s unreasonable (%" PRIi64 " bytes)"),
158 if (mkdtemp (dir) == NULL) {
159 perrorf (g, "mkdtemp");
163 snprintf (initrd, initrd_len, "%s/initrd", dir);
164 if (guestfs_download (g, path, initrd) == -1)
167 snprintf (cmd, cmd_len,
168 "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
170 int r = system (cmd);
171 if (r == -1 || WEXITSTATUS (r) != 0) {
172 perrorf (g, "cpio command failed");
176 const char *bins[] = INITRD_BINARIES2;
178 for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
179 snprintf (bin, bin_len, "%s/%s", dir, bins[i]);
181 if (is_regular_file (bin)) {
182 int flags = g->verbose ? MAGIC_DEBUG : 0;
183 flags |= MAGIC_ERROR | MAGIC_RAW;
185 magic_t m = magic_open (flags);
187 perrorf (g, "magic_open");
191 if (magic_load (m, NULL) == -1) {
192 perrorf (g, "magic_load: default magic database file");
197 const char *line = magic_file (m, bin);
199 perrorf (g, "magic_file: %s", bin);
205 if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
206 ret = canonical_elf_arch (g, elf_arch);
214 error (g, "file_architecture: could not determine architecture of cpio archive");
217 /* Free up the temporary directory. Note the directory name cannot
218 * contain shell meta-characters because of the way it was
221 snprintf (cmd, cmd_len, "rm -rf %s", dir);
222 ignore_value (system (cmd));
232 guestfs__file_architecture (guestfs_h *g, const char *path)
235 char *elf_arch = NULL;
238 /* Get the output of the "file" command. Note that because this
239 * runs in the daemon, LANG=C so it's in English.
241 file = guestfs_file (g, path);
245 if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
246 ret = canonical_elf_arch (g, elf_arch);
247 else if (strstr (file, "PE32 executable"))
248 ret = safe_strdup (g, "i386");
249 else if (strstr (file, "PE32+ executable"))
250 ret = safe_strdup (g, "x86_64");
251 else if (strstr (file, "cpio archive"))
252 ret = cpio_arch (g, file, path);
254 error (g, "file_architecture: unknown architecture: %s", path);
258 return ret; /* caller frees */
261 #else /* no PCRE or libmagic at compile time */
263 /* XXX Should be an optgroup. */
265 #define NOT_IMPL(r) \
266 error (g, _("file-architecture API not available since this version of libguestfs was compiled without PCRE or libmagic libraries")); \
270 guestfs__file_architecture (guestfs_h *g, const char *path)
275 #endif /* no PCRE or libmagic at compile time */