Add support for preloading lua modules. This is useful both when
providing lua bindings to applications based on librazor and when
producing static binaries using librazor (where otherwise the lua
POSIX library would need to be included as an additional dynamic
object).
2 * Copyright (C) 2008 Kristian Høgsberg <krh@redhat.com>
3 * Copyright (C) 2008 Red Hat, Inc
4 * Copyright (C) 2009 J. Ali Harlow <ali@juiblex.co.uk>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
41 #include "razor-internal.h"
47 #define RAZOR_ASCII_ISALPHA(c) \
48 ((c) >= 'A' && (c) <= 'Z' || (c) >= 'a' && (c) <= 'z')
50 /* Required by gnulib on non-libc platforms */
51 char *program_name = "librazor";
53 static int allow_all_root_names = 0;
56 * Primarily intended for testing named roots under UNIX platforms.
58 RAZOR_EXPORT void razor_disable_root_name_checks(int disable)
60 allow_all_root_names = disable;
63 static int razor_valid_root_name(const char *name)
65 if (allow_all_root_names)
66 return !strchr(name,'/');
69 return RAZOR_ASCII_ISALPHA(name[0]) && name[1] == ':' &&
72 return name[0] == '\0';
77 razor_create_dir(const char *root, const char *path)
79 char buffer[PATH_MAX], *p;
80 const char *slash, *next;
83 /* Create all sub-directories in dir. We know root exists and
84 * is a dir, root does not end in a '/', and path either has a
85 * leading '/' or (on MS-Windows only) root is the empty string
86 * and path starts with drive (eg., "c:/windows"). */
89 p = buffer + strlen(buffer);
91 for (slash = path; *slash != '\0'; slash = next) {
92 next = strchr(slash + 1, '/');
96 memcpy(p, slash, next - slash);
100 if (razor_valid_root_name(buffer))
103 if (stat(buffer, &buf) == 0) {
104 if (!S_ISDIR(buf.st_mode)) {
106 "%s exists but is not a directory\n",
110 } else if (mkdir(buffer, 0777) < 0) {
111 fprintf(stderr, "failed to make directory %s: %s\n",
112 buffer, strerror(errno));
116 /* FIXME: What to do about permissions for dirs we
117 * have to create but are not in the cpio archive? */
124 razor_write(int fd, const void *data, size_t size)
128 const unsigned char *p;
133 written = write(fd, p, rest);
135 perror("write error");
146 razor_file_get_contents(const char *filename, size_t *length)
156 fd = open(filename, O_RDONLY | O_BINARY);
160 if (fstat(fd, &st) < 0) {
165 *length = st.st_size;
167 addr = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
169 addr = malloc(st.st_size);
172 while(nb < st.st_size) {
173 res = read(fd, addr + nb, st.st_size - nb);
186 if (addr == MAP_FAILED)
194 razor_file_free_contents(void *addr, size_t length)
197 return munmap(addr, length);
204 struct qsort_context {
206 razor_compare_with_data_func_t compare;
211 qsort_swap(void *p1, void *p2, size_t size)
215 memcpy(buffer, p1, size);
216 memcpy(p1, p2, size);
217 memcpy(p2, buffer, size);
221 __qsort_with_data(void *base, size_t nelem, uint32_t *map,
222 struct qsort_context *ctx)
224 void *p, *start, *end, *pivot;
225 uint32_t *mp, *mstart, *mend, tmp;
226 int left, right, result;
227 size_t size = ctx->size;
231 end = base + nelem * size;
235 pivot = base + (rand() % nelem) * size;
238 result = ctx->compare(p, pivot, ctx->data);
240 qsort_swap(p, start, size);
250 } else if (result == 0) {
256 qsort_swap(p, end, size);
265 left = (start - base) / size;
266 right = (base + nelem * size - end) / size;
268 __qsort_with_data(base, left, map, ctx);
270 __qsort_with_data(end, right, mend, ctx);
274 razor_qsort_with_data(void *base, size_t nelem, size_t size,
275 razor_compare_with_data_func_t compare, void *data)
277 struct qsort_context ctx;
285 ctx.compare = compare;
288 map = malloc(nelem * sizeof (uint32_t));
289 for (i = 0; i < nelem; i++)
292 __qsort_with_data(base, nelem, map, &ctx);