Skip to content
This repository has been archived by the owner on May 4, 2018. It is now read-only.

Commit

Permalink
linux: tidy up syscall code
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Mar 31, 2012
1 parent 4632163 commit 685b36b
Show file tree
Hide file tree
Showing 9 changed files with 360 additions and 219 deletions.
2 changes: 1 addition & 1 deletion config-unix.mk
Expand Up @@ -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))
Expand Down
5 changes: 2 additions & 3 deletions src/unix/core.c
Expand Up @@ -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;

Expand Down
26 changes: 15 additions & 11 deletions src/unix/fs.c
Expand Up @@ -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,
Expand Down
78 changes: 5 additions & 73 deletions src/unix/internal.h
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
139 changes: 16 additions & 123 deletions src/unix/linux/inotify.c
Expand Up @@ -21,6 +21,7 @@
#include "uv.h"
#include "tree.h"
#include "../internal.h"
#include "syscalls.h"

#include <stdint.h>
#include <stdio.h>
Expand All @@ -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. */
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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 */

0 comments on commit 685b36b

Please sign in to comment.