Merge branch 'tizen_2.2' into tizen
[platform/core/appfw/heynoti.git] / heynoti.c
old mode 100644 (file)
new mode 100755 (executable)
index 18beb7a..aed59d3
--- a/heynoti.c
+++ b/heynoti.c
 #include <glib.h>
 #include <sys/utsname.h>
 
+/* For multi-user support */
+#include <tzplatform_config.h>
+
 #include "heynoti-internal.h"
 
 #define AU_PREFIX_SYSNOTI "SYS"
 
 #ifndef NOTI_ROOT
-#  define NOTI_ROOT "/tmp/noti"
+#define NOTI_ROOT tzplatform_mkpath(TZ_SYS_SHARE, "noti")
 #endif
 
 
@@ -60,7 +63,6 @@ static int __make_noti_root(const char *p);
 static int __make_noti_file(const char *p);
 static inline int __make_noti_path(char *path, int size, const char *name);
 static int __read_proc(const char *path, char *buf, int size);
-static int __get_kern_ver();
 static void __clear_nslot_list(GList *g_ns);
 static struct noti_cont *__get_noti_cont(int fd);
 static int __handle_callback(struct noti_cont *nc, int wd, uint32_t mask);
@@ -107,20 +109,17 @@ struct noti_cont {
 };
 typedef struct noti_cont ncont;
 
-static const char *noti_root = NOTI_ROOT;
 static GList *g_nc;
 
 static int __make_noti_root(const char *p)
 {
        int fd;
 
-       if ((fd = open(p, O_RDONLY)) == -1) {
-               if (mkdir(p, 0777) == -1)
-                       return -1;
-       } else {
-               fchmod(fd, 0777);
-               close(fd);
-       }
+       if ((fd = open(p, O_RDONLY)) == -1 )
+               return -1;
+
+       close(fd);
+
        return 0;
 }
 
@@ -128,34 +127,17 @@ static int __make_noti_file(const char *p)
 {
        int fd;
 
-#ifdef NODAC_PERMISSION
-       if ((fd = open(p, O_RDONLY)) == -1) {
-               if ((fd = open(p, O_CREAT, 0666)) < 0) {
-                       return -1;
-               }
-               fchmod(fd, 0666);
-               close(fd);
-       } else {
-               fchmod(fd, 0666);
-               close(fd);
-       }
-#else
-       if ((fd = open(p, O_RDONLY)) == -1) {
-               if ((fd = open(p, O_CREAT, 0644)) < 0) {
-                       return -1;
-               }
-               close(fd);
-       } else {
-               fchmod(fd, 0644);
-               close(fd);
-       }
-#endif
+       if ((fd = open(p, O_RDONLY)) == -1)
+               return -1;
+
+       close(fd);
+
        return 0;
 }
 
 static inline int __make_noti_path(char *path, int size, const char *name)
 {
-       return snprintf(path, size, "%s/%s", noti_root, name);
+       return snprintf(path, size, "%s/%s", NOTI_ROOT, name);
 }
 
 static int __read_proc(const char *path, char *buf, int size)
@@ -179,10 +161,9 @@ static int __read_proc(const char *path, char *buf, int size)
        return ret;
 }
 
-static int kern_ver;           /* Kernel version */
 #define PROC_VERSION "/proc/version"
 
-static int __get_kern_ver()
+static int __check_kern_ver()
 {
        struct utsname nm;
        int ret;
@@ -191,8 +172,6 @@ static int __get_kern_ver()
        int v2;
        int v3;
 
-       util_retv_if(kern_ver != 0, kern_ver);
-
        ret = uname(&nm);
        if (ret == 0) {
                strncpy(buf, nm.release, sizeof(buf));
@@ -205,8 +184,11 @@ static int __get_kern_ver()
        }
 
        if (ret == 3) {
-               kern_ver = KERNEL_VERSION(v1, v2, v3);
-               return kern_ver;
+               if(KERNEL_VERSION(v1, v2, v3) < KERNEL_VERSION(2, 6, 13)) {
+                       return -1;
+               } else {
+                       return 0;
+               }
        }
 
        return -1;
@@ -267,7 +249,7 @@ static int __handle_event(int fd)
 {
        int r;
        struct inotify_event ie;
-       char name[FILENAME_MAX];
+       char name[FILENAME_MAX] = {0, };
 
        struct noti_cont *nc;
 
@@ -280,10 +262,16 @@ static int __handle_event(int fd)
                if (nc)
                        __handle_callback(nc, ie.wd, ie.mask);
 
-               if (ie.len > 0)
-                       read(fd, name, ie.len);
+               if(ie.len > SSIZE_MAX)
+                       return -1;
+
+               if (ie.len > 0u) {
+                       r = read(fd, name, (ie.len > FILENAME_MAX) ? (size_t)FILENAME_MAX : (size_t) ie.len);
+               }
 
-               r = read(fd, &ie, sizeof(ie));
+               if(r > 0) {
+                       r = read(fd, &ie, sizeof(ie));
+               }
        }
 
        return 0;
@@ -364,8 +352,6 @@ static int __add_noti(int fd, const char *notipath, void (*cb) (void *),
                return -1;
        }
 
-       __make_noti_file(notipath);
-
        wd = __get_wd(fd, notipath);
        util_retvm_if(wd == -1, -1, "Error: add noti: %s", strerror(errno));
 
@@ -401,18 +387,6 @@ static int __add_noti(int fd, const char *notipath, void (*cb) (void *),
        return 0;
 }
 
-API int heynoti_subscribe_file(int fd, const char *path, void (*cb) (void *),
-                              void *data, uint32_t mask)
-{
-       if (path == NULL || cb == NULL) {
-               UTIL_DBG("Error: add noti: Invalid input");
-               errno = EINVAL;
-               return -1;
-       }
-
-       return __add_noti(fd, path, cb, data, mask);
-}
-
 API int heynoti_subscribe(int fd, const char *noti, void (*cb) (void *),
                          void *data)
 {
@@ -515,21 +489,6 @@ API int heynoti_unsubscribe(int fd, const char *noti, void (*cb) (void *))
        return r;
 }
 
-API int heynoti_unsubscribe_file(int fd, const char *path, void (*cb) (void *))
-{
-       int r;
-
-       if (fd < 0 || path == NULL) {
-               errno = EINVAL;
-               return -1;
-       }
-
-       r = del_noti(fd, path, cb);
-       util_warn_if(r == -1, "Error: del [%s]: %s", path, strerror(errno));
-
-       return r;
-}
-
 API int heynoti_publish(const char *noti)
 {
        int fd;
@@ -551,11 +510,8 @@ API int heynoti_publish(const char *noti)
        }
        UTIL_DBG("send noti: [%s]", notipath);
 
-#ifdef NODAC_PERMISSION
-       fd = open(notipath, O_CREAT | O_TRUNC | O_RDWR, 0666);
-#else
-       fd = open(notipath, O_CREAT | O_TRUNC | O_RDWR, 0644);
-#endif
+       fd = open(notipath, O_TRUNC | O_WRONLY);
+
        util_retvm_if(fd == -1, -1, "Error: send noti: %s", strerror(errno));
 
        /*
@@ -565,9 +521,6 @@ API int heynoti_publish(const char *noti)
        if(sb.st_mode & 0033)
                fchmod(fd, 0644);
        */
-#ifdef NODAC_PERMISSION
-       fchmod(fd, 0666);
-#endif
 
        close(fd);
        return 0;
@@ -580,7 +533,7 @@ API int heynoti_init()
 
        struct noti_cont *nc;
 
-       if (__get_kern_ver() < KERNEL_VERSION(2, 6, 13)) {
+       if(__check_kern_ver() < 0) {
                UTIL_ERR("inotify requires kernel version >= 2.6.13 ");
                errno = EPERM;
                return -1;
@@ -589,12 +542,14 @@ API int heynoti_init()
        fd = inotify_init();
        util_retvm_if(fd == -1, -1, "inotify init: %s", strerror(errno));
 
-       fcntl(fd, F_SETFD, FD_CLOEXEC);
-       fcntl(fd, F_SETFL, O_NONBLOCK);
+       r = fcntl(fd, F_SETFD, FD_CLOEXEC);
+       util_retvm_if(r < 0, -1, "fcntl error : %s", strerror(errno));
+       r = fcntl(fd, F_SETFL, O_NONBLOCK);
+       util_retvm_if(r < 0, -1, "fcntl error : %s", strerror(errno));
 
-       r = __make_noti_root(noti_root);
+       r = __make_noti_root(NOTI_ROOT);
        if (r == -1) {
-               UTIL_ERR("make noti root: %s : %s", noti_root, strerror(errno));
+               UTIL_ERR("make noti root: %s : %s", NOTI_ROOT, strerror(errno));
                close(fd);
                return -1;
        }