Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 1 | /* |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 2 | * Copyright (C) 2012-2013 ProFUSION embedded systems |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 3 | * |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 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. |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 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 |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 13 | * |
Lucas De Marchi | e1b1ab2 | 2012-07-10 09:42:24 -0300 | [diff] [blame] | 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 |
Lucas De Marchi | e701e38 | 2012-01-26 17:01:41 -0200 | [diff] [blame] | 17 | */ |
| 18 | |
Lucas De Marchi | 68cc449 | 2012-01-24 22:04:46 -0200 | [diff] [blame] | 19 | #include <errno.h> |
| 20 | #include <dlfcn.h> |
| 21 | #include <sys/utsname.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <stdio.h> |
| 25 | |
| 26 | #include "testsuite.h" |
| 27 | |
| 28 | TS_EXPORT int uname(struct utsname *u) |
| 29 | { |
| 30 | static void *nextlib = NULL; |
| 31 | static int (*nextlib_uname)(struct utsname *u); |
| 32 | const char *release = getenv(S_TC_UNAME_R); |
| 33 | int err; |
| 34 | size_t sz; |
| 35 | |
| 36 | if (release == NULL) { |
| 37 | fprintf(stderr, "TRAP uname(): missing export %s?\n", |
| 38 | S_TC_UNAME_R); |
| 39 | errno = EFAULT; |
| 40 | return -1; |
| 41 | } |
| 42 | |
| 43 | if (nextlib == NULL) { |
| 44 | #ifdef RTLD_NEXT |
| 45 | nextlib = RTLD_NEXT; |
| 46 | #else |
| 47 | nextlib = dlopen("libc.so.6", RTLD_LAZY); |
| 48 | #endif |
| 49 | nextlib_uname = dlsym(nextlib, "uname"); |
| 50 | } |
| 51 | |
| 52 | err = nextlib_uname(u); |
| 53 | if (err < 0) |
| 54 | return err; |
| 55 | |
| 56 | sz = strlen(release) + 1; |
| 57 | if (sz > sizeof(u->release)) { |
| 58 | fprintf(stderr, "uname(): sizeof release (%s) " |
Lucas De Marchi | d3f159b | 2012-01-30 13:29:17 -0200 | [diff] [blame] | 59 | "is greater than available space: %zu", |
Lucas De Marchi | 68cc449 | 2012-01-24 22:04:46 -0200 | [diff] [blame] | 60 | release, sizeof(u->release)); |
| 61 | errno = -EFAULT; |
| 62 | return -1; |
| 63 | } |
| 64 | |
| 65 | memcpy(u->release, release, sz); |
| 66 | return 0; |
| 67 | } |