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 if (mkdtemp (dir) == NULL) {
151 perrorf (g, "mkdtemp");
155 snprintf (initrd, initrd_len, "%s/initrd", dir);
156 if (guestfs_download (g, path, initrd) == -1)
159 snprintf (cmd, cmd_len,
160 "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
162 int r = system (cmd);
163 if (r == -1 || WEXITSTATUS (r) != 0) {
164 perrorf (g, "cpio command failed");
168 const char *bins[] = INITRD_BINARIES2;
170 for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
171 snprintf (bin, bin_len, "%s/%s", dir, bins[i]);
173 if (is_regular_file (bin)) {
174 int flags = g->verbose ? MAGIC_DEBUG : 0;
175 flags |= MAGIC_ERROR | MAGIC_RAW;
177 magic_t m = magic_open (flags);
179 perrorf (g, "magic_open");
183 if (magic_load (m, NULL) == -1) {
184 perrorf (g, "magic_load: default magic database file");
189 const char *line = magic_file (m, bin);
191 perrorf (g, "magic_file: %s", bin);
197 if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
198 ret = canonical_elf_arch (g, elf_arch);
206 error (g, "file_architecture: could not determine architecture of cpio archive");
209 /* Free up the temporary directory. Note the directory name cannot
210 * contain shell meta-characters because of the way it was
213 snprintf (cmd, cmd_len, "rm -rf %s", dir);
214 ignore_value (system (cmd));
224 guestfs__file_architecture (guestfs_h *g, const char *path)
227 char *elf_arch = NULL;
230 /* Get the output of the "file" command. Note that because this
231 * runs in the daemon, LANG=C so it's in English.
233 file = guestfs_file (g, path);
237 if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
238 ret = canonical_elf_arch (g, elf_arch);
239 else if (strstr (file, "PE32 executable"))
240 ret = safe_strdup (g, "i386");
241 else if (strstr (file, "PE32+ executable"))
242 ret = safe_strdup (g, "x86_64");
243 else if (strstr (file, "cpio archive"))
244 ret = cpio_arch (g, file, path);
246 error (g, "file_architecture: unknown architecture: %s", path);
250 return ret; /* caller frees */
253 #else /* no PCRE or libmagic at compile time */
255 /* XXX Should be an optgroup. */
257 #define NOT_IMPL(r) \
258 error (g, _("file-architecture API not available since this version of libguestfs was compiled without PCRE or libmagic libraries")); \
262 guestfs__file_architecture (guestfs_h *g, const char *path)
267 #endif /* no PCRE or libmagic at compile time */