]> git.ipfire.org Git - thirdparty/kmod.git/blame - testsuite/uname.c
Reorder and reorganize header files
[thirdparty/kmod.git] / testsuite / uname.c
CommitLineData
e701e381 1/*
e6b0e49b 2 * Copyright (C) 2012-2013 ProFUSION embedded systems
e701e381 3 *
e1b1ab24
LDM
4 * This program 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.1 of the License, or (at your option) any later version.
e701e381
LDM
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e1b1ab24
LDM
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
e701e381 13 *
e1b1ab24
LDM
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 St, Fifth Floor, Boston, MA 02110-1301 USA
e701e381
LDM
17 */
18
68cc4493 19#include <dlfcn.h>
c2e4286b
LDM
20#include <errno.h>
21#include <stdio.h>
68cc4493
LDM
22#include <stdlib.h>
23#include <string.h>
632fb7b4 24#include <unistd.h>
c2e4286b 25#include <sys/utsname.h>
68cc4493
LDM
26
27#include "testsuite.h"
28
29TS_EXPORT int uname(struct utsname *u)
30{
31 static void *nextlib = NULL;
32 static int (*nextlib_uname)(struct utsname *u);
632fb7b4 33 const char *release;
68cc4493
LDM
34 int err;
35 size_t sz;
36
68cc4493
LDM
37 if (nextlib == NULL) {
38#ifdef RTLD_NEXT
39 nextlib = RTLD_NEXT;
40#else
41 nextlib = dlopen("libc.so.6", RTLD_LAZY);
42#endif
43 nextlib_uname = dlsym(nextlib, "uname");
44 }
45
46 err = nextlib_uname(u);
47 if (err < 0)
48 return err;
49
632fb7b4
MM
50 if (!environ)
51 /*
52 * probably called from within glibc before main(); unsafe
53 * to call getenv()
54 */
55 return 0;
56
57 release = getenv(S_TC_UNAME_R);
58 if (release == NULL) {
59 fprintf(stderr, "TRAP uname(): missing export %s?\n",
60 S_TC_UNAME_R);
61 return 0;
62 }
63
68cc4493
LDM
64 sz = strlen(release) + 1;
65 if (sz > sizeof(u->release)) {
66 fprintf(stderr, "uname(): sizeof release (%s) "
d3f159bb 67 "is greater than available space: %zu",
68cc4493
LDM
68 release, sizeof(u->release));
69 errno = -EFAULT;
70 return -1;
71 }
72
73 memcpy(u->release, release, sz);
74 return 0;
75}