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
32 #include "ignore-value.h"
35 #include "guestfs-internal.h"
36 #include "guestfs-internal-actions.h"
37 #include "guestfs_protocol.h"
39 /* Compile all the regular expressions once when the shared library is
40 * loaded. PCRE is thread safe so we're supposedly OK here if
41 * multiple threads call into the libguestfs API functions below
44 static pcre *re_file_elf;
45 static pcre *re_file_win64;
46 static pcre *re_elf_ppc64;
48 static void compile_regexps (void) __attribute__((constructor));
50 compile_regexps (void)
55 #define COMPILE(re,pattern,options) \
57 re = pcre_compile ((pattern), (options), &err, &offset, NULL); \
59 ignore_value (write (2, err, strlen (err))); \
65 "ELF.*(?:executable|shared object|relocatable), (.+?),", 0);
66 COMPILE (re_elf_ppc64, "64.*PowerPC", 0);
69 /* Match a regular expression which contains no captures. Returns
70 * true if it matches or false if it doesn't.
73 match (guestfs_h *g, const char *str, const pcre *re)
75 size_t len = strlen (str);
78 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
79 if (r == PCRE_ERROR_NOMATCH)
82 /* Internal error -- should not happen. */
83 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
84 __FILE__, __func__, r, str);
91 /* Match a regular expression which contains exactly one capture. If
92 * the string matches, return the capture, otherwise return NULL. The
93 * caller must free the result.
96 match1 (guestfs_h *g, const char *str, const pcre *re)
98 size_t len = strlen (str);
101 r = pcre_exec (re, NULL, str, len, 0, 0, vec, sizeof vec / sizeof vec[0]);
102 if (r == PCRE_ERROR_NOMATCH)
105 /* Internal error -- should not happen. */
106 fprintf (stderr, "libguestfs: %s: %s: internal error: pcre_exec returned unexpected error code %d when matching against the string \"%s\"\n",
107 __FILE__, __func__, r, str);
111 return safe_strndup (g, &str[vec[2]], vec[3]-vec[2]);
114 /* Convert output from 'file' command on ELF files to the canonical
115 * architecture string. Caller must free the result.
118 canonical_elf_arch (guestfs_h *g, const char *elf_arch)
122 if (strstr (elf_arch, "Intel 80386"))
124 else if (strstr (elf_arch, "Intel 80486"))
126 else if (strstr (elf_arch, "x86-64"))
128 else if (strstr (elf_arch, "AMD x86-64"))
130 else if (strstr (elf_arch, "SPARC32"))
132 else if (strstr (elf_arch, "SPARC V9"))
134 else if (strstr (elf_arch, "IA-64"))
136 else if (match (g, elf_arch, re_elf_ppc64))
138 else if (strstr (elf_arch, "PowerPC"))
143 char *ret = safe_strdup (g, r);
148 is_regular_file (const char *filename)
152 return lstat (filename, &statbuf) == 0 && S_ISREG (statbuf.st_mode);
155 /* Download and uncompress the cpio file to find binaries within.
157 * (1) Two lists must be identical.
158 * (2) Implicit limit of 31 bytes for length of each element (see code
161 #define INITRD_BINARIES1 "bin/ls bin/rm bin/modprobe sbin/modprobe bin/sh bin/bash bin/dash bin/nash"
162 #define INITRD_BINARIES2 {"bin/ls", "bin/rm", "bin/modprobe", "sbin/modprobe", "bin/sh", "bin/bash", "bin/dash", "bin/nash"}
165 cpio_arch (guestfs_h *g, const char *file, const char *path)
170 if (strstr (file, "gzip"))
172 else if (strstr (file, "bzip2"))
177 char dir[] = "/tmp/initrd.XXXXXX";
178 #define dir_len (sizeof dir)
179 if (mkdtemp (dir) == NULL) {
180 perrorf (g, "mkdtemp");
184 char dir_initrd[dir_len + 16];
185 snprintf (dir_initrd, dir_len + 16, "%s/initrd", dir);
186 if (guestfs_download (g, path, dir_initrd) == -1)
189 char cmd[dir_len + 256];
190 snprintf (cmd, dir_len + 256,
191 "cd %s && %s initrd | cpio --quiet -id " INITRD_BINARIES1,
193 int r = system (cmd);
194 if (r == -1 || WEXITSTATUS (r) != 0) {
195 perrorf (g, "cpio command failed");
199 char bin[dir_len + 32];
200 const char *bins[] = INITRD_BINARIES2;
202 for (i = 0; i < sizeof bins / sizeof bins[0]; ++i) {
203 snprintf (bin, dir_len + 32, "%s/%s", dir, bins[i]);
205 if (is_regular_file (bin)) {
206 int flags = g->verbose ? MAGIC_DEBUG : 0;
207 flags |= MAGIC_ERROR | MAGIC_RAW;
209 magic_t m = magic_open (flags);
211 perrorf (g, "magic_open");
215 if (magic_load (m, NULL) == -1) {
216 perrorf (g, "magic_load: default magic database file");
221 const char *line = magic_file (m, bin);
223 perrorf (g, "magic_file: %s", bin);
229 if ((elf_arch = match1 (g, line, re_file_elf)) != NULL) {
230 ret = canonical_elf_arch (g, elf_arch);
238 error (g, "file_architecture: could not determine architecture of cpio archive");
241 /* Free up the temporary directory. Note the directory name cannot
242 * contain shell meta-characters because of the way it was
245 snprintf (cmd, dir_len + 256, "rm -rf %s", dir);
246 ignore_value (system (cmd));
253 guestfs__file_architecture (guestfs_h *g, const char *path)
256 char *elf_arch = NULL;
259 /* Get the output of the "file" command. Note that because this
260 * runs in the daemon, LANG=C so it's in English.
262 file = guestfs_file (g, path);
266 if ((elf_arch = match1 (g, file, re_file_elf)) != NULL)
267 ret = canonical_elf_arch (g, elf_arch);
268 else if (strstr (file, "PE32 executable"))
269 ret = safe_strdup (g, "i386");
270 else if (strstr (file, "PE32+ executable"))
271 ret = safe_strdup (g, "x86_64");
272 else if (strstr (file, "cpio archive"))
273 ret = cpio_arch (g, file, path);
275 error (g, "file_architecture: unknown architecture: %s", path);
279 return ret; /* caller frees */