Module: xenomai-forge
Branch: next
Commit: 291b25fc77c2a0b2ab0eb91498f57b35370b4453
URL:    
http://git.xenomai.org/?p=xenomai-forge.git;a=commit;h=291b25fc77c2a0b2ab0eb91498f57b35370b4453

Author: Philippe Gerum <r...@xenomai.org>
Date:   Tue Sep  9 16:11:38 2014 +0200

cobalt/posix/timerfd, lib/cobalt: alloc file descriptors on the anon inode

---

 kernel/cobalt/posix/timerfd.c |   36 ++++++++++++++++++++++++++++--------
 kernel/cobalt/posix/timerfd.h |    2 +-
 lib/cobalt/timerfd.c          |   17 ++++++-----------
 3 files changed, 35 insertions(+), 20 deletions(-)

diff --git a/kernel/cobalt/posix/timerfd.c b/kernel/cobalt/posix/timerfd.c
index 5502bf1..8b85de3 100644
--- a/kernel/cobalt/posix/timerfd.c
+++ b/kernel/cobalt/posix/timerfd.c
@@ -18,6 +18,8 @@
 
 #include <linux/timerfd.h>
 #include <linux/err.h>
+#include <linux/fdtable.h>
+#include <linux/anon_inodes.h>
 #include <cobalt/kernel/timer.h>
 #include <cobalt/kernel/select.h>
 #include <rtdm/fd.h>
@@ -162,15 +164,12 @@ static void timerfd_handler(struct xntimer *xntimer)
 }
 
 COBALT_SYSCALL(timerfd_create, lostage,
-              int, (int ufd, int clockid, int flags))
+              int, (int clockid, int flags))
 {
        struct cobalt_tfd *tfd;
        struct xnthread *curr;
-       struct xnsys_ppd *p;
-
-       p = cobalt_ppd_get(0);
-       if (p == &__xnsys_global_ppd)
-               return -EPERM;
+       struct xnsys_ppd *ppd;
+       int ret, ufd;
 
        if (clockid != CLOCK_REALTIME && clockid != CLOCK_MONOTONIC)
                return -EINVAL;
@@ -182,6 +181,14 @@ COBALT_SYSCALL(timerfd_create, lostage,
        if (tfd == NULL)
                return -ENOMEM;
 
+       ppd = cobalt_ppd_get(0);
+       ufd = anon_inode_getfd("[cobalt-timerfd]", &rtdm_dumb_fops, ppd,
+                              O_RDWR | (flags & TFD_SHARED_FCNTL_FLAGS));
+       if (ufd < 0) {
+               ret = ufd;
+               goto fail_getfd;
+       }
+
        tfd->flags = flags;
        tfd->clockid = clockid;
        curr = xnthread_current();
@@ -191,8 +198,21 @@ COBALT_SYSCALL(timerfd_create, lostage,
        xnselect_init(&tfd->read_select);
        tfd->target = NULL;
 
-       return rtdm_fd_enter(p, &tfd->fd, ufd, COBALT_TIMERFD_MAGIC,
-                       &timerfd_ops);
+       ret = rtdm_fd_enter(ppd, &tfd->fd, ufd, COBALT_TIMERFD_MAGIC,
+                           &timerfd_ops);
+       if (ret < 0)
+               goto fail;
+
+       return ufd;
+fail:
+       xnselect_destroy(&tfd->read_select);
+       xnsynch_destroy(&tfd->readers);
+       xntimer_destroy(&tfd->timer);
+       __close_fd(current->files, ufd);
+fail_getfd:
+       xnfree(tfd);
+
+       return ret;
 }
 
 static inline struct cobalt_tfd *tfd_get(int ufd)
diff --git a/kernel/cobalt/posix/timerfd.h b/kernel/cobalt/posix/timerfd.h
index c833c83..ef1592c 100644
--- a/kernel/cobalt/posix/timerfd.h
+++ b/kernel/cobalt/posix/timerfd.h
@@ -5,7 +5,7 @@
 #include <xenomai/posix/syscall.h>
 
 COBALT_SYSCALL_DECL(timerfd_create,
-                   int, (int fd, int clockid, int flags));
+                   int, (int clockid, int flags));
 
 COBALT_SYSCALL_DECL(timerfd_settime,
                    int, (int fd, int flags,
diff --git a/lib/cobalt/timerfd.c b/lib/cobalt/timerfd.c
index 97e54eb..417e3aa 100644
--- a/lib/cobalt/timerfd.c
+++ b/lib/cobalt/timerfd.c
@@ -24,20 +24,15 @@
 
 COBALT_IMPL(int, timerfd_create, (int clockid, int flags))
 {
-       int ret;
        int fd;
 
-       fd = __STD(open("/dev/null", O_RDWR, 0));
-       if (fd == -1)
-               return fd;
-
-       ret = -XENOMAI_SYSCALL3(sc_cobalt_timerfd_create, fd, clockid, flags);
-       if (ret == 0)
-               return fd;
+       fd = XENOMAI_SYSCALL2(sc_cobalt_timerfd_create, clockid, flags);
+       if (fd < 0) {
+               errno = -fd;
+               return -1;
+       }
        
-       __STD(close(fd));
-       errno = ret;
-       return -1;
+       return fd;
 }
 
 COBALT_IMPL(int, timerfd_settime, (int fd, int flags,


_______________________________________________
Xenomai-git mailing list
Xenomai-git@xenomai.org
http://www.xenomai.org/mailman/listinfo/xenomai-git

Reply via email to