The Pedigree Project  0.1
glue-musl.c
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 // From musl
21 #include <errno.h>
22 #include <stdio.h>
23 #include <bits/syscall.h>
24 #include <stdarg.h>
25 
26 // From the Pedigree source tree (syscall stubs). References musl errno.
27 #include <posix-syscall.h>
28 #include <posixSyscallNumbers.h>
29 #include <translate.h>
30 
31 #define STUBBED(which) do { \
32  char buf[32]; \
33  snprintf(buf, 32, "linux=%ld", which); \
34  syscall1(POSIX_STUBBED, (long)(buf)); \
35 } while(0)
36 
37 long pedigree_translate_syscall(long which, long a1, long a2, long a3, long a4,
38  long a5, long a6)
39 {
40  long pedigree_translation = posix_translate_syscall(which);
41  if (pedigree_translation == -1)
42  {
43  STUBBED(which);
44  return -ENOSYS;
45  }
46  else
47  {
48  long err = 0;
49  long r = syscall6_err(pedigree_translation, a1, a2, a3, a4, a5, a6, &err);
50  if (err)
51  {
52  return -err;
53  }
54  else
55  {
56  return r;
57  }
58  }
59 }
60 
61 // Normally implemented in assembly - brought in here to avoid having to
62 // replace the .c file
63 long __syscall(long which, long a1, long a2, long a3, long a4,
64  long a5, long a6)
65 {
66  return pedigree_translate_syscall(which, a1, a2, a3, a4, a5, a6);
67 }
68 
69 // Extension that provides write access to the kernel log.
70 int klog(int prio, const char *fmt, ...)
71 {
72  static char print_temp[1024];
73  va_list argptr;
74  va_start(argptr, fmt);
75  vsnprintf(print_temp, sizeof print_temp, fmt, argptr);
76  syscall2(POSIX_SYSLOG, (long) print_temp, prio);
77  va_end(argptr);
78 }