1 This patch adds bindings to Linux syscalls for which glibc has symbols.
3 Using the FFI would have been nice, but that's not an option when using
4 a statically-linked Guile in an initrd that doesn't have libc.so around.
6 diff --git a/libguile/posix.c b/libguile/posix.c
9 @@ -2375,6 +2375,336 @@ scm_init_popen (void)
11 #endif /* HAVE_START_CHILD */
17 +#include <sys/mount.h>
18 +#include <sys/syscall.h>
20 +#include "libguile/foreign.h"
21 +#include "libguile/bytevectors.h"
22 +#include <libguile/variable.h>
24 +SCM_DEFINE (scm_mount, "mount", 3, 2, 0,
25 + (SCM source, SCM target, SCM type, SCM flags, SCM data),
26 + "Mount file system of @var{type} specified by @var{source} "
28 +#define FUNC_NAME s_scm_mount
31 + char *c_source, *c_target, *c_type, *c_data;
32 + unsigned long c_flags;
34 + c_source = scm_to_locale_string (source);
35 + c_target = scm_to_locale_string (target);
36 + c_type = scm_to_locale_string (type);
37 + c_flags = SCM_UNBNDP (flags) ? 0 : scm_to_ulong (flags);
39 + if (SCM_UNBNDP (data) || scm_is_false (data))
42 + c_data = scm_to_locale_string (data);
44 + err = mount (c_source, c_target, c_type, c_flags, c_data);
61 + return SCM_UNSPECIFIED;
65 +SCM_DEFINE (scm_umount, "umount", 1, 0, 0,
67 + "Unmount the file system on @var{target}.")
68 +#define FUNC_NAME s_scm_umount
73 + c_target = scm_to_locale_string (target);
75 + err = umount (c_target);
87 + return SCM_UNSPECIFIED;
91 +/* Linux's module installation syscall. See `kernel/module.c' in Linux;
92 + the function itself is part of the GNU libc.
94 + Load the LEN bytes at MODULE as a kernel module, with arguments from
95 + ARGS, a space-separated list of options. */
96 +extern long init_module (void *module, unsigned long len, const char *args);
98 +/* Load a kernel module from FD. FLAGS must be a bitwise or of
99 + MODULE_INIT_* constants. The GNU libc doesn't provide a wrapper for
100 + this one so we use 'syscall'. */
102 +finit_module (int fd, const char *args, int flags)
104 + return syscall (SYS_finit_module, fd, args, flags);
108 +SCM_DEFINE (scm_load_linux_module, "load-linux-module", 1, 1, 0,
109 + (SCM data, SCM options),
110 + "Load the Linux kernel module whose contents are in bytevector "
111 + "DATA (the contents of a @code{.ko} file), with the arguments "
112 + "from the OPTIONS string.")
113 +#define FUNC_NAME s_scm_load_linux_module
117 + unsigned long c_len;
120 + SCM_VALIDATE_BYTEVECTOR (SCM_ARG1, data);
122 + c_data = SCM_BYTEVECTOR_CONTENTS (data);
123 + c_len = SCM_BYTEVECTOR_LENGTH (data);
125 + scm_to_locale_string (SCM_UNBNDP (options) ? scm_nullstr : options);
127 + err = init_module (c_data, c_len, c_options);
134 + return SCM_UNSPECIFIED;
138 +SCM_DEFINE (scm_load_linux_module_fd, "load-linux-module/fd", 1, 2, 0,
139 + (SCM fd, SCM options, SCM flags),
140 + "Load the Linux kernel module from the file at FD, "
141 + "with the arguments from the OPTIONS string, and "
142 + "optionally the given FLAGS.")
143 +#define FUNC_NAME s_scm_load_linux_module_fd
149 + c_fd = scm_to_int (fd);
151 + scm_to_locale_string (SCM_UNBNDP (options) ? scm_nullstr : options);
152 + c_flags = SCM_UNBNDP (flags) ? 0 : scm_to_int (flags);
154 + err = finit_module (c_fd, c_options, c_flags);
161 + return SCM_UNSPECIFIED;
166 +/* Rebooting, halting, and all that. */
168 +#include <sys/reboot.h>
170 +SCM_VARIABLE_INIT (flag_RB_AUTOBOOT, "RB_AUTOBOOT",
171 + scm_from_int (RB_AUTOBOOT));
172 +SCM_VARIABLE_INIT (flag_RB_HALT_SYSTEM, "RB_HALT_SYSTEM",
173 + scm_from_int (RB_HALT_SYSTEM));
174 +SCM_VARIABLE_INIT (flag_RB_ENABLE_CAD, "RB_ENABLE_CAD",
175 + scm_from_int (RB_ENABLE_CAD));
176 +SCM_VARIABLE_INIT (flag_RB_DISABLE_CAD, "RB_DISABLE_CAD",
177 + scm_from_int (RB_DISABLE_CAD));
178 +SCM_VARIABLE_INIT (flag_RB_POWER_OFF, "RB_POWER_OFF",
179 + scm_from_int (RB_POWER_OFF));
180 +SCM_VARIABLE_INIT (flag_RB_SW_SUSPEND, "RB_SW_SUSPEND",
181 + scm_from_int (RB_SW_SUSPEND));
182 +SCM_VARIABLE_INIT (flag_RB_KEXEC, "RB_KEXEC",
183 + scm_from_int (RB_KEXEC));
185 +SCM_DEFINE (scm_reboot, "reboot", 0, 1, 0,
187 + "Reboot the system. @var{command} must be one of the @code{RB_} "
188 + "constants; if omitted, @var{RB_AUTOBOOT} is used, thus "
189 + "performing a hard reset.")
190 +#define FUNC_NAME s_scm_reboot
194 + if (SCM_UNBNDP (command))
195 + c_command = RB_AUTOBOOT;
197 + c_command = scm_to_int (command);
199 + reboot (c_command);
201 + return SCM_UNSPECIFIED; /* likely unreached */
205 +/* Linux network interfaces. See <linux/if.h>. */
207 +#include <linux/if.h>
208 +#include <linux/sockios.h>
209 +#include "libguile/socket.h"
211 +SCM_VARIABLE_INIT (flag_IFF_UP, "IFF_UP",
212 + scm_from_int (IFF_UP));
213 +SCM_VARIABLE_INIT (flag_IFF_BROADCAST, "IFF_BROADCAST",
214 + scm_from_int (IFF_BROADCAST));
215 +SCM_VARIABLE_INIT (flag_IFF_DEBUG, "IFF_DEBUG",
216 + scm_from_int (IFF_DEBUG));
217 +SCM_VARIABLE_INIT (flag_IFF_LOOPBACK, "IFF_LOOPBACK",
218 + scm_from_int (IFF_LOOPBACK));
219 +SCM_VARIABLE_INIT (flag_IFF_POINTOPOINT, "IFF_POINTOPOINT",
220 + scm_from_int (IFF_POINTOPOINT));
221 +SCM_VARIABLE_INIT (flag_IFF_NOTRAILERS, "IFF_NOTRAILERS",
222 + scm_from_int (IFF_NOTRAILERS));
223 +SCM_VARIABLE_INIT (flag_IFF_RUNNING, "IFF_RUNNING",
224 + scm_from_int (IFF_RUNNING));
225 +SCM_VARIABLE_INIT (flag_IFF_NOARP, "IFF_NOARP",
226 + scm_from_int (IFF_NOARP));
227 +SCM_VARIABLE_INIT (flag_IFF_PROMISC, "IFF_PROMISC",
228 + scm_from_int (IFF_PROMISC));
229 +SCM_VARIABLE_INIT (flag_IFF_ALLMULTI, "IFF_ALLMULTI",
230 + scm_from_int (IFF_ALLMULTI));
232 +SCM_DEFINE (scm_set_network_interface_address, "set-network-interface-address",
234 + (SCM socket, SCM name, SCM address),
235 + "Configure network interface @var{name}.")
236 +#define FUNC_NAME s_scm_set_network_interface_address
240 + struct sockaddr *c_address;
244 + socket = SCM_COERCE_OUTPORT (socket);
245 + SCM_VALIDATE_OPFPORT (1, socket);
246 + fd = SCM_FPORT_FDES (socket);
248 + memset (&ifr, 0, sizeof ifr);
249 + c_name = scm_to_locale_string (name);
250 + c_address = scm_to_sockaddr (address, &sa_len);
252 + strncpy (ifr.ifr_name, c_name, sizeof ifr.ifr_name - 1);
253 + memcpy (&ifr.ifr_addr, c_address, sa_len);
255 + err = ioctl (fd, SIOCSIFADDR, &ifr);
268 + return SCM_UNSPECIFIED;
272 +SCM_DEFINE (scm_set_network_interface_flags, "set-network-interface-flags",
274 + (SCM socket, SCM name, SCM flags),
275 + "Change the flags of network interface @var{name} to "
277 +#define FUNC_NAME s_scm_set_network_interface_flags
283 + socket = SCM_COERCE_OUTPORT (socket);
284 + SCM_VALIDATE_OPFPORT (1, socket);
285 + fd = SCM_FPORT_FDES (socket);
287 + memset (&ifr, 0, sizeof ifr);
288 + c_name = scm_to_locale_string (name);
289 + strncpy (ifr.ifr_name, c_name, sizeof ifr.ifr_name - 1);
290 + ifr.ifr_flags = scm_to_short (flags);
292 + err = ioctl (fd, SIOCSIFFLAGS, &ifr);
304 + return SCM_UNSPECIFIED;
308 +SCM_DEFINE (scm_network_interface_flags, "network-interface-flags",
310 + (SCM socket, SCM name),
311 + "Return the flags of network interface @var{name}.")
312 +#define FUNC_NAME s_scm_network_interface_flags
318 + socket = SCM_COERCE_OUTPORT (socket);
319 + SCM_VALIDATE_OPFPORT (1, socket);
320 + fd = SCM_FPORT_FDES (socket);
322 + memset (&ifr, 0, sizeof ifr);
323 + c_name = scm_to_locale_string (name);
324 + strncpy (ifr.ifr_name, c_name, sizeof ifr.ifr_name - 1);
326 + err = ioctl (fd, SIOCGIFFLAGS, &ifr);
338 + return scm_from_short (ifr.ifr_flags);