Release new version 0.108
[grml-debootstrap.git] / packer / fake-uname.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <dlfcn.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9 #include <sys/syslog.h>
10 #include <sys/utsname.h>
11
12 #ifndef UTS_RELEASE
13 #define UTS_RELEASE "0.0.0"
14 #endif
15
16 #ifndef RTLD_NEXT
17 #define RTLD_NEXT      ((void *) -1l)
18 #endif
19
20 #define SYMBOL_EXPORT __attribute__((visibility("default")))
21
22 typedef int uname_func(struct utsname *buf);
23
24 static void *get_libc_func(const char *funcname)
25 {
26   void *func;
27   char *error;
28
29   /* Clear any previous errors. */
30   dlerror();
31   func = dlsym(RTLD_NEXT, funcname);
32   error = dlerror();
33   if (error != NULL) {
34     fprintf(stderr, "Cannot locate libc function '%s' error: %s",
35             funcname, error);
36     _exit(EXIT_FAILURE);
37   }
38   return func;
39 }
40
41 int SYMBOL_EXPORT uname(struct utsname *buf)
42 {
43   static uname_func *real_uname;
44   const char *release;
45   int ret;
46
47   if (real_uname == NULL)
48     real_uname = (uname_func *)get_libc_func("uname");
49
50   ret = real_uname(buf);
51   if (ret < 0)
52     return ret;
53
54   release = getenv("UTS_RELEASE");
55   if (release == NULL)
56     release = UTS_RELEASE;
57   strncpy(buf->release, release, sizeof(buf->release) - 1);
58   buf->release[sizeof(buf->release) - 1] = '\0';
59
60   return ret;
61 }