linux: tidy up syscall code

This commit is contained in:
Ben Noordhuis 2012-03-31 00:11:34 +00:00
parent 4632163190
commit 685b36ba66
9 changed files with 360 additions and 219 deletions

View File

@ -63,7 +63,7 @@ EIO_CONFIG=config_linux.h
CSTDFLAG += -D_GNU_SOURCE
CPPFLAGS += -Isrc/ares/config_linux
LINKFLAGS+=-ldl -lrt
OBJS += src/unix/linux/core.o src/unix/linux/inotify.o
OBJS += src/unix/linux/core.o src/unix/linux/inotify.o src/unix/linux/syscalls.o
endif
ifeq (FreeBSD,$(uname_S))

View File

@ -767,9 +767,8 @@ int uv__accept(int sockfd, struct sockaddr* saddr, socklen_t slen) {
assert(sockfd >= 0);
while (1) {
#if HAVE_SYS_ACCEPT4
peerfd = sys_accept4(sockfd, saddr, &slen, SOCK_NONBLOCK | SOCK_CLOEXEC);
#if __linux__
peerfd = uv__accept4(sockfd, saddr, &slen, UV__SOCK_NONBLOCK|UV__SOCK_CLOEXEC);
if (peerfd != -1)
break;

View File

@ -497,23 +497,27 @@ int uv_fs_utime(uv_loop_t* loop, uv_fs_t* req, const char* path, double atime,
#if HAVE_FUTIMES
static int _futime(const uv_file file, double atime, double mtime) {
static int _futime(const uv_file fd, double atime, double mtime) {
#if __linux__
/* utimesat() has nanosecond resolution but we stick to microseconds
* for the sake of consistency with other platforms.
*/
struct timespec ts[2];
ts[0].tv_sec = atime;
ts[0].tv_nsec = (unsigned long)(atime * 1000000) % 1000000 * 1000;
ts[1].tv_sec = mtime;
ts[1].tv_nsec = (unsigned long)(mtime * 1000000) % 1000000 * 1000;
return uv__utimesat(fd, NULL, ts, 0);
#else
struct timeval tv[2];
/* FIXME possible loss of precision in floating-point arithmetic? */
tv[0].tv_sec = atime;
tv[0].tv_usec = (unsigned long)(atime * 1000000) % 1000000;
tv[1].tv_sec = mtime;
tv[1].tv_usec = (unsigned long)(mtime * 1000000) % 1000000;
#ifdef __sun
return futimesat(file, NULL, tv);
#else
return futimes(file, tv);
#endif
return futimes(fd, tv);
#endif /* __linux__ */
}
#endif
#endif /* HAVE_FUTIMES */
int uv_fs_futime(uv_loop_t* loop, uv_fs_t* req, uv_file file, double atime,

View File

@ -37,79 +37,9 @@
#undef HAVE_KQUEUE
#undef HAVE_PORTS_FS
#if defined(__linux__)
# undef HAVE_SYS_UTIMESAT
# undef HAVE_SYS_PIPE2
# undef HAVE_SYS_ACCEPT4
# undef _GNU_SOURCE
# define _GNU_SOURCE
# include <linux/version.h>
# include <sys/syscall.h>
# include <features.h>
# include <unistd.h>
# if __NR_utimensat
# define HAVE_SYS_UTIMESAT 1
# endif
# if __NR_pipe2
# define HAVE_SYS_PIPE2 1
# endif
# if __NR_accept4
# define HAVE_SYS_ACCEPT4 1
# endif
# ifndef O_CLOEXEC
# define O_CLOEXEC 02000000
# endif
# ifndef SOCK_CLOEXEC
# define SOCK_CLOEXEC O_CLOEXEC
# endif
# ifndef SOCK_NONBLOCK
# define SOCK_NONBLOCK O_NONBLOCK
# endif
# if HAVE_SYS_UTIMESAT
inline static int sys_utimesat(int dirfd,
const char* path,
const struct timespec times[2],
int flags)
{
return syscall(__NR_utimensat, dirfd, path, times, flags);
}
inline static int sys_futimes(int fd, const struct timeval times[2])
{
struct timespec ts[2];
ts[0].tv_sec = times[0].tv_sec, ts[0].tv_nsec = times[0].tv_usec * 1000;
ts[1].tv_sec = times[1].tv_sec, ts[1].tv_nsec = times[1].tv_usec * 1000;
return sys_utimesat(fd, NULL, ts, 0);
}
# undef HAVE_FUTIMES
# define HAVE_FUTIMES 1
# define futimes(fd, times) sys_futimes(fd, times)
# endif /* HAVE_SYS_FUTIMESAT */
# if HAVE_SYS_PIPE2
inline static int sys_pipe2(int pipefd[2], int flags)
{
return syscall(__NR_pipe2, pipefd, flags);
}
# endif /* HAVE_SYS_PIPE2 */
# if HAVE_SYS_ACCEPT4
inline static int sys_accept4(int fd,
struct sockaddr* addr,
socklen_t* addrlen,
int flags)
{
return syscall(__NR_accept4, fd, addr, addrlen, flags);
}
# endif /* HAVE_SYS_ACCEPT4 */
#if __linux__
# include "linux/syscalls.h"
# define HAVE_FUTIMES 1 /* emulated with utimesat() */
#endif /* __linux__ */
#if defined(__sun)
@ -118,6 +48,8 @@ inline static int sys_accept4(int fd,
# ifdef PORT_SOURCE_FILE
# define HAVE_PORTS_FS 1
# endif
# define HAVE_FUTIMES 1
# define futimes(fd, tv) futimesat(fd, (void*)0, tv)
#endif /* __sun */
#if defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun)

View File

@ -21,6 +21,7 @@
#include "uv.h"
#include "tree.h"
#include "../internal.h"
#include "syscalls.h"
#include <stdint.h>
#include <stdio.h>
@ -31,91 +32,6 @@
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#undef HAVE_INOTIFY_INIT
#undef HAVE_INOTIFY_INIT1
#undef HAVE_INOTIFY_ADD_WATCH
#undef HAVE_INOTIFY_RM_WATCH
#if __NR_inotify_init
# define HAVE_INOTIFY_INIT 1
#endif
#if __NR_inotify_init1
# define HAVE_INOTIFY_INIT1 1
#endif
#if __NR_inotify_add_watch
# define HAVE_INOTIFY_ADD_WATCH 1
#endif
#if __NR_inotify_rm_watch
# define HAVE_INOTIFY_RM_WATCH 1
#endif
#if HAVE_INOTIFY_INIT || HAVE_INOTIFY_INIT1
# undef IN_ACCESS
# undef IN_MODIFY
# undef IN_ATTRIB
# undef IN_CLOSE_WRITE
# undef IN_CLOSE_NOWRITE
# undef IN_OPEN
# undef IN_MOVED_FROM
# undef IN_MOVED_TO
# undef IN_CREATE
# undef IN_DELETE
# undef IN_DELETE_SELF
# undef IN_MOVE_SELF
# define IN_ACCESS 0x001
# define IN_MODIFY 0x002
# define IN_ATTRIB 0x004
# define IN_CLOSE_WRITE 0x008
# define IN_CLOSE_NOWRITE 0x010
# define IN_OPEN 0x020
# define IN_MOVED_FROM 0x040
# define IN_MOVED_TO 0x080
# define IN_CREATE 0x100
# define IN_DELETE 0x200
# define IN_DELETE_SELF 0x400
# define IN_MOVE_SELF 0x800
struct inotify_event {
int32_t wd;
uint32_t mask;
uint32_t cookie;
uint32_t len;
/* char name[0]; */
};
#endif /* HAVE_INOTIFY_INIT || HAVE_INOTIFY_INIT1 */
#undef IN_CLOEXEC
#undef IN_NONBLOCK
#if HAVE_INOTIFY_INIT1
# define IN_CLOEXEC O_CLOEXEC
# define IN_NONBLOCK O_NONBLOCK
#endif /* HAVE_INOTIFY_INIT1 */
#if HAVE_INOTIFY_INIT
inline static int inotify_init(void) {
return syscall(__NR_inotify_init);
}
#endif /* HAVE_INOTIFY_INIT */
#if HAVE_INOTIFY_INIT1
inline static int inotify_init1(int flags) {
return syscall(__NR_inotify_init1, flags);
}
#endif /* HAVE_INOTIFY_INIT1 */
#if HAVE_INOTIFY_ADD_WATCH
inline static int inotify_add_watch(int fd, const char* path, uint32_t mask) {
return syscall(__NR_inotify_add_watch, fd, path, mask);
}
#endif /* HAVE_INOTIFY_ADD_WATCH */
#if HAVE_INOTIFY_RM_WATCH
inline static int inotify_rm_watch(int fd, uint32_t wd) {
return syscall(__NR_inotify_rm_watch, fd, wd);
}
#endif /* HAVE_INOTIFY_RM_WATCH */
/* Don't look aghast, this is exactly how glibc's basename() works. */
@ -149,23 +65,19 @@ void uv__inotify_loop_delete(uv_loop_t* loop) {
}
#if HAVE_INOTIFY_INIT || HAVE_INOTIFY_INIT1
static void uv__inotify_read(EV_P_ ev_io* w, int revents);
static int new_inotify_fd(void) {
int fd;
#if HAVE_INOTIFY_INIT1
fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC);
fd = uv__inotify_init1(UV__IN_NONBLOCK | UV__IN_CLOEXEC);
if (fd != -1)
return fd;
if (errno != ENOSYS)
return -1;
#endif
if ((fd = inotify_init()) == -1)
if ((fd = uv__inotify_init()) == -1)
return -1;
if (uv__cloexec(fd, 1) || uv__nonblock(fd, 1)) {
@ -216,7 +128,7 @@ static void remove_watcher(uv_fs_event_t* handle) {
static void uv__inotify_read(EV_P_ ev_io* w, int revents) {
const struct inotify_event* e;
const struct uv__inotify_event* e;
uv_fs_event_t* handle;
uv_loop_t* uv_loop;
const char* filename;
@ -243,12 +155,12 @@ static void uv__inotify_read(EV_P_ ev_io* w, int revents) {
/* Now we have one or more inotify_event structs. */
for (p = buf; p < buf + size; p += sizeof(*e) + e->len) {
e = (const struct inotify_event*)p;
e = (const struct uv__inotify_event*)p;
events = 0;
if (e->mask & (IN_ATTRIB|IN_MODIFY))
if (e->mask & (UV__IN_ATTRIB|UV__IN_MODIFY))
events |= UV_CHANGE;
if (e->mask & ~(IN_ATTRIB|IN_MODIFY))
if (e->mask & ~(UV__IN_ATTRIB|UV__IN_MODIFY))
events |= UV_RENAME;
handle = find_watcher(uv_loop, e->wd);
@ -282,15 +194,15 @@ int uv_fs_event_init(uv_loop_t* loop,
if (init_inotify(loop)) return -1;
events = IN_ATTRIB
| IN_CREATE
| IN_MODIFY
| IN_DELETE
| IN_DELETE_SELF
| IN_MOVED_FROM
| IN_MOVED_TO;
events = UV__IN_ATTRIB
| UV__IN_CREATE
| UV__IN_MODIFY
| UV__IN_DELETE
| UV__IN_DELETE_SELF
| UV__IN_MOVED_FROM
| UV__IN_MOVED_TO;
wd = inotify_add_watch(loop->inotify_fd, filename, events);
wd = uv__inotify_add_watch(loop->inotify_fd, filename, events);
if (wd == -1) return uv__set_sys_error(loop, errno);
uv__handle_init(loop, (uv_handle_t*)handle, UV_FS_EVENT);
@ -304,29 +216,10 @@ int uv_fs_event_init(uv_loop_t* loop,
void uv__fs_event_destroy(uv_fs_event_t* handle) {
inotify_rm_watch(handle->loop->inotify_fd, handle->fd);
uv__inotify_rm_watch(handle->loop->inotify_fd, handle->fd);
remove_watcher(handle);
handle->fd = -1;
free(handle->filename);
handle->filename = NULL;
}
#else /* !HAVE_INOTIFY_INIT || HAVE_INOTIFY_INIT1 */
int uv_fs_event_init(uv_loop_t* loop,
uv_fs_event_t* handle,
const char* filename,
uv_fs_event_cb cb,
int flags) {
loop->counters.fs_event_init++;
uv__set_sys_error(loop, ENOSYS);
return -1;
}
void uv__fs_event_destroy(uv_fs_event_t* handle) {
UNREACHABLE();
}
#endif /* HAVE_INOTIFY_INIT || HAVE_INOTIFY_INIT1 */

230
src/unix/linux/syscalls.c Normal file
View File

@ -0,0 +1,230 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#include "syscalls.h"
#include <unistd.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <errno.h>
#if __i386__
# ifndef __NR_socketcall
# define __NR_socketcall 102
# endif
#endif
#if __arm__
# if __thumb__ || __ARM_EABI__
# define UV_SYSCALL_BASE 0
# else
# define UV_SYSCALL_BASE 0x900000
# endif
#endif /* __arm__ */
#ifndef __NR_accept4
# if __x86_64__
# define __NR_accept4 288
# elif __i386__
/* Nothing. Handled through socketcall(). */
# elif __arm__
# define __NR_accept4 (UV_SYSCALL_BASE + 366)
# endif
#endif /* __NR_accept4 */
#ifndef __NR_inotify_init
# if __x86_64__
# define __NR_inotify_init 253
# elif __i386__
# define __NR_inotify_init 291
# elif __arm__
# define __NR_inotify_init (UV_SYSCALL_BASE + 316)
# endif
#endif /* __NR_inotify_init */
#ifndef __NR_inotify_init1
# if __x86_64__
# define __NR_inotify_init1 294
# elif __i386__
# define __NR_inotify_init1 332
# elif __arm__
# define __NR_inotify_init1 (UV_SYSCALL_BASE + 360)
# endif
#endif /* __NR_inotify_init1 */
#ifndef __NR_inotify_add_watch
# if __x86_64__
# define __NR_inotify_add_watch 254
# elif __i386__
# define __NR_inotify_add_watch 292
# elif __arm__
# define __NR_inotify_add_watch (UV_SYSCALL_BASE + 317)
# endif
#endif /* __NR_inotify_add_watch */
#ifndef __NR_inotify_rm_watch
# if __x86_64__
# define __NR_inotify_rm_watch 255
# elif __i386__
# define __NR_inotify_rm_watch 293
# elif __arm__
# define __NR_inotify_rm_watch (UV_SYSCALL_BASE + 318)
# endif
#endif /* __NR_inotify_rm_watch */
#ifndef __NR_pipe2
# if __x86_64__
# define __NR_pipe2 293
# elif __i386__
# define __NR_pipe2 331
# elif __arm__
# define __NR_pipe2 (UV_SYSCALL_BASE + 359)
# endif
#endif /* __NR_pipe2 */
#ifndef __NR_recvmmsg
# if __x86_64__
# define __NR_recvmmsg 299
# elif __i386__
# define __NR_recvmmsg 337
# elif __arm__
# define __NR_recvmmsg (UV_SYSCALL_BASE + 365)
# endif
#endif /* __NR_recvmsg */
#ifndef __NR_sendmmsg
# if __x86_64__
# define __NR_sendmmsg 307
# elif __i386__
# define __NR_sendmmsg 345
# elif __arm__
# define __NR_recvmmsg (UV_SYSCALL_BASE + 374)
# endif
#endif /* __NR_sendmmsg */
#ifndef __NR_utimensat
# if __x86_64__
# define __NR_utimensat 280
# elif __i386__
# define __NR_utimensat 320
# elif __arm__
# define __NR_utimensat (UV_SYSCALL_BASE + 348)
# endif
#endif /* __NR_utimensat */
int uv__accept4(int fd, struct sockaddr* addr, socklen_t* addrlen, int flags) {
#if __i386__
unsigned long args[] = {
(unsigned long) fd,
(unsigned long) addr,
(unsigned long) addrlen,
(unsigned long) flags
};
return syscall(__NR_socketcall, 18 /* SYS_ACCEPT4 */, args);
#elif __NR_accept4
return syscall(__NR_accept4, fd, addr, addrlen, flags);
#else
return errno = ENOSYS, -1;
#endif
}
int uv__inotify_init(void) {
#if __NR_inotify_init
return syscall(__NR_inotify_init);
#else
return errno = ENOSYS, -1;
#endif
}
int uv__inotify_init1(int flags) {
#if __NR_inotify_init1
return syscall(__NR_inotify_init1, flags);
#else
return errno = ENOSYS, -1;
#endif
}
int uv__inotify_add_watch(int fd, const char* path, __u32 mask) {
#if __NR_inotify_add_watch
return syscall(__NR_inotify_add_watch, fd, path, mask);
#else
return errno = ENOSYS, -1;
#endif
}
int uv__inotify_rm_watch(int fd, __s32 wd) {
#if __NR_inotify_rm_watch
return syscall(__NR_inotify_rm_watch, fd, wd);
#else
return errno = ENOSYS, -1;
#endif
}
int uv__pipe2(int pipefd[2], int flags) {
#if __NR_pipe2
return syscall(__NR_pipe2, pipefd, flags);
#else
return errno = ENOSYS, -1;
#endif
}
int uv__sendmmsg(int fd,
struct uv__mmsghdr* mmsg,
unsigned int vlen,
unsigned int flags) {
#if __NR_sendmmsg
return syscall(__NR_sendmmsg, fd, mmsg, vlen, flags);
#else
return errno = ENOSYS, -1;
#endif
}
int uv__recvmmsg(int fd,
struct uv__mmsghdr* mmsg,
unsigned int vlen,
unsigned int flags,
struct timespec* timeout) {
#if __NR_recvmmsg
return syscall(__NR_recvmmsg, fd, mmsg, vlen, flags, timeout);
#else
return errno = ENOSYS, -1;
#endif
}
int uv__utimesat(int dirfd,
const char* path,
const struct timespec times[2],
int flags)
{
#if __NR_utimensat
return syscall(__NR_utimensat, dirfd, path, times, flags);
#else
return errno = ENOSYS, -1;
#endif
}

87
src/unix/linux/syscalls.h Normal file
View File

@ -0,0 +1,87 @@
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
*/
#ifndef UV_LINUX_SYSCALL_H_
#define UV_LINUX_SYSCALL_H_
#undef _GNU_SOURCE
#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/types.h>
#define UV__O_NONBLOCK 0x800
#define UV__O_CLOEXEC 0x80000
#define UV__SOCK_CLOEXEC UV__O_CLOEXEC
#define UV__SOCK_NONBLOCK UV__O_NONBLOCK
#define UV__IN_CLOEXEC UV__O_CLOEXEC
#define UV__IN_NONBLOCK UV__O_NONBLOCK
#define UV__IN_ACCESS 0x001
#define UV__IN_MODIFY 0x002
#define UV__IN_ATTRIB 0x004
#define UV__IN_CLOSE_WRITE 0x008
#define UV__IN_CLOSE_NOWRITE 0x010
#define UV__IN_OPEN 0x020
#define UV__IN_MOVED_FROM 0x040
#define UV__IN_MOVED_TO 0x080
#define UV__IN_CREATE 0x100
#define UV__IN_DELETE 0x200
#define UV__IN_DELETE_SELF 0x400
#define UV__IN_MOVE_SELF 0x800
struct uv__inotify_event {
__s32 wd;
__u32 mask;
__u32 cookie;
__u32 len;
/* char name[0]; */
};
struct uv__mmsghdr {
struct msghdr msg_hdr;
unsigned int msg_len;
};
int uv__accept4(int fd, struct sockaddr* addr, socklen_t* addrlen, int flags);
int uv__inotify_init(void);
int uv__inotify_init1(int flags);
int uv__inotify_add_watch(int fd, const char* path, __u32 mask);
int uv__inotify_rm_watch(int fd, __s32 wd);
int uv__pipe2(int pipefd[2], int flags);
int uv__recvmmsg(int fd,
struct uv__mmsghdr* mmsg,
unsigned int vlen,
unsigned int flags,
struct timespec* timeout);
int uv__sendmmsg(int fd,
struct uv__mmsghdr* mmsg,
unsigned int vlen,
unsigned int flags);
int uv__utimesat(int dirfd,
const char* path,
const struct timespec times[2],
int flags);
#endif /* UV_LINUX_SYSCALL_H_ */

View File

@ -105,7 +105,7 @@ int uv__make_socketpair(int fds[2], int flags) {
int uv__make_pipe(int fds[2], int flags) {
#if HAVE_SYS_PIPE2
#if __linux__
int fl;
fl = O_CLOEXEC;
@ -113,17 +113,11 @@ int uv__make_pipe(int fds[2], int flags) {
if (flags & UV__F_NONBLOCK)
fl |= O_NONBLOCK;
if (sys_pipe2(fds, fl) == 0)
if (uv__pipe2(fds, fl) == 0)
return 0;
if (errno != ENOSYS)
return -1;
/* errno == ENOSYS so maybe the kernel headers lied about
* the availability of pipe2(). This can happen if people
* build libuv against newer kernel headers than the kernel
* they actually run the software on.
*/
#endif
if (pipe(fds))

2
uv.gyp
View File

@ -224,6 +224,8 @@
'sources': [
'src/unix/linux/core.c',
'src/unix/linux/inotify.c',
'src/unix/linux/syscalls.c',
'src/unix/linux/syscalls.h',
],
'defines': [
'EV_CONFIG_H="config_linux.h"',