blob: 27f92d5b922c35a3b0a97edca5cf345dedadac96 [file] [log] [blame]
Lucas De Marchie701e382012-01-26 17:01:41 -02001/*
Lucas De Marchie6b0e492013-01-16 11:27:21 -02002 * Copyright (C) 2012-2013 ProFUSION embedded systems
Lucas De Marchie701e382012-01-26 17:01:41 -02003 *
Lucas De Marchie1b1ab22012-07-10 09:42:24 -03004 * 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.
Lucas De Marchie701e382012-01-26 17:01:41 -02008 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Lucas De Marchie1b1ab22012-07-10 09:42:24 -030011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
Lucas De Marchie701e382012-01-26 17:01:41 -020013 *
Lucas De Marchie1b1ab22012-07-10 09:42:24 -030014 * You should have received a copy of the GNU Lesser General Public
Lucas De Marchidea2dfe2014-12-25 23:32:03 -020015 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Lucas De Marchie701e382012-01-26 17:01:41 -020016 */
17
Lucas De Marchi68cc4492012-01-24 22:04:46 -020018#include <dlfcn.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030019#include <errno.h>
20#include <stdio.h>
Lucas De Marchi68cc4492012-01-24 22:04:46 -020021#include <stdlib.h>
22#include <string.h>
Michal Marek632fb7b2014-03-06 18:03:46 +010023#include <unistd.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030024#include <sys/utsname.h>
Lucas De Marchi68cc4492012-01-24 22:04:46 -020025
26#include "testsuite.h"
27
28TS_EXPORT int uname(struct utsname *u)
29{
30 static void *nextlib = NULL;
31 static int (*nextlib_uname)(struct utsname *u);
Michal Marek632fb7b2014-03-06 18:03:46 +010032 const char *release;
Lucas De Marchi68cc4492012-01-24 22:04:46 -020033 int err;
34 size_t sz;
35
Lucas De Marchi68cc4492012-01-24 22:04:46 -020036 if (nextlib == NULL) {
37#ifdef RTLD_NEXT
38 nextlib = RTLD_NEXT;
39#else
40 nextlib = dlopen("libc.so.6", RTLD_LAZY);
41#endif
42 nextlib_uname = dlsym(nextlib, "uname");
43 }
44
45 err = nextlib_uname(u);
46 if (err < 0)
47 return err;
48
Michal Marek632fb7b2014-03-06 18:03:46 +010049 if (!environ)
50 /*
51 * probably called from within glibc before main(); unsafe
52 * to call getenv()
53 */
54 return 0;
55
56 release = getenv(S_TC_UNAME_R);
57 if (release == NULL) {
58 fprintf(stderr, "TRAP uname(): missing export %s?\n",
59 S_TC_UNAME_R);
60 return 0;
61 }
62
Lucas De Marchi68cc4492012-01-24 22:04:46 -020063 sz = strlen(release) + 1;
64 if (sz > sizeof(u->release)) {
65 fprintf(stderr, "uname(): sizeof release (%s) "
Lucas De Marchid3f159b2012-01-30 13:29:17 -020066 "is greater than available space: %zu",
Lucas De Marchi68cc4492012-01-24 22:04:46 -020067 release, sizeof(u->release));
68 errno = -EFAULT;
69 return -1;
70 }
71
72 memcpy(u->release, release, sz);
73 return 0;
74}