Integrate tizen-platform-wrapper 56/110856/2 accepted/tizen/common/20170303.090922 accepted/tizen/ivi/20170303.070158 accepted/tizen/mobile/20170303.070120 accepted/tizen/tv/20170303.070128 accepted/tizen/unified/20170309.033834 accepted/tizen/wearable/20170303.070137 submit/tizen/20170303.023924 submit/tizen_unified/20170308.100409
authorSunmin Lee <sunm.lee@samsung.com>
Wed, 18 Jan 2017 08:31:25 +0000 (17:31 +0900)
committerSunmin Lee <sunm.lee@samsung.com>
Thu, 23 Feb 2017 00:58:49 +0000 (09:58 +0900)
In terms of management, it looks better that tizen-platform-config
sources are migrated in its package.

Change-Id: I70f0cd9b7ec98a375aa4bfa2fd7012d9ec5a0d36
Signed-off-by: Sunmin Lee <sunm.lee@samsung.com>
32 files changed:
packaging/tizen-platform-config.spec
src/Makefile.am
src/buffer.c [new file with mode: 0644]
src/buffer.h [new file with mode: 0644]
src/build.sh [new file with mode: 0755]
src/context.c [new file with mode: 0644]
src/context.h [new file with mode: 0644]
src/foreign.c [new file with mode: 0644]
src/foreign.h [new file with mode: 0644]
src/hashing.c [new file with mode: 0644]
src/hashing.h [new file with mode: 0644]
src/heap.c [new file with mode: 0644]
src/heap.h [new file with mode: 0644]
src/init.c [new file with mode: 0644]
src/init.h [new file with mode: 0644]
src/isadmin.c [new file with mode: 0644]
src/isadmin.h [new file with mode: 0644]
src/parser.c [new file with mode: 0644]
src/parser.h [new file with mode: 0644]
src/passwd.c [new file with mode: 0644]
src/passwd.h [new file with mode: 0644]
src/scratch.c [new file with mode: 0644]
src/scratch.h [new file with mode: 0644]
src/sha256sum.c [new file with mode: 0644]
src/sha256sum.h [new file with mode: 0644]
src/shared-api.c [new file with mode: 0644]
src/shared-api.h [new file with mode: 0644]
src/static-api.c [new file with mode: 0644]
src/toolbox.c [new file with mode: 0644]
src/tzplatform_config.h [new file with mode: 0644]
src/tzplatform_config.sym [new file with mode: 0644]
src/tzplatform_get.c [new file with mode: 0644]

index abaccaa..7583841 100644 (file)
@@ -5,7 +5,7 @@ Name:           tizen-platform-config
 Version:        4.0
 Release:        1
 Summary:        Tizen Platform Configuration
-License:        MIT
+License:        MIT and LGPL-2.0
 Url:            http://www.tizen.org
 Group:          System/Configuration
 Source0:        %{name}-%{version}.tar.gz
@@ -14,8 +14,10 @@ Source1001:     %{name}.manifest
 Source1002:     201.tizen-platform-config_upgrade.sh
 Source2001:     200.filesystem-pre.patch.sh
 Source2002:     799.filesystem-post.patch.sh
-BuildRequires:  tizen-platform-wrapper >= 2
 BuildRequires:  tizen-platform-config-meta
+BuildRequires:  gperf
+# for sha256sum
+BuildRequires:  coreutils
 Requires(post): smack
 Requires(post): coreutils
 
index 18e28dd..fbf3de1 100644 (file)
@@ -2,8 +2,6 @@ lib_LTLIBRARIES = libtzplatform-config-@LIBTZPLATFORM_CONFIG_VERSION@.la
 
 bin_PROGRAMS = tzplatform-get
 
-wrappersrcdir=$(shell pkg-config --variable=datadir tizen-platform-wrapper)
-
 SRC_FROMWRAPPER_STATIC= \
        static-api.c \
        isadmin.h \
@@ -35,35 +33,43 @@ SRC_FROMWRAPPER_SHARED= \
 
 SRC_FROMWRAPPER=$(SRC_FROMWRAPPER_SHARED) $(SRC_FROMWRAPPER_STATIC)
 
+SRC_TOOL = \
+       buffer.c \
+       foreign.c \
+       heap.c \
+       parser.c \
+       sha256sum.c \
+       toolbox.c
+
 SRC_FROMTOOL= \
        tzplatform_variables.h \
        signup.inc \
        hash.inc
 
-CFLAGS += -fPIC
+libtzplatform_config_@LIBTZPLATFORM_CONFIG_VERSION@_la_CFLAGS = -fPIC
 
-BUILT_SOURCES=$(SRC_FROMWRAPPER) $(SRC_FROMTOOL)
+BUILT_SOURCES=tzplatform-tool $(SRC_FROMTOOL)
 
-CLEANFILES=$(SRC_FROMWRAPPER) $(SRC_FROMTOOL)
+CLEANFILES=tzplatform-tool $(SRC_FROMTOOL)
 
 libtzplatform_config_@LIBTZPLATFORM_CONFIG_VERSION@_la_SOURCES = \
        $(SRC_FROMWRAPPER) \
        $(SRC_FROMTOOL)
 
-tizen-platform.conf: /etc/tizen-platform.meta
-       tzplatform-tool pretty $< >$@
+tzplatform-tool: $(SRC_TOOL)
+       $(CC) $^ -o $@
 
-$(SRC_FROMWRAPPER) tzplatform_get.c:
-       cp $(wrappersrcdir)/tizen-platform-wrapper/$@ .
+tizen-platform.conf: /etc/tizen-platform.meta tzplatform-tool
+       ./tzplatform-tool pretty $< >$@
 
-tzplatform_variables.h: tizen-platform.conf
-       tzplatform-tool h $< >$@
+tzplatform_variables.h: tizen-platform.conf tzplatform-tool
+       ./tzplatform-tool h $< >$@
 
-hash.inc: tizen-platform.conf
-       tzplatform-tool c $< >$@
+hash.inc: tizen-platform.conf tzplatform-tool
+       ./tzplatform-tool c $< >$@
 
-signup.inc: tizen-platform.conf
-       tzplatform-tool signup $< >$@
+signup.inc: tizen-platform.conf tzplatform-tool
+       ./tzplatform-tool signup $< >$@
 
 libtzplatform_config_@LIBTZPLATFORM_CONFIG_VERSION@_la_LDFLAGS = -version-info $(LIBTZPLATFORM_CONFIG_SO_VERSION)
 libtzplatform_config_@LIBTZPLATFORM_CONFIG_VERSION@_la_LDFLAGS += -Wl,--version-script=tzplatform_config.sym
@@ -75,8 +81,8 @@ nobase_libtzplatform_config_include_HEADERS = tzplatform_variables.h tzplatform_
 
 dist_sysconf_DATA=tizen-platform.conf
 
-macros.tizen-platform: tizen-platform.conf
-       tzplatform-tool rpm $< | grep -v TZ_USER_ > $@
+macros.tizen-platform: tizen-platform.conf tzplatform-tool
+       ./tzplatform-tool rpm $< | grep -v TZ_USER_ > $@
 
 tzplatform_config_sysconfdir= $(sysconfdir)/rpm
 dist_tzplatform_config_sysconf_DATA=macros.tizen-platform
diff --git a/src/buffer.c b/src/buffer.c
new file mode 100644 (file)
index 0000000..92e9bf3
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+
+#include "buffer.h"
+
+static int buffread( struct buffer *buffer, int fd)
+{
+    const int size = 4096;
+    void *p;
+    char *memory = NULL;
+    size_t length = 0;
+    ssize_t status;
+
+    /* do read */
+    for(;;) {
+        p = realloc( memory, length + size);
+        if (p == NULL) {
+            free( memory);
+            return -1;
+        }
+        memory = p;
+        status = read( fd, memory+length, size);
+        if (status == 0) {
+            buffer->buffer = memory;
+            buffer->length = length;
+            buffer->mapped = 0;
+            return 0;
+        }
+        if (status > 0) {
+            length = length + (int)status;
+        }
+        else if (errno != EAGAIN && errno != EINTR) {
+            free( memory);
+            return -1;
+        }   
+    }
+}
+
+int buffer_create( struct buffer *buffer, const char *pathname)
+{
+    int fd, result;
+    struct stat bstat;
+    void *memory;
+    size_t length;
+
+    result = open(pathname, O_RDONLY);
+    if (result >= 0)
+    {
+        fd = result;
+        result = fstat(fd, &bstat);
+        if (result == 0)
+        {
+            length = (size_t)bstat.st_size;
+            if (bstat.st_size != (off_t)length)
+            {
+                errno = EOVERFLOW;
+                result = -1;
+            }
+            else
+            {
+                memory = mmap(NULL,length,PROT_READ,MAP_PRIVATE,fd,0);
+                if (memory != MAP_FAILED) {
+                    buffer->buffer = memory;
+                    buffer->length = length;
+                    buffer->mapped = 1;
+                }
+                else {
+                    result = buffread( buffer, fd);
+                }
+            }
+        }
+        close(fd);
+    }
+    return result;
+}
+
+int buffer_destroy( struct buffer *buffer)
+{
+    if (buffer->mapped)
+        return munmap(buffer->buffer, buffer->length);
+    free( buffer->buffer);
+    return 0;
+}
+
+
diff --git a/src/buffer.h b/src/buffer.h
new file mode 100644 (file)
index 0000000..28b43be
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef BUFFER_H
+#define BUFFER_H
+
+/* structure of the buffer */
+struct buffer {
+    char    *buffer;    /* start address */
+    size_t   length;    /* length in byte */
+    int      mapped;    /* is memory mapped */
+};
+
+/*
+   Create the 'buffer' from reading content of the file of 'pathname'.
+   Returns 0 if success, -1 if error occured (see then errno)
+*/
+int buffer_create( struct buffer *buffer, const char *pathname);
+
+/*
+   Destroy the 'buffer'.
+   Returns 0 if success, -1 if error occured (see then errno)
+*/
+int buffer_destroy( struct buffer *buffer);
+
+#endif
+
diff --git a/src/build.sh b/src/build.sh
new file mode 100755 (executable)
index 0000000..8f76b08
--- /dev/null
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+# useful build script for purpose of testing
+
+f="-O -Wall -DCONFIGPATH=\"meta\" -fPIC"
+
+e() { echo error: "$@" >&2; exit 1; }
+d() { echo running: "$@" >&2; "$@" || e "$@"; }
+
+[ -f meta ] || e no file meta
+
+d gcc $f -o toolbox toolbox.c parser.c buffer.c foreign.c sha256sum.c
+d ./toolbox h > tzplatform_variables.h
+d ./toolbox c > hash.inc
+d ./toolbox signup > signup.inc
+d gcc $f -c *.c
+d ld -shared --version-script=tzplatform_config.sym -o libtzplatform-shared.so buffer.o   foreign.o  heap.o  parser.o  scratch.o context.o  hashing.o  init.o  passwd.o  shared-api.o
+d ar cr libtzplatform-static.a static-api.o isadmin.o
+d gcc -o get tzplatform_get.o static-api.o -L. -ltzplatform-static -ltzplatform-shared
+
+
diff --git a/src/context.c b/src/context.c
new file mode 100644 (file)
index 0000000..85c417b
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+
+
+
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <unistd.h>
+
+#ifndef NOT_MULTI_THREAD_SAFE
+#include <pthread.h>
+#endif
+
+#include "tzplatform_variables.h"
+#include "heap.h"
+#include "foreign.h"
+#include "context.h"
+
+
+uid_t get_uid(struct tzplatform_context *context)
+{
+    uid_t result;
+
+    result = context->user;
+    if (result == _USER_NOT_SET_)
+        result = getuid();
+
+    return result;
+}
+
+#if _FOREIGN_HAS_(EUID)
+uid_t get_euid(struct tzplatform_context *context)
+{
+    uid_t result;
+
+    result = context->user;
+    if (result == _USER_NOT_SET_)
+        result = geteuid();
+
+    return result;
+}
+#endif
+
+#if _FOREIGN_HAS_(GID)
+gid_t get_gid(struct tzplatform_context *context)
+{
+    return getgid();
+}
+#endif
+
diff --git a/src/context.h b/src/context.h
new file mode 100644 (file)
index 0000000..9353168
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef CONTEXT_H
+#define CONTEXT_H
+
+#ifndef HEAP_H
+#error "you should include heap.h"
+#endif
+
+#ifndef FOREIGN_H
+#error "you should include foreign.h"
+#endif
+
+enum STATE { RESET=0, ERROR, VALID };
+
+#define _USER_NOT_SET_  ((uid_t)-1)
+
+struct tzplatform_context {
+#ifndef NOT_MULTI_THREAD_SAFE
+    pthread_mutex_t mutex;
+#endif
+    enum STATE state;
+    uid_t user;
+    struct heap heap;
+    const char *values[_TZPLATFORM_VARIABLES_COUNT_];
+};
+
+uid_t get_uid(struct tzplatform_context *context);
+
+#if _FOREIGN_HAS_(EUID)
+uid_t get_euid(struct tzplatform_context *context);
+#endif
+
+#if _FOREIGN_HAS_(GID)
+gid_t get_gid(struct tzplatform_context *context);
+#endif
+
+#endif
+
diff --git a/src/foreign.c b/src/foreign.c
new file mode 100644 (file)
index 0000000..a19f6ad
--- /dev/null
@@ -0,0 +1,90 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+
+#include "foreign.h"
+
+enum fkey foreign( const char *name, size_t length)
+{
+    if (length == 3) { /* UID */
+        if (name[1]=='I' && name[2]=='D')
+            switch (name[0]) {
+#if _FOREIGN_HAS_(UID)
+            case 'G': /* GID */ return GID;
+#endif
+#if _FOREIGN_HAS_(GID)
+            case 'U': /* UID */ return UID;
+#endif
+            default: break;
+            }
+    }
+    else if (length == 4) {
+        switch (name[0]) {
+#if _FOREIGN_HAS_(HOME)
+        case 'H': /* HOME */
+            if (name[1]=='O' && name[2]=='M' && name[3]=='E')
+                return HOME;
+            break;
+#endif
+#if _FOREIGN_HAS_(EUID)
+        case 'E': /* EUID */
+            if (name[1]=='U' && name[2]=='I' && name[3]=='D')
+                return EUID;
+            break;
+#endif
+#if _FOREIGN_HAS_(USER)
+        case 'U': /* USER */
+            if (name[1]=='S' && name[2]=='E' && name[3]=='R')
+                return USER;
+            break;
+#endif
+        default: break;
+        }
+    }
+    else if (length == 5 && name[0]=='E') {
+        switch (name[1]) {
+#if _FOREIGN_HAS_(EHOME)
+        case 'H': /* EHOME */
+            if (name[2]=='O' && name[3]=='M' && name[4]=='E')
+                return EHOME;
+            break;
+#endif
+#if _FOREIGN_HAS_(EUSER)
+        case 'U': /* EUSER */
+            if (name[2]=='S' && name[3]=='E' && name[4]=='R')
+                return EUSER;
+            break;
+#endif
+        default: break;
+        }
+    }
+    return _FOREIGN_INVALID_;
+}
+
diff --git a/src/foreign.h b/src/foreign.h
new file mode 100644 (file)
index 0000000..646a41d
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef FOREIGN_H
+#define FOREIGN_H
+
+#define _FOREIGN_MASK_HOME_    1
+#define _FOREIGN_MASK_UID_     2
+#define _FOREIGN_MASK_USER_    4
+#define _FOREIGN_MASK_GID_     8
+#define _FOREIGN_MASK_EHOME_  16
+#define _FOREIGN_MASK_EUID_   32
+#define _FOREIGN_MASK_EUSER_  64
+
+#define _FOREIGNS_TO_USE_   ( _FOREIGN_MASK_HOME_  \
+                            | _FOREIGN_MASK_USER_  )
+
+#define _FOREIGN_HAS_(x)  (0 != ((_FOREIGNS_TO_USE_) & (_FOREIGN_MASK_##x##_)))
+
+enum fkey {
+    _FOREIGN_INVALID_ = -1, 
+#if _FOREIGN_HAS_(HOME)
+    HOME,
+#endif
+#if _FOREIGN_HAS_(UID)
+    UID, 
+#endif
+#if _FOREIGN_HAS_(USER)
+    USER,
+#endif
+#if _FOREIGN_HAS_(GID)
+    GID, 
+#endif
+#if _FOREIGN_HAS_(EHOME)
+    EHOME, 
+#endif
+#if _FOREIGN_HAS_(EUID)
+    EUID, 
+#endif
+#if _FOREIGN_HAS_(EUSER)
+    EUSER,
+#endif
+    _FOREIGN_COUNT_
+};
+
+/* get the foreign key for the 'name' of 'length' */
+enum fkey foreign( const char *name, size_t length);
+
+#endif
+
diff --git a/src/hashing.c b/src/hashing.c
new file mode 100644 (file)
index 0000000..e74464d
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <string.h>
+#include <assert.h>
+
+#include "tzplatform_variables.h"
+#include "hash.inc"
+
+static const char *var_names[_TZPLATFORM_VARIABLES_COUNT_];
+
+int hashid(const char *text, unsigned int len)
+{
+    const struct varassoc *vara = hashvar(text, len);
+    return vara ? vara->id : -1;
+}
+
+const char *keyname(int id)
+{
+    const struct varassoc *iter, *end;
+
+    assert(0 <= id && id < _TZPLATFORM_VARIABLES_COUNT_);
+    if (!var_names[0]) {
+        iter = namassoc;
+        end = iter + (sizeof namassoc / sizeof namassoc[0]);
+        while (iter != end) {
+            if (iter->offset >= 0) {
+                assert(0 <= iter->id && iter->id < _TZPLATFORM_VARIABLES_COUNT_);
+                var_names[iter->id] = varpool + iter->offset;
+            }
+            iter++;
+        }
+    }
+    return var_names[id];
+}
diff --git a/src/hashing.h b/src/hashing.h
new file mode 100644 (file)
index 0000000..f20f3e9
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef HASHING_H
+#define HASHING_H
+
+int hashid(const char *text, unsigned int len);
+const char *keyname(int id);
+
+#endif
+
diff --git a/src/heap.c b/src/heap.c
new file mode 100644 (file)
index 0000000..d091f51
--- /dev/null
@@ -0,0 +1,144 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <unistd.h>
+#include <string.h>
+#include <assert.h>
+#include <sys/mman.h>
+
+#include "heap.h"
+
+/* align to a size_t size */
+inline static size_t align(size_t size)
+{
+    /* we assume that sizeof(size_t) is a power of 2 */
+    assert( (sizeof(size_t) & (sizeof(size_t)-1)) == 0 );
+    return (size + (sizeof(size_t)-1)) & ~(sizeof(size_t)-1);
+}
+
+/* align to a page size */
+inline static size_t pagealign( size_t size)
+{
+    static size_t pagemask = 0;
+    /* we assume that pagesize is a power of 2 */
+    if (!pagemask) {
+       pagemask = (size_t)sysconf(_SC_PAGE_SIZE) - 1;
+       assert( pagemask );
+       assert( (pagemask & (pagemask+1)) == 0 );
+    }
+    return (size + pagemask) & ~pagemask;
+}
+
+int heap_create( struct heap *heap, size_t capacity)
+{
+    char *data;
+
+    /* allocation of the heap */
+    capacity = pagealign(capacity ? capacity : 1);
+    data = mmap(NULL, capacity, PROT_READ|PROT_WRITE,
+                                    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
+
+    /* success of the allocation? */
+    if (data == MAP_FAILED)
+        return -1;
+
+    /* yes. initialisae the heap */
+    heap->data = data;
+    heap->capacity = capacity;
+    heap->size = 0;
+    return 0;
+}
+
+int heap_destroy( struct heap *heap)
+{
+    return munmap( heap->data, heap->capacity);
+}
+
+int heap_resize( struct heap *heap, size_t size)
+{
+    /* has heap enought data? */
+    if (size > heap->capacity) {
+        
+        /* no. resizing of the heap. */
+        /* compute the sizes and realloc */
+        size_t capa = pagealign(size);
+        char *data = mremap(heap->data, heap->capacity, capa, MREMAP_MAYMOVE);
+
+        /* error if failure */
+        if (data == MAP_FAILED)
+            return -1;
+
+        /* record new parameters. */
+        heap->data = data;
+        heap->capacity = capa;
+    }
+    heap->size = size;
+
+    return 0;
+}
+
+size_t heap_alloc( struct heap *heap, size_t count)
+{
+    size_t result = heap->size;
+    if( heap_resize( heap, align( result + count)) != 0)
+        result = HNULL;
+    return result;
+}
+
+size_t heap_strndup( struct heap *heap, const char *value, size_t length)
+{
+    size_t offset;
+    char *string;
+
+    offset = heap_alloc( heap, length+1);
+    if (offset != HNULL) {
+        string = heap_address( heap, offset);
+        memcpy( string, value, length);
+        string[length] = 0;
+    }
+    return offset;
+}
+
+size_t heap_strdup( struct heap *heap, const char *string)
+{
+    return heap_strndup( heap, string, strlen(string));
+}
+
+int heap_read_write( struct heap *heap)
+{
+    return mprotect(heap->data, heap->capacity, PROT_READ|PROT_WRITE);
+}
+
+int heap_read_only( struct heap *heap)
+{
+    return mprotect(heap->data, heap->capacity, PROT_READ);
+}
+
+
+
diff --git a/src/heap.h b/src/heap.h
new file mode 100644 (file)
index 0000000..744e6c0
--- /dev/null
@@ -0,0 +1,99 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef HEAP_H
+#define HEAP_H
+
+/* null offset value */
+#define HNULL               ((size_t)-1)
+
+/* structure defining the heap */
+struct heap {
+    char  *data;     /* pointer to data of the heap */
+    size_t size;     /* count of used byte of the heap */
+    size_t capacity; /* count of byte of the heap */
+};
+
+/*
+   Return the address of the 'heap' memory of 'offset'.
+   The returned pointer is only valid until
+   a call to 'heap_destroy', 'heap_resize', 'heap_alloc' 
+*/
+inline static void *heap_address( struct heap *heap, size_t offset)
+{
+    return heap->data + offset;
+}
+
+
+/*
+   Initialize the 'heap' with a 'capacity' in byte.
+   The allocated size will be zero.
+   Returns 0 if success, -1 if error occured (see then errno)
+*/
+int heap_create( struct heap *heap, size_t capacity);
+
+/*
+   Resize the 'heap' to 'size'.
+   Returns 0 if success, -1 if error occured (see then errno)
+*/
+int heap_resize( struct heap *heap, size_t size);
+
+/*
+   Allocation of 'count' bytes from the 'heap'.
+   Returns the offset (in byte) from the start
+   of the heap. Return HNULL in case of error (see then errno).
+*/
+size_t heap_alloc( struct heap *heap, size_t count);
+
+/*
+   Copy the 'string' of 'length' in the 'heap', appending a terminating null.
+   Return the offset or HNULL if error.
+*/
+size_t heap_strndup( struct heap *heap, const char *string, size_t length);
+
+/*
+   Copy the 'string' in the 'heap' with the terminating null.
+   Return the offset or HNULL if error.
+*/
+size_t heap_strdup( struct heap *heap, const char *string);
+
+/*
+   Destroy the 'heap'.
+   Returns 0 if success, -1 if error occured (see then errno)
+*/
+int heap_destroy( struct heap *heap);
+
+/*
+   Set the heap as read/write
+   Returns 0 if success, -1 if error occured (see then errno)
+*/
+int heap_read_write( struct heap *heap);
+
+/*
+   Set the heap as read only
+   Returns 0 if success, -1 if error occured (see then errno)
+*/
+int heap_read_only( struct heap *heap);
+
+#endif
+
diff --git a/src/init.c b/src/init.c
new file mode 100644 (file)
index 0000000..4edf520
--- /dev/null
@@ -0,0 +1,431 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <pthread.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+#include <ctype.h>
+#include <stdarg.h>
+#include <alloca.h>
+#include <pwd.h>
+#include <assert.h>
+
+#ifndef NOT_MULTI_THREAD_SAFE
+#include <pthread.h>
+#endif
+
+#ifndef CONFIGPATH
+#define CONFIGPATH "/etc/tizen-platform.conf"
+#endif
+
+#include "tzplatform_variables.h"
+#include "tzplatform_config.h"
+#include "parser.h"
+#include "heap.h"
+#include "buffer.h"
+#include "foreign.h"
+#include "scratch.h"
+#include "passwd.h"
+#include "context.h"
+#include "hashing.h"
+#include "init.h"
+
+#define _HAS_IDS_   (  _FOREIGN_HAS_(UID)  \
+                    || _FOREIGN_HAS_(EUID) \
+                    || _FOREIGN_HAS_(GID)  )
+
+#define _HAS_PWS_   (  _FOREIGN_HAS_(HOME)  \
+                    || _FOREIGN_HAS_(USER)  \
+                    || _FOREIGN_HAS_(EHOME) \
+                    || _FOREIGN_HAS_(EUSER) )
+
+/* local and static variables */
+static const char metafilepath[] = CONFIGPATH;
+static const char emptystring[] = "";
+
+/* structure for reading config files */
+struct reading {
+    int errcount;
+    struct tzplatform_context *context;
+    size_t dynvars[_FOREIGN_COUNT_];
+    size_t offsets[_TZPLATFORM_VARIABLES_COUNT_];
+};
+
+/* write the error message */
+static void writerror( const char *format, ...)
+{
+    va_list ap;
+    fprintf( stderr, "tzplatform_config ERROR: ");
+    va_start(ap, format);
+    vfprintf(stderr, format, ap);
+    va_end(ap);
+    fprintf( stderr, "\n");
+}
+
+#if _HAS_IDS_
+/* fill the foreign variables for ids */
+static void foreignid( struct reading *reading)
+{
+    int n;
+    char buffer[50];
+
+#if _FOREIGN_HAS_(UID)
+    /* set the uid */
+    if (reading->dynvars[UID] == HNULL) {
+        n = snprintf( buffer, sizeof buffer, "%d", (int)get_uid(reading->context));
+        if (0 < n && n < (int)(sizeof buffer))
+            reading->dynvars[UID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
+    }
+#endif
+
+#if _FOREIGN_HAS_(EUID)
+    /* set the euid */
+    if (reading->dynvars[EUID] == HNULL) {
+        n = snprintf( buffer, sizeof buffer, "%d", (int)get_euid(reading->context));
+        if (0 < n && n < (int)(sizeof buffer))
+            reading->dynvars[EUID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
+    }
+#endif
+
+#if _FOREIGN_HAS_(GID)
+    /* set the gid */
+    if (reading->dynvars[GID] == HNULL) {
+        n = snprintf( buffer, sizeof buffer, "%d", (int)get_gid(reading->context));
+        if (0 < n && n < (int)(sizeof buffer))
+            reading->dynvars[GID] = heap_strndup( &reading->context->heap, buffer, (size_t)n);
+    }
+#endif
+}
+#endif
+
+#if _HAS_PWS_
+/* fill the foreign variables for home and user */
+static void foreignpw( struct reading *reading)
+{
+    int n = 0;
+    struct pwget *array[3];
+#if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
+    struct pwget uid;
+    char suid[50];
+#endif
+#if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
+    struct pwget euid;
+    char seuid[50];
+#endif
+
+#if _FOREIGN_HAS_(HOME) || _FOREIGN_HAS_(USER)
+    if (
+#if _FOREIGN_HAS_(HOME)
+        reading->dynvars[HOME] == HNULL
+#endif
+#if _FOREIGN_HAS_(HOME) && _FOREIGN_HAS_(USER)
+        ||
+#endif
+#if _FOREIGN_HAS_(USER)
+        reading->dynvars[USER] == HNULL
+#endif
+    ) {
+        snprintf( suid, sizeof suid, "%u", (unsigned)get_uid(reading->context));
+        uid.id = suid;
+        array[n++] = &uid;
+    }
+    else {
+        uid.set = 0;
+    }
+#endif
+
+#if _FOREIGN_HAS_(EHOME) || _FOREIGN_HAS_(EUSER)
+    if (
+#if _FOREIGN_HAS_(EHOME)
+        reading->dynvars[EHOME] == HNULL
+#endif
+#if _FOREIGN_HAS_(EHOME) && _FOREIGN_HAS_(USER)
+        ||
+#endif
+#if _FOREIGN_HAS_(EUSER)
+        reading->dynvars[EUSER] == HNULL
+#endif
+    ) {
+        snprintf( seuid, sizeof seuid, "%u", (unsigned)get_euid(reading->context));
+        euid.id = seuid;
+        array[n++] = &euid;
+    }
+    else {
+        euid.set = 0;
+    }
+#endif
+
+    if (n) {
+        array[n] = NULL;
+        if (pw_get( &reading->context->heap, array) == 0) {
+#if _FOREIGN_HAS_(HOME)
+            if (uid.set)
+                reading->dynvars[HOME] = uid.home;
+#endif
+#if _FOREIGN_HAS_(USER)
+            if (uid.set)
+                reading->dynvars[USER] = uid.user;
+#endif
+#if _FOREIGN_HAS_(EHOME)
+            if (euid.set)
+                reading->dynvars[EHOME] = euid.home;
+#endif
+#if _FOREIGN_HAS_(EUSER)
+            if (euid.set)
+                reading->dynvars[EUSER] = euid.user;
+#endif
+        }
+    }
+}
+#endif
+
+/* get the foreign variable */
+static const char *foreignvar( struct reading *reading, 
+                                            const char *name, size_t length)
+{
+    enum fkey key = foreign( name, length);
+    size_t offset;
+
+    switch (key) {
+#if _HAS_PWS_
+#if _FOREIGN_HAS_(HOME)
+    case HOME:
+#endif
+#if _FOREIGN_HAS_(USER)
+    case USER:
+#endif
+#if _FOREIGN_HAS_(EHOME)
+    case EHOME:
+#endif
+#if _FOREIGN_HAS_(EUSER)
+    case EUSER:
+#endif
+        foreignpw( reading);
+        break;
+#endif
+#if _HAS_IDS_
+#if _FOREIGN_HAS_(UID)
+    case UID:
+#endif
+#if _FOREIGN_HAS_(GID)
+    case GID:
+#endif
+#if _FOREIGN_HAS_(EUID)
+    case EUID:
+#endif
+        foreignid( reading);
+        break;
+#endif
+
+    default:
+        return NULL;
+    }
+    offset = reading->dynvars[key];
+    return offset==HNULL ? NULL : heap_address( &reading->context->heap, offset);
+}
+
+/* callback for parsing errors */
+static int errcb( struct parsing *parsing, 
+            size_t position, const char *message)
+{
+    struct parsinfo info;
+    struct reading *reading = parsing->data;
+
+    /* count the error */
+    reading->errcount++;
+
+    /* print the error */
+    parse_utf8_info( parsing, &info, position);
+    writerror( "%s (file %s line %d)", message, metafilepath, info.lino);
+
+    /* continue to parse */
+    return 0;
+}
+
+/* callback for solving variables */
+static const char *getcb( struct parsing *parsing, 
+            const char *key, size_t length,
+            size_t begin_pos, size_t end_pos)
+{
+    struct parsinfo info;
+    const char *result;
+    size_t offset;
+    struct reading *reading = parsing->data;
+    int id;
+
+    /* try to find a tzplatform variable */
+    id = hashid(key, length);
+    if (id >= 0) {
+        /* found: try to use it */
+        offset = reading->offsets[id];
+        if (offset != HNULL)
+            result = heap_address( &reading->context->heap, offset);
+        else 
+            result = NULL;
+    }
+    else {
+        /* that is a foreign variable */
+        result = foreignvar( reading, key, length);
+    }
+
+    /* emit the error and then return */
+    if (result == NULL) {
+        reading->errcount++;
+        parse_utf8_info( parsing, &info, begin_pos);
+        writerror( "undefined value for %.*s (file %s line %d)",
+            length, key, metafilepath, info.lino);
+        result = emptystring; /* avoid error of the parser */
+    }
+    return result;
+}
+
+/* callback to define variables */
+static int putcb( struct parsing *parsing, 
+            const char *key, size_t key_length, 
+            const char *value, size_t value_length,
+            size_t begin_pos, size_t end_pos)
+{
+    struct parsinfo info;
+    size_t offset;
+    char *string;
+    struct reading *reading = parsing->data;
+    int id;
+
+    /* try to find a tzplatform variable */
+    id = hashid( key, key_length);
+    if (id >= 0) {
+        /* check that the variable isn't already defined */
+        offset = reading->offsets[id];
+        if (offset != HNULL) {
+            reading->errcount++;
+            parse_utf8_info( parsing, &info, begin_pos);
+            writerror( "redefinition of variable %.*s (file %s line %d)",
+                    key_length, key, metafilepath, info.lino);
+        }
+
+        /* allocate the variable value */
+        offset = heap_alloc( &reading->context->heap, value_length+1);
+        if (offset == HNULL) {
+            /* error of allocation */
+            reading->errcount++;
+            writerror( "out of memory");
+        }
+        else {
+            /* record the variable value */
+            reading->offsets[id] = offset;
+            string = heap_address( &reading->context->heap, offset);
+            memcpy( string, value, value_length);
+            string[value_length] = 0;
+        }
+    }
+    else {
+        /* forbidden variable */
+        parse_utf8_info( parsing, &info, begin_pos);
+        writerror( "forbidden variable name %.*s (file %s line %d)",
+            key_length, key, metafilepath, info.lino);
+        
+    }
+
+    /* continue to parse */
+    return 0;
+}
+
+/* initialize the environment */
+void initialize(struct tzplatform_context *context)
+{
+    struct buffer buffer;
+    struct parsing parsing;
+    struct reading reading;
+    size_t offset;
+    int i, result;
+
+    /* clear the variables */
+    reading.errcount = 0;
+    reading.context = context;
+    for (i = 0 ; i < (int)_FOREIGN_COUNT_ ; i++) {
+        reading.dynvars[i] = HNULL;
+    }
+    for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
+        context->values[i] = NULL;
+        reading.offsets[i] = HNULL;
+    }
+
+    /* read the configuration file */
+    result = buffer_create( &buffer, metafilepath);
+    if (result != 0) {
+        writerror( "can't read file %s",metafilepath);
+        context->state = ERROR;
+        return;
+    }
+
+    /* create the heap */
+    result = heap_create( &context->heap, 1);
+    if (result != 0) {
+        buffer_destroy( &buffer);
+        writerror( "out of memory");
+        context->state = ERROR;
+        return;
+    }
+
+    /* read the file */
+    parsing.buffer = buffer.buffer;
+    parsing.length = buffer.length;
+    parsing.maximum_data_size = 0;
+    parsing.should_escape = 0;
+    parsing.data = &reading;
+    parsing.get = getcb;
+    parsing.put = putcb;
+    parsing.error = errcb;
+    result = parse_utf8_config( &parsing);
+    buffer_destroy( &buffer);
+    if (result != 0 || reading.errcount != 0) {
+        writerror( "%d errors while parsing file %s",
+                                            reading.errcount, metafilepath);
+    }
+
+    /* set the variables */
+    heap_read_only( &context->heap);
+    for (i = 0 ; i < (int)_TZPLATFORM_VARIABLES_COUNT_ ; i++) {
+        offset = reading.offsets[i];
+        if (offset != HNULL)
+            context->values[i] = heap_address( &context->heap, offset);
+        else
+            writerror( "the variable %s isn't defined in file %s",
+                keyname(i), metafilepath);
+            /* TODO undefined variable */;
+    }
+    context->state = VALID;
+}
+
diff --git a/src/init.h b/src/init.h
new file mode 100644 (file)
index 0000000..6ed0f15
--- /dev/null
@@ -0,0 +1,30 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef INIT_H
+#define INIT_H
+
+void initialize(struct tzplatform_context *context);
+
+#endif
+
diff --git a/src/isadmin.c b/src/isadmin.c
new file mode 100644 (file)
index 0000000..f62a449
--- /dev/null
@@ -0,0 +1,109 @@
+/*
+ * Copyright (C) 2013 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   Stephen Clymans <stephen.clymans@open.eurogiciel.org>
+ */
+ /*
+ * DISCLAIMER :
+ *  This source file and its associated header are present to maintain a 
+ * temporary solution. We need to know if an user have the privilege for
+ * a particular action.
+ * 
+ * At the end, this feature will be managed by Cynara
+ * 
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <grp.h>
+#include <pwd.h>
+
+#include "isadmin.h"
+#include "tzplatform_variables.h"
+#include "tzplatform_config.h"
+
+int _has_system_group_static_(uid_t uid) {
+       
+       struct passwd pwd, *userinfo = NULL;
+       struct group grp, *systemgroupinfo = NULL;
+       const char *sysgrpname = NULL;
+       char buf[1024];
+       uid_t myuid = 0;
+       gid_t system_gid = 0;
+       gid_t *groups = NULL;
+       int i, nbgroups = 0;
+       
+       
+       if(uid == -1)
+               /* Get current uid */
+               myuid = getuid();
+       else
+               myuid = uid;
+       
+       /* Get the gid of the group named "system" */
+       sysgrpname = tzplatform_getname(TZ_SYS_ADMIN_GROUP);
+       if(sysgrpname == NULL) {
+               fprintf( stderr, "isadmin ERROR: variable TZ_SYS_ADMIN_GROUP is NULL");
+               return -1;
+       }
+       getgrnam_r(sysgrpname, &grp, buf, sizeof(buf), &systemgroupinfo);
+       if(systemgroupinfo == NULL) {
+               fprintf( stderr, "isadmin ERROR: cannot find group named \"%s\"\n", sysgrpname);
+               return -1;
+       }
+       
+       system_gid = systemgroupinfo->gr_gid;
+       
+       /* Get all the gid of the given uid */
+       
+       getpwuid_r(myuid, &pwd, buf, sizeof(buf), &userinfo);
+       
+       /* Need to call this function now to get the number of group to make the
+          malloc correctly sized */
+       if (getgrouplist(userinfo->pw_name, userinfo->pw_gid, groups, &nbgroups) != -1) {
+               fprintf( stderr, "isadmin ERROR: cannot get number of groups\n");
+               return -1;
+       }
+       
+       groups = malloc(nbgroups * sizeof (gid_t));
+       if (groups == NULL) {
+               fprintf( stderr, "isadmin ERROR: malloc cannot allocate memory\n");
+               return -1;
+       }
+       
+       if (getgrouplist(userinfo->pw_name, userinfo->pw_gid, groups, &nbgroups) == -1) {
+               free(groups);
+               fprintf( stderr, "isadmin ERROR: cannot get groups\n");
+               return -1;
+       }
+       
+       /* Check if the given uid is in the system group */
+       
+       for(i = 0 ; i < nbgroups ; i++) {
+               if(groups[i] == system_gid) {
+                       free(groups);
+                       return 1;
+               }
+       }
+       free(groups);
+       return 0;
+}
+
diff --git a/src/isadmin.h b/src/isadmin.h
new file mode 100644 (file)
index 0000000..25ebd75
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2013 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   Stephen Clymans <stephen.clymans@open.eurogiciel.org>
+ */
+#ifdef __cplusplus
+extern "C" {
+#endif
+#ifndef TIZEN_PLATFORM_WRAPPER_ISADMIN_H
+#define TIZEN_PLATFORM_WRAPPER_ISADMIN_H
+
+/*
+ * DISCLAIMER :
+ *  This header and its associated source file are present to maintain a 
+ * temporary solution. We need to know if an user have the privilege for
+ * a particular action.
+ * 
+ * At the end, this feature will be managed by Cynara
+ * 
+ */
+
+/*
+ * Return 0 if the given uid is not in the admin group.
+ * Return 1 if the given uid is in the admin group.
+ * 
+ * If you pass the -1 value to this function it will take the current uid given
+ * by the POSIX function getuid();
+*/
+int _has_system_group_static_(uid_t uid);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* TIZEN_PLATFORM_WRAPPER_ISADMIN_H  */
+
diff --git a/src/parser.c b/src/parser.c
new file mode 100644 (file)
index 0000000..fa8d94d
--- /dev/null
@@ -0,0 +1,319 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <alloca.h>
+#include <ctype.h>
+
+#include "parser.h"
+
+#ifndef MINIMUM_DATA_SIZE
+# define MINIMUM_DATA_SIZE             128
+#endif
+#ifndef DEFAULT_MAXIMUM_DATA_SIZE
+# define DEFAULT_MAXIMUM_DATA_SIZE   32768
+#endif
+
+#ifndef _
+# define _(x) x
+#endif
+
+int parse_utf8_config( struct parsing *parsing)
+{
+    char c, q, acc, overflow, escape;
+    char *bdata;
+    const char *value, *head, *end, *skey, *svar, *bvar;
+    size_t lkey, lvar, ldata, datasz;
+    int  errors;
+
+#define iskeybeg(x)     (isalpha(x)||(x)=='_')
+#define iskey(x)        (isalnum(x)||(x)=='_')
+#define atend           ((head-end) >= 0)
+
+#define pos(x)          ((size_t)((x)-parsing->buffer))
+#define error(p,x)      do{ errors++; \
+                            if(parsing->error \
+                                && !parsing->error(parsing,pos(p),x)) \
+                                goto stop; \
+                        }while(0)
+
+    /* alloc data buffer */
+    datasz = parsing->maximum_data_size;
+    if (datasz == 0)
+        datasz = DEFAULT_MAXIMUM_DATA_SIZE;
+    else if (datasz < MINIMUM_DATA_SIZE)
+        datasz = MINIMUM_DATA_SIZE;
+    bdata = alloca(datasz);
+
+    /* init */
+    escape = parsing->should_escape ? 1 : 0;
+    errors = 0;
+    head = parsing->buffer;
+    end = head + parsing->length;
+    goto initial;
+
+next_initial:
+
+    head++;
+
+initial: /* expecting key, comment or ??? */
+
+    if (atend)
+        goto end_ok;
+
+    c = *head;
+
+    if (iskeybeg(c)) {
+        skey = head;
+        goto key;
+    }
+
+    if (c == '#')
+        goto comment;
+
+    if (!isspace(c)) 
+        error(head,_("unexpected character while looking to a key start"));
+
+    goto next_initial;
+
+comment: /* skipping a comment */
+
+    head++;
+    if (atend)
+        goto end_ok;
+    c = *head;
+    if (c == '\n')
+        goto next_initial;
+    goto comment;
+
+key: /* reading a key */
+
+    head++;
+    if (atend) {
+        error(head,_("end in a key"));
+        goto stop;
+    }
+    c = *head;
+    if (iskey(c))
+        goto key;
+    if (c != '=') {
+        error(head,_("unexpected character while looking to ="));
+        goto next_initial;
+    }
+    lkey = head - skey;
+    overflow = ldata = 0;
+    q = 0;
+    goto next_value;
+
+escapable:
+
+    if (escape && ldata < datasz)
+        bdata[ldata++] = '\\';
+
+add_value: /* add the current char to data */
+
+    if (ldata < datasz)
+        bdata[ldata++] = c;
+    else
+        overflow = 1;
+
+next_value:
+
+    head++;
+
+value: /* reading the value */
+
+    if (atend) {
+        /* end in the value */
+        if (!q)
+            goto end_of_value;
+
+        error(head,_("end in quoted value"));
+        goto stop;
+    }
+
+    c = *head;
+    switch (c) {
+
+    case '\\':
+        if (q == '\'')
+            goto escapable;
+        ++head;
+        if (atend) {
+            error(head,_("end after escape char"));
+            goto stop;
+        }
+        c = *head;
+        goto escapable;
+
+    case '\'':
+        if (q == '"')
+            goto escapable;
+        q = q ^ c;
+        goto next_value;
+
+    case '"':
+        if (q == '\'')
+            goto escapable;
+        q = q ^ c;
+        goto next_value;
+
+    case '$':
+        if (q == '\'')
+            goto escapable;
+
+        /* begin of a variable, just after $ */
+        bvar = head++;
+        if (atend) {
+            error(head,_("end after $"));
+            goto stop;
+        }
+        c = *head;
+        acc = c;
+        if (c == '{') {
+            ++head;
+            if (atend) {
+                error(head,_("end after ${"));
+                goto stop;
+            }
+            c = *head;
+        }
+        if (!iskeybeg(c)) {
+            error(head,_("invalid character after $ or ${"));
+            goto value;
+        }
+        svar = head;
+        goto variable;
+
+    default:
+        if (q || !isspace(c)) 
+            goto add_value;
+
+        goto end_of_value;
+    }
+
+end_of_value: /* end of the value */
+
+    if (overflow)
+        error(head,_("value too big"));
+    else if (parsing->put)
+        parsing->put( parsing, skey, lkey, bdata, ldata, pos(skey), pos(head));
+    if (atend)
+        goto initial;
+    goto next_initial;
+
+variable: /* read a variable */
+
+    ++head;
+    if (atend) {
+        if (acc == '{')
+            error(head,_("unmatched pair { }"));
+        lvar = head - svar;
+    }
+    else {
+        c = *head;
+        if (iskey(c))
+            goto variable;
+        lvar = head - svar;
+        if (acc == '{') {
+            if (c == '}')
+                ++head;
+            else
+                error(head,_("unmatched pair { }"));
+        }
+    }
+    if (parsing->get) {
+        value = parsing->get( parsing, svar,lvar,pos(bvar),pos(head));
+        if (value == NULL)
+            error(bvar,_("no value for the variable"));
+        else {
+            while (*value) {
+                if (ldata < datasz)
+                    bdata[ldata++] = *value++;
+                else {
+                    overflow = 1;
+                    break;
+                }
+            }
+        }
+    }
+    goto value;
+
+stop:
+end_ok:
+    return -errors;
+    
+#undef error
+#undef pos
+#undef atend
+#undef iskey
+#undef iskeybeg
+}
+
+void parse_utf8_info(
+            struct parsing *parsing,
+            struct parsinfo *info,
+            size_t pos
+)
+{
+    const char *buf, *begin, *end;
+    size_t length;
+    int lino, colno;
+
+    /* init */
+    lino = 1, colno = 1;
+    buf = begin = end = parsing->buffer;
+    length = parsing->length;
+    if (length < pos)
+        pos = length;
+
+    /* search the begin of the line */
+    while ((end - buf) != pos) {
+        /* dealing utf8 */
+        colno += ((*end & '\xc0') != '\x80');
+        /* dealing with lines */
+        if(*end++ == '\n') { 
+            begin = end; 
+            lino++;
+            colno = 1;
+        }
+    }
+
+    /* search the end of the line */
+    while ((end - buf) < length && *end != '\n') 
+        end++;
+
+    /* record computed values */
+    info->begin = begin;
+    info->end = end;
+    info->length = end - begin;
+    info->lino = lino;
+    info->colno = colno;
+}
+
diff --git a/src/parser.h b/src/parser.h
new file mode 100644 (file)
index 0000000..0ee30e8
--- /dev/null
@@ -0,0 +1,112 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef TIZEN_PLATFORM_WRAPPER_PARSER_H
+#define TIZEN_PLATFORM_WRAPPER_PARSER_H
+
+/* structure used for parsing config files */
+struct parsing {
+
+    /* The buffer to parse. */
+    const char *buffer; 
+
+    /* The length of the buffer to parse. */
+    size_t length; 
+
+    /* The maximum data size allowed */
+    size_t maximum_data_size;
+
+    /* Some user data, please use it as you wish. */
+    void * data;
+
+    /* Should escape the key values (inserting \ where needed) */
+    int should_escape;
+
+    /*
+      Callback function to resolve the variables.
+      Should return the value of the variable of 'key' that
+      length is 'length' without terminating zero.
+    */
+    const char *(*get)( struct parsing *parsing, 
+                const char *key, size_t length,
+                size_t begin_pos, size_t end_pos);
+
+    /*
+      Callback function to receive new values. 
+      Should add/insert/replace the key/value pair
+      given. This values aren't zero terminated.
+      The given length is the one without terminating nul.
+    */
+    int (*put)( struct parsing *parsing, 
+                const char *key, size_t key_length, 
+                const char *value, size_t value_length,
+                size_t begin_pos, size_t end_pos);
+
+    /*
+      Callback function to report errors.
+      'buffer' is the scanned buffer.
+      'position' is the position of the character raising the error.
+      'message' is a short explanation of the error.
+      Should return 0 to stop parsing;
+    */
+    int (*error)( struct parsing *parsing, 
+                size_t position, const char *message);
+};
+
+/*
+   Parse the config file using data of 'parsing'. 
+   Return 0 if not error found or a negative number
+   corresponding to the opposite of the count of found errors 
+   (ex: -5 means 5 errors).
+   Note: works on utf8 data.
+*/
+int parse_utf8_config(
+    struct parsing *parsing
+);
+
+/* Structure for getting information about a position. */
+struct parsinfo {
+    const char *begin; /* pointer to the first char of the line */
+    const char *end;   /* pointer to the char just after the end of the line */
+    size_t length;     /* length of the line (== end-begin) */ 
+    int lino;          /* number of the line within the buffer */
+    int colno;         /* number of the column within the line */
+};
+
+/*
+  Fill the data of 'info' for the current position 'pos' that is an offset
+  into the buffer of 'parsing'.
+  This function computes into info the pointers of the line containig the
+  char of offset 'pos', the number of this line and the column number of
+  the position within the line.
+  Note: works on utf8 data.
+*/
+void parse_utf8_info(
+    struct parsing *parsing,
+    struct parsinfo *info,
+    size_t pos
+);
+
+
+#endif
+
diff --git a/src/passwd.c b/src/passwd.c
new file mode 100644 (file)
index 0000000..287d375
--- /dev/null
@@ -0,0 +1,304 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <errno.h>
+
+#include "heap.h"
+#include "passwd.h"
+
+#ifndef NOT_PASSWD_ONLY
+
+#include "buffer.h"
+
+/* index of fields */
+enum { iname, ipasswd, iuid, igid, icmt, idir, ishell };
+
+static const char pwfile[] = "/etc/passwd";
+static struct buffer buffer;
+static size_t pos, lengths[7];
+static const char *starts[7];
+
+#define is(s,i) (!strncmp(s,starts[i],lengths[i]) && !s[lengths[i]])
+
+/* open the passwd file */
+static int oppw()
+{
+    pos = 0;
+    return buffer_create( &buffer, pwfile);
+}
+
+/* close the passwd file */
+static void clpw()
+{
+    buffer_destroy( &buffer);
+}
+
+/* read the passwd file */
+static int rdpw()
+{
+    int col = 0;
+    const char *head = buffer.buffer + pos;
+    const char *end = buffer.buffer + buffer.length;
+    while (head != end) {
+        starts[col] = head;
+        while(head!=end && *head!=':' && *head!='\n') head++;
+        lengths[col] = head - starts[col];
+        col++;
+        if (col == 7) {
+            if (head==end) {
+                pos = head - buffer.buffer;
+                return 1;
+            }
+            if (*head=='\n') {
+                head++;
+                pos = head - buffer.buffer;
+                return 1;
+            }
+            while (head!=end && *head!='\n') head++;
+            if (head!=end) head++;
+            col = 0;
+        }
+        else {
+            if (head != end) {
+                if (*head++=='\n') 
+                    col = 0;
+            }
+        }
+    }
+    pos = head - buffer.buffer;
+    return 0;
+}
+
+int pw_get( struct heap *heap, struct pwget **items)
+{
+    size_t user, home;
+    int result;
+    int i, n, s;
+
+    for( n = 0 ; items[n] != NULL ; n++ )
+        items[n]->set = 0;
+
+    result = oppw();
+    if (result == 0) {
+        s = n;
+        while (s && rdpw()) {
+            user = home = HNULL;
+            for( i = 0 ; i < n ; i++ ) {
+                if (!items[i]->set && is(items[i]->id,iuid)) {
+                    if (user==HNULL) {
+                        user = heap_strndup( heap,
+                                            starts[iname], lengths[iname]);
+                        home = heap_strndup( heap,
+                                            starts[idir], lengths[idir]);
+                    }
+                    items[i]->set = 1;
+                    items[i]->user = user;
+                    items[i]->home = home;
+                    s--;
+                }
+            }
+        }
+        clpw();
+    }
+    return result;
+}
+
+int pw_has_uid( uid_t uid)
+{
+    if (oppw() == 0) {
+        while (rdpw()) {
+            if (lengths[iuid] && (int)uid == atoi(starts[iuid])) {
+                clpw();
+                return 1;
+            }
+        }
+        clpw();
+    }
+    return 0;
+}
+
+int pw_get_uid( const char *name, uid_t *uid)
+{
+    int result = oppw();
+    if (result == 0) {
+        while (rdpw()) {
+            if (is(name,iname)) {
+                *uid = (uid_t)atoi(starts[iuid]);
+                clpw();
+                return 0;
+            }
+        }
+        clpw();
+        result = -1;
+        errno = EEXIST;
+    }
+    return result;
+}
+
+int pw_get_gid( const char *name, gid_t *gid)
+{
+    int result = oppw();
+    if (result == 0) {
+        while (rdpw()) {
+            if (is(name,iname)) {
+                *gid = (gid_t)atoi(starts[igid]);
+                clpw();
+                return 0;
+            }
+        }
+        clpw();
+        result = -1;
+        errno = EEXIST;
+    }
+    return result;
+}
+
+#else
+
+#include <pwd.h>
+
+#define BUFSIZE  4096
+
+int pw_get( struct heap *heap, struct pwget **items)
+{
+    char buffer[BUFSIZE];
+    struct passwd entry, *pe;
+    int result;
+    int n;
+    uid_t id;
+
+    for( n = 0 ; items[n] != NULL ; n++ ) {
+        id = (uid_t)atoi(items[n]->id);
+        result = getpwuid_r( id, &entry, buffer, sizeof buffer, &pe);
+        if (result != 0) {
+            while(items[n] != NULL) items[n++]->set = 0;
+            return result;
+        }
+        if (pe == NULL) 
+            items[n]->set = 0;
+        else {
+            items[n]->set = 1;
+            items[n]->user = heap_strdup( heap, pe->pw_name);
+            items[n]->home = heap_strdup( heap, pe->pw_dir);
+        }
+    }
+    return 0;
+}
+
+int pw_has_uid( uid_t uid)
+{
+    char buffer[BUFSIZE];
+    struct passwd entry, *pe;
+    int result;
+
+    result = getpwuid_r( uid, &entry, buffer, sizeof buffer, &pe);
+    return !result && pe;
+}
+
+int pw_get_uid( const char *name, uid_t *uid)
+{
+    char buffer[BUFSIZE];
+    struct passwd entry, *pe;
+    int result = getpwnam_r( name, &entry, buffer, sizeof buffer, &pe);
+    if (result == 0) {
+        if (pe == NULL) {
+            errno = EEXIST;
+            result = -1;
+        }
+        else {
+            *uid = entry.pw_uid;
+        }
+    }
+    return result;
+}
+
+int pw_get_gid( const char *name, gid_t *gid)
+{
+    char buffer[BUFSIZE];
+    struct passwd entry, *pe;
+    int result = getpwnam_r( name, &entry, buffer, sizeof buffer, &pe);
+    if (result == 0) {
+        if (pe == NULL) {
+            errno = EEXIST;
+            result = -1;
+        }
+        else {
+            *gid = entry.pw_gid;
+        }
+    }
+    return result;
+}
+
+#endif
+
+#ifdef TEST_PASSWD
+#include <stdio.h>
+int main(int argc,char**argv) {
+    struct heap heap;
+    enum { uid, gid, names } action = uid;
+    heap_create(&heap,1000);
+    while(*++argv) {
+        if (!strcmp(*argv,"-u"))
+            action = uid;
+        else if (!strcmp(*argv,"-g"))
+            action = gid;
+        else if (!strcmp(*argv,"-n"))
+            action = names;
+        else if (action == uid) {
+            uid_t u = 0;
+            int sts = pw_get_uid( *argv, &u);
+            printf("uid(%s)=%d [%d]\n",*argv,(int)u,sts);
+        }
+        else if (action == gid) {
+            gid_t g = 0;
+            int sts = pw_get_gid( *argv, &g);
+            printf("gid(%s)=%d [%d]\n",*argv,(int)g,sts);
+        }
+        else if (action == names) {
+            struct pwget pw = { .id=*argv }, *ppw[2] = { &pw, NULL };
+            int sts = pw_get( &heap, ppw);
+            printf("names(%s) set=%d",*argv,pw.set);
+            if (pw.set) {
+                printf(" name=%s home=%s",
+                    pw.user==HNULL ? "(null)" 
+                                    : (char*)heap_address( &heap, pw.user),
+                    pw.home==HNULL ? "(null)" 
+                                    : (char*)heap_address( &heap, pw.home));
+            }
+            printf(" [%d]\n",sts);
+        }
+    }
+    return 0;
+}
+#endif
+
+
diff --git a/src/passwd.h b/src/passwd.h
new file mode 100644 (file)
index 0000000..f9de75c
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef TIZEN_PLATFORM_WRAPPER_PASSWD_H
+#define TIZEN_PLATFORM_WRAPPER_PASSWD_H
+
+struct pwget {
+    int set;
+    const char *id;
+    size_t user;
+    size_t home;
+};
+
+int pw_get( struct heap *heap, struct pwget **items);
+int pw_get_uid( const char *name, uid_t *uid);
+int pw_get_gid( const char *name, gid_t *gid);
+int pw_has_uid( uid_t uid);
+
+#endif
+
diff --git a/src/scratch.c b/src/scratch.c
new file mode 100644 (file)
index 0000000..6f25b54
--- /dev/null
@@ -0,0 +1,238 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <memory.h>
+
+#ifndef INITIAL_SCRATCH_CAPACITY
+#define INITIAL_SCRATCH_CAPACITY  240
+#endif
+
+#if INITIAL_SCRATCH_CAPACITY <= 0
+#error "bad value for INITIAL_SCRATCH_CAPACITY"
+#endif
+
+#define INSTANCIATE 1
+#if INSTANCIATE
+#define SET_CAPACITY        97
+#define HASHCODE_INIT       5381
+#define HASHCODE_NEXT(H,C)  (((H) << 5) + (H) + (C))
+#endif
+
+#ifndef NOT_MULTI_THREAD_SAFE
+#include <pthread.h>
+#endif
+
+#if !(defined(NOT_MULTI_THREAD_SAFE) || INSTANCIATE)
+static __thread void *global_scratch = NULL;
+#else
+static void *global_scratch = NULL;
+#endif
+#if INSTANCIATE && !defined(NOT_MULTI_THREAD_SAFE)
+static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
+#endif
+
+#if INSTANCIATE
+/* structure for recording items in the hash map */
+struct set_item {
+    struct set_item *next;        /* chain to next item */
+    size_t hashcode;                /* hash of the string */
+    size_t length;                /* length of the string including null */
+};
+
+/* the array of recorded strings */
+static struct set_item *global_path_set[SET_CAPACITY]; /* initialized to  zeros */
+
+/* instanciate (or retrieve an instance) of the 'string' that
+is granted to have the 'length' including terminating null and a
+hash code 'hashcode' */
+static const char *instantiate(const char *string, size_t length, size_t hashcode)
+{
+    struct set_item **pp, *item;
+    char *result;
+
+    /* get first item in the table */
+    pp = &global_path_set[hashcode % SET_CAPACITY];
+    result = 0;
+    do {
+        /* inspect the item */
+        item = *pp;
+        if (!item) {
+            /* no item: create it */
+            item = malloc(length + sizeof * item);
+            if (!item)
+                return NULL;
+            /* init it */
+            item->next = 0;
+            item->hashcode = hashcode;
+            item->length = length;
+            result = (char *)(item + 1);
+            memcpy(result, string, length);
+            /* record it */
+            *pp = item;
+        } else if (item->hashcode == hashcode
+                && item->length == length
+                && 0 == strcmp(string, (const char *)(item + 1))) {
+            /* item found */
+            result = (char *)(item + 1);
+        } else {
+            /* try the next */
+            pp = &item->next;
+        }
+    } while (!result);
+
+    return result;
+}
+#endif
+
+/* CAUTION: in a multitheaded context, it is expected that
+=========== the function scratchcat is call under a mutex. 
+If it is not the case please check for initializing 'tlskey' 
+only one time before use of it. */
+
+#if INSTANCIATE && !defined(NOT_MULTI_THREAD_SAFE)
+static const char *_scratchcat( int ispath, const char **strings);
+
+const char *scratchcat( int ispath, const char **strings)
+{
+    const char *result;
+    pthread_mutex_lock(&mutex);
+    result = _scratchcat( ispath, strings);
+    pthread_mutex_unlock(&mutex);
+    return result;
+}
+
+static const char *_scratchcat( int ispath, const char **strings)
+#else
+const char *scratchcat( int ispath, const char **strings)
+#endif
+{
+    void *scratch, *p;
+    char *result;
+    size_t length, capacity;
+    const char *instr;
+    char c, pc;
+#if INSTANCIATE
+    size_t hashcode = HASHCODE_INIT;
+#endif
+
+    /* get the recorded pointer on scrtch area */
+    scratch = global_scratch;
+
+    /* create the scratch area if needed */
+    if (scratch == NULL) {
+        capacity = INITIAL_SCRATCH_CAPACITY;
+        p = malloc( capacity + sizeof(size_t));
+        if (p == NULL)
+            return NULL;
+        *((size_t*)p) = capacity;
+        scratch = p;
+        global_scratch = p;
+    }
+
+    /* set local data for scratch area */
+    capacity = *((size_t*)scratch);
+    result = (char*)(1+((size_t*)scratch));
+    length = 0;
+
+    /* copy the strings */
+    c = 0;
+    pc = 1;
+    while(pc) {
+
+        if (c == 0) {
+            instr = *strings++;
+            if (instr != NULL) {
+                c = *instr;
+                if (c == 0)
+                    continue;
+                if (!ispath)
+                    instr++;
+                else if(c != '/' && pc != '/')
+                    c = '/';
+                else if(c == '/' && pc == '/') {
+                    instr++;
+                    continue;
+                }
+                else
+                    instr++;
+            }
+        }
+        else {
+            c = *instr;
+            if (c == 0)
+                continue;
+            instr++;
+        }
+
+        /* extend the scratch area if needed */
+        if (length == capacity) {
+            capacity = 2 * capacity;
+            p = realloc( scratch, capacity + sizeof(size_t));
+            if (p == NULL)
+                return NULL;
+            *((size_t*)p) = capacity;
+            if (p != scratch) {
+                scratch = p;
+                global_scratch = p;
+                result = (char*)(1+((size_t*)p));
+            }
+        }
+
+        /* append the char */
+        pc = result[length++] = c;
+#if INSTANCIATE
+        hashcode = HASHCODE_NEXT(hashcode, (size_t)c);
+#endif
+    }
+
+#if INSTANCIATE
+    return instantiate(result, length, hashcode);
+#else
+    return result;
+#endif
+}
+
+
+#ifdef TEST_SCRATCH
+#include <stdio.h>
+int main(int argc, const char**argv) {
+    int ispath, iter, i;
+    argv++;
+    ispath = argv[0] && argv[0][0] == '-' && argv[0][1] == 'p';
+    for (i = 0 ; i < 2 ; i++) {
+        iter = ispath;
+        while (iter < argc) {
+            const char *p = scratchcat(ispath,argv+iter++);
+            printf("%p: %s\n",p,p);
+        }
+    }
+    return 0;
+}
+#endif
diff --git a/src/scratch.h b/src/scratch.h
new file mode 100644 (file)
index 0000000..3b15bcb
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef TIZEN_PLATFORM_WRAPPER_SCRATCH_H
+#define TIZEN_PLATFORM_WRAPPER_SCRATCH_H
+
+/*
+ Return a scratch buffer containing the concatenation of the strings of the
+ array 'strings'. If 'ispath' isn't zero (then is "true") the directory
+ separator is inserted between the strings. The last item of 'strings' must
+ be NULL to indicate the end.
+
+ The returned value is a scratch buffer (unique for the thread) that is
+ available until the next call.
+
+ Can return NULL in case of internal error (memory depletion).
+
+ Example:
+
+    char *array[] = { "hello", " ", "world", "!", NULL };
+
+    scratchcat( 0, array) will return "hello world!"
+
+    scratchcat( 1, array) will return "hello/ /world/!"
+*/
+const char *scratchcat( int ispath, const char **strings);
+
+
+#endif
+
diff --git a/src/sha256sum.c b/src/sha256sum.c
new file mode 100644 (file)
index 0000000..9b399d1
--- /dev/null
@@ -0,0 +1,250 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+#include <fcntl.h>
+#include <string.h>
+
+#include "sha256sum.h"
+
+static const char *arg[2] = { "/usr/bin/sha256sum", NULL };
+
+struct sha256sum {
+    enum { RUNNING, FAILED, SUCCESSED } state;
+    pid_t pid;
+    int tofd;
+    int fromfd;
+    char result[32];
+};
+
+struct sha256sum *sha256sum_create()
+{
+    int fds1[2];
+    int fds2[2];
+    pid_t pid;
+    int sts;
+    struct sha256sum *result;
+
+    result = malloc(sizeof * result);
+    if (result == NULL)
+        return NULL;
+
+    sts = pipe(fds1);
+    if (sts != 0) {
+        free(result);
+        return NULL;
+    }
+
+    sts = pipe(fds2);
+    if (sts != 0) {
+        close( fds1[0]);
+        close( fds1[1]);
+        free(result);
+        return NULL;
+    }
+
+    pid = fork();
+    if (pid == -1) {
+        close( fds1[0]);
+        close( fds1[1]);
+        close( fds2[0]);
+        close( fds2[1]);
+        free(result);
+        return NULL;
+    }
+
+    if (pid == 0) {
+        dup2( fds1[0], 0);
+        dup2( fds2[1], 1);
+        close( fds1[0]);
+        close( fds1[1]);
+        close( fds2[0]);
+        close( fds2[1]);
+        execve( arg[0], (char**)arg, environ);
+        exit(1);
+    }
+
+    close( fds1[0]);
+    close( fds2[1]);
+    result->state = RUNNING;
+    result->pid = pid;
+    result->tofd = fds1[1];
+    result->fromfd = fds2[0];
+    return result;
+}
+
+void sha256sum_destroy(struct sha256sum *s)
+{
+    int sts;
+
+    if (s->state == RUNNING) {
+        close(s->fromfd);
+        close(s->tofd);
+        waitpid(s->pid, &sts, 0);
+    }
+    free(s);
+}
+
+int sha256sum_add_data(struct sha256sum *s, const void *data, size_t length)
+{
+    ssize_t sl;
+    int sts;
+
+    if (s->state != RUNNING)
+        return -1;
+
+    while (length) {
+        do {
+            sl = write(s->tofd, data, length);
+        } while (sl == -1 && (errno == EINTR || errno == EAGAIN));
+        if (sl == -1) {
+            s->state = FAILED;
+            close(s->fromfd);
+            close(s->tofd);
+            waitpid(s->pid, &sts, 0);
+            return -1;
+        }
+        length -= (size_t)sl;
+        data = (const void*)((const char*)data + (size_t)sl);
+    }
+    return 0;
+}
+
+int sha256sum_add_file(struct sha256sum *s, const char *filename)
+{
+    char buffer[16384];
+    int fd;
+    ssize_t rd;
+
+    fd = open(filename, O_RDONLY);
+    if (fd < 0)
+        return -1;
+
+    for (;;) {
+        do {
+            rd = read(fd, buffer, sizeof buffer);
+        } while (rd == -1 && (errno == EINTR || errno == EAGAIN));
+        if (rd == -1) {
+            close(fd);
+            return -1;
+        }
+        if (rd == 0) {
+            close(fd);
+            return 0;
+        }
+        if (sha256sum_add_data(s, buffer, (size_t)rd)) {
+            close(fd);
+            return -1;
+        }
+    }
+}
+
+int sha256sum_get(struct sha256sum *s, char result[32])
+{
+    int sts;
+    int j;
+    char buffer[65];
+    char c1;
+    char c2;
+
+    if (s->state == RUNNING) {
+        s->state = FAILED;
+        close(s->tofd);
+        waitpid(s->pid, &sts, 0);
+        if (WIFEXITED(sts) && WEXITSTATUS(sts) == 0) {
+            sts = read(s->fromfd, buffer, 65);
+            if (sts == 65 && buffer[64] == ' ') {
+                s->state = SUCCESSED;
+                for (j = 0 ; j < 32 ; j++) {
+                    c1 = buffer[j+j];
+                    if ('0' <= c1 && c1 <= '9')
+                        c1 = c1 - '0';
+                    else if ('a' <= c1 && c1 <= 'f')
+                        c1 = c1 - 'a' + '\012';
+                    else if ('A' <= c1 && c1 <= 'F')
+                        c1 = c1 - 'A' + '\012';
+                    else {
+                        s->state = FAILED;
+                        break;
+                    }
+                    c2 = buffer[j+j+1];
+                    if ('0' <= c2 && c2 <= '9')
+                        c2 = c2 - '0';
+                    else if ('a' <= c2 && c2 <= 'f')
+                        c2 = c2 - 'a' + '\012';
+                    else if ('A' <= c2 && c2 <= 'F')
+                        c2 = c2 - 'A' + '\012';
+                    else {
+                        s->state = FAILED;
+                        break;
+                    }
+                    s->result[j] = (c1 << 4) | c2;
+                }
+            }
+        }
+        close(s->fromfd);
+    }
+
+    if (s->state == FAILED)
+        return -1;
+
+    memcpy(result, s->result, 32);
+
+    return 0;
+}
+
+#ifdef TEST
+#include <stdio.h>
+#include <assert.h>
+int main(int argc, char **argv)
+{
+    char sum[32];
+    int j, r;
+    struct sha256sum *s;
+
+    while (*++argv) {
+        s = sha256sum_create();
+        assert(s != NULL);
+        r = sha256sum_add_file(s, *argv);
+        assert(r == 0);
+        r = sha256sum_get(s, sum);
+        assert(r == 0);
+        sha256sum_destroy(s);
+        for (j=0 ; j < 32 ; j++)
+            printf("%02x", (int)(unsigned char)sum[j]);
+        printf("  %s\n", *argv);
+    }
+    return 0;
+}
+#endif
+
diff --git a/src/sha256sum.h b/src/sha256sum.h
new file mode 100644 (file)
index 0000000..9494594
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef SHA256SUM_H
+#define SHA256SUM_H
+
+struct sha256sum;
+
+struct sha256sum *sha256sum_create();
+void sha256sum_destroy(struct sha256sum *s);
+int sha256sum_add_data(struct sha256sum *s, const void *data, size_t length);
+int sha256sum_add_file(struct sha256sum *s, const char *filename);
+int sha256sum_get(struct sha256sum *s, char result[32]);
+
+#endif
diff --git a/src/shared-api.c b/src/shared-api.c
new file mode 100644 (file)
index 0000000..742e69a
--- /dev/null
@@ -0,0 +1,386 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <assert.h>
+#include <syslog.h>
+
+#ifndef NOT_MULTI_THREAD_SAFE
+#include <pthread.h>
+#endif
+
+#include "tzplatform_variables.h"
+#include "tzplatform_config.h"
+#include "heap.h"
+#include "scratch.h"
+#include "passwd.h"
+#include "foreign.h"
+#include "context.h"
+#include "hashing.h"
+#include "init.h"
+#include "shared-api.h"
+
+
+/* the global context */
+static struct tzplatform_context global_context = {
+#ifndef NOT_MULTI_THREAD_SAFE
+    .mutex = PTHREAD_MUTEX_INITIALIZER,
+#endif
+    .state = RESET,
+    .user = _USER_NOT_SET_
+};
+
+/* the signup of names */
+#include "signup.inc"
+
+/* validate the signup */
+static void validate_signup(char signup[33])
+{
+    if (memcmp(signup+1, tizen_platform_config_signup+1, 32)) {
+        syslog(LOG_CRIT, "Bad signup of the client of tizen-platform-config");
+        abort();
+    }
+    signup[0] = 1;
+}
+
+/* check the signup */
+static inline void check_signup(char signup[33])
+{
+    if (!signup[0])
+        validate_signup(signup);
+}
+
+/* locks the context */
+inline static void lock(struct tzplatform_context *context)
+{
+#ifndef NOT_MULTI_THREAD_SAFE
+    pthread_mutex_lock( &context->mutex);
+#endif
+}
+
+/* unlock the context */
+inline static void unlock(struct tzplatform_context *context)
+{
+#ifndef NOT_MULTI_THREAD_SAFE
+    pthread_mutex_unlock( &context->mutex);
+#endif
+}
+
+static inline const char *get_lock(int id, struct tzplatform_context *context)
+{
+    lock( context);
+
+    if (id < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= id)
+        return NULL;
+
+    if (context->state == RESET)
+        initialize( context);
+
+    return context->state == ERROR ? NULL : context->values[id];
+}
+
+/*************** PUBLIC API begins here **************/
+
+int tzplatform_context_create(struct tzplatform_context **result)
+{
+    struct tzplatform_context *context;
+
+    context = malloc( sizeof * context);
+    *result = context;
+    if (context == NULL)
+        return -1;
+
+    context->state = RESET;
+    context->user = _USER_NOT_SET_;
+#ifndef NOT_MULTI_THREAD_SAFE
+    pthread_mutex_init( &context->mutex, NULL);
+#endif
+    return 0;
+}
+
+void tzplatform_context_destroy(struct tzplatform_context *context)
+{
+    if (context->state == VALID)
+            heap_destroy( &context->heap);
+    context->state = ERROR;
+    free( context);
+}
+
+void tzplatform_reset()
+{
+    tzplatform_context_reset(&global_context);
+}
+
+void tzplatform_context_reset(struct tzplatform_context *context)
+{
+    lock( context);
+    if (context->state != RESET) {
+        if (context->state == VALID)
+            heap_destroy( &context->heap);
+        context->state = RESET;
+    }
+    unlock( context);
+}
+
+uid_t tzplatform_get_user(char signup[33])
+{
+    return tzplatform_context_get_user( &global_context);
+}
+
+uid_t tzplatform_context_get_user(struct tzplatform_context *context)
+{
+    uid_t result;
+
+    lock( context);
+    result = get_uid( context);
+    unlock( context);
+
+    return result;
+}
+
+void tzplatform_reset_user()
+{
+    tzplatform_context_reset_user( &global_context);
+}
+
+void tzplatform_context_reset_user(struct tzplatform_context *context)
+{
+    tzplatform_context_set_user( context, _USER_NOT_SET_);
+}
+
+int tzplatform_set_user(uid_t uid)
+{
+    return tzplatform_context_set_user( &global_context, uid);
+}
+
+int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid)
+{
+    lock( context);
+    if (context->user != uid) {
+        if (uid != _USER_NOT_SET_ && !pw_has_uid( uid)) {
+            unlock( context);
+            return -1;
+       }
+        else {
+            if (context->state == VALID)
+                heap_destroy( &context->heap);
+            context->state = RESET;
+            context->user = uid;
+        }
+    }
+    unlock( context);
+
+    return 0;
+}
+
+/*************** PUBLIC INTERNAL API begins here **************/
+
+const char* _getname_tzplatform_(int id, char signup[33])
+{
+    check_signup(signup);
+    return 0 <= id && id < _TZPLATFORM_VARIABLES_COUNT_ ? keyname(id) : NULL;
+}
+
+int _getid_tzplatform_(const char *name, char signup[33])
+{
+    check_signup(signup);
+    return hashid(name, strlen(name));
+}
+
+const char* _getenv_tzplatform_(int id, char signup[33]) 
+{
+    return _context_getenv_tzplatform_(id, signup, &global_context);
+}
+
+const char* _context_getenv_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[2];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = NULL;
+        result = scratchcat( 0, array);
+    }
+    unlock( context);
+    return result;
+}
+
+int _getenv_int_tzplatform_(int id, char signup[33])
+{
+    return _context_getenv_int_tzplatform_(id, signup, &global_context);
+}
+
+int _context_getenv_int_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
+{
+    const char *value;
+    int result;
+
+    check_signup(signup);
+    value = get_lock(id, context);
+    result = value==NULL ? -1 : atoi(value);
+    unlock( context);
+    return result;
+}
+
+const char* _mkstr_tzplatform_(int id, const char * str, char signup[33])
+{
+    return _context_mkstr_tzplatform_(id, str, signup,  &global_context);
+}
+
+const char* _context_mkstr_tzplatform_(int id, const char *str, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[3];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = str;
+        array[2] = NULL;
+        result = scratchcat( 0, array);
+    }
+    unlock( context);
+    return result;
+}
+
+const char* _mkpath_tzplatform_(int id, const char * path, char signup[33])
+{
+    return _context_mkpath_tzplatform_(id, path, signup,  &global_context);
+}
+
+const char* _context_mkpath_tzplatform_(int id, const char *path, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[3];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = path;
+        array[2] = NULL;
+        result = scratchcat( 1, array);
+    }
+    unlock( context);
+    return result;
+}
+
+const char* _mkpath3_tzplatform_(int id, const char * path, const char* path2, char signup[33])
+{
+    return _context_mkpath3_tzplatform_( id, path, path2, signup,  &global_context);
+}
+
+const char* _context_mkpath3_tzplatform_(int id, const char *path, const char *path2, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[4];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = path;
+        array[2] = path2;
+        array[3] = NULL;
+        result = scratchcat( 1, array);
+    }
+    unlock( context);
+    return result;
+}
+
+const char* _mkpath4_tzplatform_(int id, const char * path, const char* path2, const char *path3, char signup[33])
+{
+    return _context_mkpath4_tzplatform_( id, path, path2, path3, signup,  &global_context);
+}
+
+const char* _context_mkpath4_tzplatform_(int id, const char *path, const char *path2, const char *path3, char signup[33], struct tzplatform_context *context)
+{
+    const char *array[5];
+    const char *result;
+
+    check_signup(signup);
+    result = get_lock(id, context);
+    if (result != NULL) {
+        array[0] = result;
+        array[1] = path;
+        array[2] = path2;
+        array[3] = path3;
+        array[4] = NULL;
+        result = scratchcat( 1, array);
+    }
+    unlock( context);
+    return result;
+}
+
+uid_t _getuid_tzplatform_(int id, char signup[33])
+{
+    return _context_getuid_tzplatform_( id, signup,  &global_context);
+}
+
+uid_t _context_getuid_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
+{
+    uid_t result;
+    const char *value;
+
+    check_signup(signup);
+    result = (uid_t)-1;
+    value = get_lock(id, context);
+    if (value != NULL) {
+        pw_get_uid( value, &result);
+    }
+    unlock( context);
+    return result;
+}
+
+gid_t _getgid_tzplatform_(int id, char signup[33])
+{
+    return _context_getgid_tzplatform_( id, signup,  &global_context);
+}
+
+gid_t _context_getgid_tzplatform_(int id, char signup[33], struct tzplatform_context *context)
+{
+    gid_t result;
+    const char *value;
+
+    check_signup(signup);
+    result = (uid_t)-1;
+    value = get_lock(id, context);
+    if (value != NULL) {
+        pw_get_gid( value, &result);
+    }
+    unlock( context);
+    return result;
+}
+
diff --git a/src/shared-api.h b/src/shared-api.h
new file mode 100644 (file)
index 0000000..8735026
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+
+#ifndef SHARED_API_H
+#define SHARED_API_H
+
+extern const char* _getname_tzplatform_(int id, char signup[33]);
+extern int _getid_tzplatform_(const char *name, char signup[33]);
+extern const char* _getenv_tzplatform_(int id, char signup[33]) ;
+extern const char* _context_getenv_tzplatform_(int id, char signup[33], struct tzplatform_context *context);
+extern int _getenv_int_tzplatform_(int id, char signup[33]);
+extern int _context_getenv_int_tzplatform_(int id, char signup[33], struct tzplatform_context *context);
+extern const char* _mkstr_tzplatform_(int id, const char * str, char signup[33]);
+extern const char* _context_mkstr_tzplatform_(int id, const char *str, char signup[33], struct tzplatform_context *context);
+extern const char* _mkpath_tzplatform_(int id, const char * path, char signup[33]);
+extern const char* _context_mkpath_tzplatform_(int id, const char *path, char signup[33], struct tzplatform_context *context);
+extern const char* _mkpath3_tzplatform_(int id, const char * path, const char* path2, char signup[33]);
+extern const char* _context_mkpath3_tzplatform_(int id, const char *path, const char *path2, char signup[33], struct tzplatform_context *context);
+extern const char* _mkpath4_tzplatform_(int id, const char * path, const char* path2, const char *path3, char signup[33]);
+extern const char* _context_mkpath4_tzplatform_(int id, const char *path, const char *path2, const char *path3, char signup[33], struct tzplatform_context *context);
+extern uid_t _getuid_tzplatform_(int id, char signup[33]);
+extern uid_t _context_getuid_tzplatform_(int id, char signup[33], struct tzplatform_context *context);
+extern gid_t _getgid_tzplatform_(int id, char signup[33]);
+extern gid_t _context_getgid_tzplatform_(int id, char signup[33], struct tzplatform_context *context);
+
+#endif
+
diff --git a/src/static-api.c b/src/static-api.c
new file mode 100644 (file)
index 0000000..d6a4e0c
--- /dev/null
@@ -0,0 +1,190 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <unistd.h>
+#include "tzplatform_variables.h"
+#include "tzplatform_config.h"
+
+#include "shared-api.h"
+#include "isadmin.h"
+
+#include "signup.inc"
+
+int tzplatform_getcount()
+{
+    return _TZPLATFORM_VARIABLES_COUNT_;
+}
+
+const char* tzplatform_getname(enum tzplatform_variable id)
+{
+    return _getname_tzplatform_(id, tizen_platform_config_signup);
+}
+
+enum tzplatform_variable tzplatform_getid(const char *name)
+{
+    return _getid_tzplatform_(name, tizen_platform_config_signup);
+}
+
+const char* tzplatform_getenv(enum tzplatform_variable id) 
+{
+    return _getenv_tzplatform_(id, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id)
+{
+    return _context_getenv_tzplatform_(id, tizen_platform_config_signup, context);
+}
+
+int tzplatform_getenv_int(enum tzplatform_variable id)
+{
+    return _getenv_int_tzplatform_(id, tizen_platform_config_signup);
+}
+
+int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id)
+{
+    return _context_getenv_int_tzplatform_(id, tizen_platform_config_signup, context);
+}
+
+const char* tzplatform_mkstr(enum tzplatform_variable id, const char * str)
+{
+    return _mkstr_tzplatform_(id, str, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str)
+{
+    return _context_mkstr_tzplatform_(id, str, tizen_platform_config_signup, context);
+}
+
+const char* tzplatform_mkpath(enum tzplatform_variable id, const char * path)
+{
+    return _mkpath_tzplatform_(id, path, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path)
+{
+    return _context_mkpath_tzplatform_(id, path, tizen_platform_config_signup, context);
+}
+
+const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path, const char* path2)
+{
+    return _mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2)
+{
+    return _context_mkpath3_tzplatform_(id, path, path2, tizen_platform_config_signup, context);
+}
+
+const char* tzplatform_mkpath4(enum tzplatform_variable id, const char * path, const char* path2, const char *path3)
+{
+    return _mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup);
+}
+
+const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path, const char *path2, const char *path3)
+{
+    return _context_mkpath4_tzplatform_(id, path, path2, path3, tizen_platform_config_signup, context);
+}
+
+uid_t tzplatform_getuid(enum tzplatform_variable id)
+{
+    return _getuid_tzplatform_(id, tizen_platform_config_signup);
+}
+
+uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id)
+{
+    return _context_getuid_tzplatform_(id, tizen_platform_config_signup, context);
+}
+
+gid_t tzplatform_getgid(enum tzplatform_variable id)
+{
+    return _getgid_tzplatform_(id, tizen_platform_config_signup);
+}
+
+gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id)
+{
+    return _context_getgid_tzplatform_(id, tizen_platform_config_signup, context);
+}
+
+int tzplatform_has_system_group(uid_t uid) 
+{
+       return _has_system_group_static_(uid);
+}
+
+#ifdef TEST
+#include <stdlib.h>
+#include <stdio.h>
+
+int main() {
+    int i;
+    struct tzplatform_context *context;
+    enum tzplatform_variable id;
+    const char *name;
+    const char *value;
+    int xid;
+    uid_t uid;
+
+    i = 0;
+    while(i != tzplatform_getcount()) {
+        id = (enum tzplatform_variable)i;
+        name = tzplatform_getname(id);
+        value = tzplatform_getenv(id);
+        xid = (int)tzplatform_getid(name);
+        printf("%d=%d\t%s=%s\n",i,xid,name,value?value:"<null>");
+        i++;
+    }
+
+    printf("------------------------\n");
+    i = tzplatform_context_create(&context);
+    if (i) {
+        printf("error while creating context %d\n",i);
+        return 1;
+    }
+
+    uid = (uid_t)0;
+    i = tzplatform_context_set_user(context, uid);
+    if (i) {
+        printf("error %d while switching to user %d\n",i,(int)uid);
+        return 1;
+    }
+    i = 0;
+    while(i != tzplatform_getcount()) {
+        id = (enum tzplatform_variable)i;
+        name = tzplatform_getname(id);
+        value = tzplatform_context_getenv(context, id);
+        xid = (int)tzplatform_getid(name);
+        printf("%d=%d\t%s=%s\n",i,xid,name,value?value:"<null>");
+        i++;
+    }
+    tzplatform_context_destroy(context);
+
+    return 0;
+}
+#endif
+
+
diff --git a/src/toolbox.c b/src/toolbox.c
new file mode 100644 (file)
index 0000000..35dfd3d
--- /dev/null
@@ -0,0 +1,885 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#define _GNU_SOURCE
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/wait.h>
+#include <stdarg.h>
+
+#include "parser.h"
+#include "heap.h"
+#include "buffer.h"
+#include "foreign.h"
+#include "sha256sum.h"
+
+/*======================================================================*/
+
+#ifndef CONFIGPATH
+# define CONFIGPATH "/etc/tizen-platform.conf"
+#endif
+
+#ifndef TOOLNAME
+# define TOOLNAME "tzplatform-tool"
+#endif
+
+/*== TYPES =============================================================*/
+
+/* for recording read keys */
+struct key {
+    struct key *next;       /* link to next */
+    const char *name;       /* name of the key */
+    const char *value;      /* value of the key */
+    size_t      begin;      /* positions of begin (used by pretty) */
+    size_t      end;        /* positions of end (used by pretty) */
+    int         dependant;  /* is dependant (used by rpm) */
+};
+
+/* for recording used variables */
+struct var {
+    struct var *next;       /* link to next */
+    const char *name;       /* name of the variable */
+    const char *normal;     /* normalized value: "${name}" (for pretty) */
+    int         dependant;  /* is dependant (used by rpm) */
+};
+
+/*== STATIC DATA =======================================================*/
+
+static char help[] = "\
+\n\
+usage: "TOOLNAME" [command] [--] [file]\n\
+\n\
+You can specify the 'file' to process.\n\
+The default file is "CONFIGPATH"\n\
+Specifying - mean the standard input.\n\
+\n\
+Commands:\n\
+\n\
+help       Display this help\n\
+check      Check validity of 'file' (this is the default command)\n\
+pretty     Pretty print of the 'file' (the normalized format)\n\
+h          Produce the C header with the enumeration of the variables\n\
+c          Produce the C code to hash the variable names\n\
+rpm        Produce the macro file to use with RPM\n\
+signup     Produce the signup data for the proxy linked statically\n\
+\n\
+";
+
+static char genh_head[] = "\
+/* I'm generated. Dont edit me! */\n\
+#ifndef TZPLATFORM_VARIABLES_H\n\
+#define TZPLATFORM_VARIABLES_H\n\
+#ifdef __cplusplus\n\
+extern \"C\" {\n\
+#endif\n\
+enum tzplatform_variable {\n\
+\t_TZPLATFORM_VARIABLES_INVALID_ = -1,\n\
+";
+
+static char genh_tail[] = "\
+\t_TZPLATFORM_VARIABLES_COUNT_\n\
+};\n\
+#ifdef __cplusplus\n\
+}\n\
+#endif\n\
+#endif\n\
+";
+
+static char gperf_head[] = "\
+struct varassoc {\n\
+  int offset;\n\
+  int id;\n\
+};\n\
+%%\n\
+";
+
+static char *gperf_command[] = {
+    "/bin/sh",
+    "-c", 
+    "gperf -r -m 100 --null-strings -C -P -L ANSI-C -c"
+          " -t -N hashvar -Q varpool -K offset -G -W namassoc"
+          " -F \", _TZPLATFORM_VARIABLES_INVALID_\"",
+    NULL
+};
+
+static char rpm_head[] = "\
+# I'm generated. Dont edit me! \n\
+\n\
+";
+
+static char signup_head[] = "\
+/* I'm generated. Dont edit me! */\n\
+static char tizen_platform_config_signup[33] = {\n\
+    '\\x00',\n\
+";
+
+static char signup_tail[] = "\
+  };\n\
+";
+
+/*== GLOBALS VARIABLES =================================================*/
+
+/* name of the meta file to process */
+static const char * metafilepath = CONFIGPATH;
+
+/* list of the read keys */
+static struct key *keys = NULL;
+
+/* list of the used variables */
+static struct var *vars = NULL;
+
+/* count of errors */
+static int errcount = 0;
+
+/* dependency state */
+static int dependant = 0;
+
+/* action to perform */
+static enum { CHECK, PRETTY, GENC, GENH, RPM, SIGNUP } action = CHECK;
+
+/* output of error */
+static int notstderr = 0;
+
+/*======================================================================*/
+
+/* write the error message */
+static void vwriterror( const char *format, va_list ap)
+{
+    vfprintf(notstderr ? stdout : stderr, format, ap);
+}
+
+/* write the error message */
+static void writerror( const char *format, ...)
+{
+    va_list ap;
+    va_start(ap, format);
+    vwriterror( format, ap);
+    va_end(ap);
+}
+
+/* write error and exit */
+static void fatal( const char *format, ...)
+{
+    va_list ap;
+
+    writerror("Error, ");
+    va_start(ap, format);
+    vwriterror( format, ap);
+    va_end(ap);
+    writerror(".\n");
+
+    /* exit */
+    exit(1);
+}
+
+/* error in the command line */
+static void argerror( const char *format, ...)
+{
+    va_list ap;
+
+    writerror("Error, ");
+    va_start(ap, format);
+    vwriterror( format, ap);
+    va_end(ap);
+    writerror(".\nType '"TOOLNAME" help' to get usage.\n");
+
+    /* exit */
+    exit(1);
+}
+
+/*== MANAGEMENT OF THE LIST OF READ KEYS ===============================*/
+
+/* search a key of 'name' and length 'lname' and return it or NULL */
+static struct key *key_search( const char *name, size_t lname)
+{
+    struct key *result = keys;
+
+    while(result!=NULL && (strncmp( result->name, name, lname)!=0
+                    || result->name[lname]!=0))
+        result = result->next;
+
+    return result;
+}
+
+/* append a new key to the list and return it or NULL if allocations failed */
+static struct key *key_add( const char *name, size_t lname, 
+                            const char *value, size_t lvalue,
+                            size_t begin_pos, size_t end_pos)
+{
+    struct key *result, *prev;
+    char *sname, *svalue;
+
+    /* allocations */
+    result = malloc(sizeof *result);
+    sname = strndup( name, lname);
+    svalue = strndup( value, lvalue);
+    if (result == NULL || sname == NULL || svalue == NULL) {
+        /* failure of allocations */
+        free( result);
+        free( sname);
+        free( svalue);
+        result = NULL;
+    }
+    else {
+        /* success: init the structure */
+        result->next = NULL;
+        result->name = sname;
+        result->value = svalue;
+        result->begin = begin_pos;
+        result->end = end_pos;
+        result->dependant = dependant;
+
+        /* link at end of the list */
+        prev = keys;
+        if (prev == NULL)
+            keys = result;
+        else {
+            while(prev!=NULL && prev->next!=NULL)
+                prev = prev->next;
+            prev->next = result;
+        }
+    }
+
+    return result;
+}
+
+/*======================================================================*/
+
+/* search a var of 'name' and length 'lname' and return it or NULL */
+static struct var *var_search( const char *name, size_t lname)
+{
+    struct var *result = vars;
+    while(result!=NULL && (strncmp( result->name, name, lname)!=0
+                    || result->name[lname]!=0))
+        result = result->next;
+
+    return result;
+}
+
+/* append a new var to the list and return it or NULL if allocations failed */
+static struct var *var_add( const char *name, size_t lname, 
+                                                int depend, const char *value)
+{
+    struct var *result, *prev;
+    char *sname, *normal;
+    size_t length;
+
+    /* check for the value */
+    if (action == RPM && value != NULL) {
+        length = strlen( value) + 1;
+    }
+    else {
+        value = NULL;
+        length = lname + 4;
+    }
+    /* allocations */
+    result = malloc(sizeof *result);
+    sname = strndup( name, lname);
+    normal = malloc( length);
+    if (result == NULL || sname == NULL || normal == NULL) {
+        /* failure of allocations */
+        free( result);
+        free( sname);
+        free( normal);
+        result = NULL;
+    }
+    else {
+        /* success: init the structure */
+        result->next = NULL;
+        result->name = sname;
+        result->normal = normal;
+        result->dependant = depend;
+        if (value) {
+            memcpy( normal, value, length);
+        }
+        else {
+            *normal++ = '$';
+            *normal++ = '{';
+            memcpy( normal, name, lname);
+            normal += lname;
+            *normal++ = '}';
+            *normal = 0;
+        }
+
+        /* link at end of the list */
+        prev = vars;
+        if (prev == NULL)
+            vars = result;
+        else {
+            while(prev!=NULL && prev->next!=NULL)
+                prev = prev->next;
+            prev->next = result;
+        }
+    }
+
+    return result;
+}
+
+/*== CALLBACK OF PARSING OF THE META FILE ==============================*/
+
+static void conferr( const char *format, ...)
+{
+    va_list ap;
+
+    notstderr = action == CHECK;
+    writerror( "ERROR ");
+    va_start(ap, format);
+    vwriterror( format, ap);
+    va_end(ap);
+    notstderr = 0;
+}
+
+static int errcb( struct parsing *parsing,
+                size_t pos, const char *message)
+{
+    struct parsinfo info;
+
+    /* get the info */
+    parse_utf8_info( parsing, &info, pos);
+
+    /* emit the error */
+    conferr("line %d: %s\n..: %.*s\n..: %*s\n", 
+                info.lino, message, 
+                (int)info.length, info.begin,
+                info.colno, "^"); 
+
+    /* count it */
+    errcount++;
+
+    /* continue to parse */
+    return 1;
+}
+
+static int putcb( struct parsing *parsing, 
+                const char *name, size_t lname, 
+                const char *value, size_t lvalue,
+                size_t begin_pos, size_t end_pos)
+{
+    enum fkey fkey;
+    struct key *key;
+    struct parsinfo here, there;
+
+    /* check that it is not a foreign key */
+    fkey = foreign( name, lname);
+    if (fkey != _FOREIGN_INVALID_) {
+        parse_utf8_info( parsing, &here, begin_pos);
+        conferr("line %d: reserved variable name '%.*s'\n" 
+                "..: %.*s\n..: %*s\n", 
+                   here.lino, (int)lname, name,
+                   here.lino, (int)here.length, here.begin, here.colno, "^"); 
+
+        errcount++;
+        dependant = 0;
+        return 0;
+    }
+
+    /* search if already defined */
+    key = key_search( name, lname);
+    if (key != NULL) {
+
+        /* yes! that's an error */
+        parse_utf8_info( parsing, &here, begin_pos);
+        parse_utf8_info( parsing, &there, key->begin);
+        conferr("line %d: redefinition of '%s'\n" 
+                "...was defined line %d\n..: %.*s\n..: %*s\n"
+                "...is redefined line %d\n..: %.*s\n..: %*s\n", 
+                   here.lino, key->name,
+                   there.lino, (int)there.length, there.begin, there.colno, "^",
+                   here.lino, (int)here.length, here.begin, here.colno, "^"); 
+
+        errcount++;
+        dependant = 0;
+        return 0;
+    }
+
+    /* create and record the key */
+    key = key_add( name, lname, value, lvalue, begin_pos, end_pos);
+    dependant = 0;
+    if (key != NULL)
+        return 0;
+
+    /* no can't because of memory! */
+    fatal("out of memory");
+    errcount++;
+    return -1;
+}
+
+static const char *getcb( struct parsing *parsing,
+                const char *name, size_t length,
+                size_t begin_pos, size_t end_pos)
+{
+    struct parsinfo here;
+    struct var *var;
+    enum fkey fkey;
+    struct key *key;
+    int depend;
+
+    /* search if already defined */
+    var = var_search( name, length);
+    if (var != NULL) {
+        /* yes cool, return the normalized form */
+        if (var->dependant)
+            dependant = 1;
+        return var->normal;
+    }
+
+    /* search the variable */
+    fkey = foreign( name, length);
+    key = key_search( name, length);
+
+    if (fkey == _FOREIGN_INVALID_ && key == NULL) {
+
+        /* not a valid variable, emit the error */
+        parse_utf8_info( parsing, &here, begin_pos);
+        conferr("line %d: use of unknown variable '%.*s'\n"
+                "..: %.*s\n..: %*s\n", 
+                    here.lino, (int)length, name,
+                    (int)here.length, here.begin, here.colno, "^"); 
+        errcount++;
+        dependant = 1; /* kind of invalidity */
+        return "***error***"; /* avoid further error */
+    }
+
+    /* valid variables:  those of foreign or the already defined keys */
+
+    /* set dependant state */
+    depend = fkey != _FOREIGN_INVALID_ || key->dependant;
+    if (depend)
+        dependant = 1;
+
+    /* create and record the variable */
+    var = var_add( name, length, depend, key==NULL ? NULL : key->value);
+    if (var != NULL)
+        /* created, return the normalized form */
+        return var->normal;
+
+    /* memory depletion */
+    fatal("out of memory");
+    dependant = 1; /* kind of invalidity */
+    errcount++;
+    return "***error***"; /* avoid further error */
+}
+
+/*======================================================================*/
+
+/* compare two keys */
+static int keycmp(const void *a, const void *b)
+{
+    const struct key *ka = *(const struct key **)a;
+    const struct key *kb = *(const struct key **)b;
+    return strcmp(ka->name, kb->name);
+}
+
+/* sort the keys and return their count */
+static int sortkeys()
+{
+    struct key *key = keys, **array;
+    int count = 0, index;
+
+    while (key) {
+        key = key->next;
+        count++;
+    }
+
+    array = malloc( count * sizeof * array);
+    if (array == NULL)
+        return -1;
+
+    key = keys;
+    index = 0;
+    
+    while (key) {
+        array[index++] = key;
+        key = key->next;
+    }
+
+    qsort(array, count, sizeof * array, keycmp);
+
+    while (index) {
+       array[--index]->next = key;
+        key = array[index];
+    }
+    keys = key;
+    free( array);
+
+    return count;
+}
+
+/*======================================================================*/
+
+/* pretty print the read file */
+static int pretty( const char *buffer, size_t length, FILE *output)
+{
+    int status;
+    struct key *key = keys;
+    size_t pos = 0;
+
+    while (pos < length && key != NULL) {
+        if (pos < key->begin) {
+            status = fprintf(output, "%.*s", (int)(key->begin-pos), buffer+pos);
+            if (status < 0)
+                return status;
+        }
+        status = fprintf( output, "%s=%s", key->name, key->value);
+        if (status < 0)
+            return status;
+        pos = key->end;
+        key = key->next;
+    }
+
+    if (pos < length) {
+        status = fprintf( output, "%.*s", (int)(length-pos), buffer+pos);
+        if (status < 0)
+            return status;
+    }
+
+    return 0;
+}
+
+/* generate the header */
+static int genh( FILE *output)
+{
+    struct key *key;
+    int status;
+
+#ifndef NO_SORT_KEYS
+    status = sortkeys();
+    if (status < 0)
+        return status;
+#endif
+
+    status = fprintf( output, "%s", genh_head);
+    if (status < 0)
+        return status;
+    for (key = keys ; key != NULL ; key = key->next) {
+        status = fprintf( output, "\t%s,\n", key->name);
+        if (status < 0)
+            return status;
+    }
+    status = fprintf( output, "%s", genh_tail);
+    if (status < 0)
+        return status;
+    return 0;
+}
+
+/* generate hash code using gperf */
+static int genc(FILE *output)
+{
+    struct key *key;
+    int fds[2];
+    pid_t pid;
+    int result, sts;
+    size_t l;
+
+#ifndef NO_SORT_KEYS
+    sts = sortkeys();
+    if (sts < 0)
+        return sts;
+#endif
+
+    result = pipe(fds);
+    if (result != 0)
+        return result;
+
+    fflush( output);
+    pid = fork();
+    if (pid == -1) {
+        close( fds[0]);
+        close( fds[1]);
+        fatal( "can't fork");
+        result = -1;
+    }
+    else if (pid == 0) {
+        dup2( fds[0], 0);
+        close( fds[0]);
+        close( fds[1]);
+        if (fileno(output) != 1)
+            dup2( fileno(output), 1);
+        result = execve( gperf_command[0], gperf_command, environ);
+        fatal("can't execute gperf");
+        exit(1);
+    }
+    else {
+        close( fds[0]);
+        sts = write( fds[1], gperf_head, sizeof(gperf_head)-1);
+        if (sts < 0)
+            result = sts;
+        for (key = keys ; key != NULL ; key = key->next) {
+            l = strlen( key->name);
+            sts = write( fds[1], key->name, l);
+            if (sts < 0)
+                result = sts;
+            sts = write( fds[1], ", ", 2);
+            if (sts < 0)
+                result = sts;
+            sts = write( fds[1], key->name, l);
+            if (sts < 0)
+                result = sts;
+            sts = write( fds[1], "\n", 1);
+            if (sts < 0)
+                result = sts;
+        }
+        close( fds[1]);
+        wait(&result);
+        sts = WIFEXITED(result) && WEXITSTATUS(result)==0 ? 0 : -1;
+        if (sts < 0)
+            result = sts;
+    }
+    return result;
+}
+
+/* generate the rpm macros */
+static int rpm( FILE *output)
+{
+    struct key *key;
+    int status;
+
+#ifndef NO_SORT_KEYS
+    status = sortkeys();
+    if (status < 0)
+        return status;
+#endif
+
+    status = fprintf( output, "%s", rpm_head);
+    if (status < 0)
+        return status;
+    for (key = keys ; key != NULL ; key = key->next) {
+        if (!key->dependant) {
+            status = fprintf( output, "%%%-40s %s\n", key->name, key->value);
+            if (status < 0)
+                return status;
+        }
+    }
+    return 0;
+}
+
+/* generate the signup */
+static int signup( FILE *output)
+{
+    struct key *key;
+    int status;
+    int i;
+    struct sha256sum *sum;
+    char term;
+    char signup[32];
+
+#ifndef NO_SORT_KEYS
+    status = sortkeys();
+    if (status < 0)
+        return status;
+#endif
+
+    sum = sha256sum_create();
+    if (sum == NULL)
+        return -1;
+
+    term = ';';
+    for (key = keys ; key != NULL ; key = key->next) {
+        status = sha256sum_add_data(sum, key->name, strlen(key->name));
+        if (status < 0) {
+            sha256sum_destroy(sum);
+            return status;
+        }
+        status = sha256sum_add_data(sum, &term, 1);
+        if (status < 0) {
+            sha256sum_destroy(sum);
+            return status;
+        }
+    }
+
+    status = sha256sum_get(sum, signup);
+    sha256sum_destroy(sum);
+    if (status < 0)
+        return status;
+
+    status = fprintf( output, "%s", signup_head);
+    if (status < 0)
+        return status;
+
+    for (i=0 ; i<32 ; i++) {
+        status = fprintf( output, "%s'\\x%02x'%s",
+                    (i & 7) ? " " : "    ",
+                    (int)(unsigned char)signup[i],
+                    (i & 7) < 7 ? "," : i == 31 ? "\n" : ",\n");
+        if (status < 0)
+            return status;
+    }
+    status = fprintf( output, "%s", signup_tail);
+    if (status < 0)
+        return status;
+    return 0;
+}
+
+/* main of processing */
+static int process()
+{
+    struct parsing parsing;
+    struct buffer buffer;
+    int result;
+
+    /* read the file */
+    result = buffer_create( &buffer, metafilepath);
+    if (result != 0) {
+        fatal( "can't read file %s", metafilepath);
+        return -1;
+    }
+
+    /* parse the file */
+    parsing.buffer = buffer.buffer;
+    parsing.length = buffer.length;
+    parsing.maximum_data_size = 0;
+    parsing.should_escape = action!=RPM;
+    parsing.data = 0;
+    parsing.get = getcb;
+    parsing.put = putcb;
+    parsing.error = errcb;
+    dependant = 0;
+    result = parse_utf8_config( &parsing);
+    if (result != 0) {
+        buffer_destroy( &buffer);
+        fatal( "while parsing the file %s", metafilepath);
+        return -1;
+    }
+    if (errcount != 0) {
+        buffer_destroy( &buffer);
+        fatal( "%d errors detected %s", errcount, metafilepath);
+        return -1;
+    }
+
+    /* process */
+    switch( action) {
+    case CHECK:
+        break;
+    case PRETTY:
+        pretty( buffer.buffer, buffer.length, stdout);
+        break;
+    case GENH:
+        genh( stdout);
+        break;
+    case GENC:
+        genc( stdout);
+        break;
+    case RPM:
+        rpm( stdout);
+        break;
+    case SIGNUP:
+        signup( stdout);
+        break;
+    }
+
+    buffer_destroy( &buffer);
+    return 0;
+}
+
+/*======================================================================*/
+
+/* very simple argument parsing */
+static int arguments( char **argv)
+{
+    /* skip the program name*/
+    argv++;
+
+    /* scan first arg */
+    if (*argv == NULL) {
+        /* no argument then default is to check */
+        action = CHECK;
+    }
+    else {
+        /* check if first argument is 'check', 'pretty', 'c', 'h', '-h',
+           '--help' or 'help' */
+        if (0 == strcmp( *argv, "check")) {
+            action = CHECK;
+            argv++;
+        }
+        else if (0 == strcmp( *argv, "pretty")) {
+            action = PRETTY;
+            argv++;
+        }
+        else if (0 == strcmp( *argv, "c")) {
+            action = GENC;
+            argv++;
+        }
+        else if (0 == strcmp( *argv, "h")) {
+            action = GENH;
+            argv++;
+        }
+        else if (0 == strcmp( *argv, "rpm")) {
+            action = RPM;
+            argv++;
+        }
+        else if (0 == strcmp( *argv, "signup")) {
+            action = SIGNUP;
+            argv++;
+        }
+        else if (0 == strcmp( *argv, "help") || 0 == strcmp( *argv, "--help")) {
+            printf("%s", help);
+            exit(0);
+            return -1;
+        }
+        else if (**argv == '-') {
+            if (0 == strcmp( *argv, "--") || 0 == strcmp( *argv, "-") ) {
+                action = CHECK;
+            }
+            else {
+                argerror( "unknown option '%s'", *argv);
+                return -1;
+            }
+        }
+        /* skip the -- arg if present */
+        if (*argv != NULL && 0 == strcmp( *argv, "--")) {
+            argv++;
+        }
+        /* get a meta file argument */
+        if (*argv != NULL) {
+            if (0 == strcmp( *argv, "-"))
+                metafilepath = "/dev/stdin";
+            else
+                metafilepath = *argv;
+            argv++;
+        }
+        /* check that there is no extra argument */
+        if (*argv != NULL) {
+            argerror("extra argument found '%s'",*argv);
+            return -1;
+        }   
+    }
+    return 0;
+}
+
+int main(int argc, char **argv)
+{
+    if (arguments(argv) == 0)
+        if (process() == 0)
+            return 0;
+
+    return 1;
+}
+
+
diff --git a/src/tzplatform_config.h b/src/tzplatform_config.h
new file mode 100644 (file)
index 0000000..ab998b0
--- /dev/null
@@ -0,0 +1,412 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+#ifndef LIBTIZEN_PLATFORM_WRAPPER
+#define LIBTIZEN_PLATFORM_WRAPPER
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include <sys/types.h>
+#include <tzplatform_variables.h>
+
+/*------------------------------ COMMON API (no context) ---------------*/
+
+/*
+ Return the count of variables.
+*/
+extern
+int tzplatform_getcount();
+
+/*
+ Return the name of the variable 'id' as a string.
+ Return NULL if 'id' is invalid.
+*/
+extern
+const char* tzplatform_getname(enum tzplatform_variable id);
+
+/*
+ Return the id of the variable of 'name'.
+ Return _TZPLATFORM_VARIABLES_INVALID_ if 'name' doesn't match a
+ valid variable name.
+*/
+extern
+enum tzplatform_variable tzplatform_getid(const char *name);
+
+/*------------------------------ GLOBAL API (default global context) ----*/
+
+/*
+ Enforces the removal of the previously evaluated tizen platform variables.
+
+ Call this function in case of changing of user inside the application.
+*/
+extern
+void tzplatform_reset();
+
+/*
+ Set the user used for UID/EUID/USER/EUSER computations.
+ Using uid==(uid_t)-1 reset the context as if tzplatform_reset_user
+ was called.
+
+ Returns 0 if uid is valid or -1 if not valid.
+*/
+extern
+int tzplatform_set_user(uid_t uid);
+
+/*
+*/
+extern
+uid_t tzplatform_get_user();
+
+/*
+ Reset the user context to use the values returned by
+ getuid and geteuid.
+*/
+extern
+void tzplatform_reset_user();
+
+/*
+ Return the read-only string value of the tizen plaform variable 'id'.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error or when 'id' isn't defined.
+*/
+extern
+const char* tzplatform_getenv(enum tzplatform_variable id);
+
+/*
+ Return the integer value of the tizen plaform variable 'id'.
+*/
+extern
+int tzplatform_getenv_int(enum tzplatform_variable id);
+
+/*
+ Return the string resulting of the concatenation of string value of the
+ tizen plaform variable 'id' and the given string 'str'.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error.
+
+ Example:
+    if TZ_SYS_HOME == "/opt/home" then calling
+
+       tzplatform_mkstr(TZ_SYS_HOME,"-yes")
+
+    will return "/opt/home-yes"
+*/
+extern
+const char* tzplatform_mkstr(enum tzplatform_variable id, const char *str);
+
+/*
+ Return the string resulting of the path-concatenation of string value of the
+ tizen plaform variable 'id' and the given string 'path'.
+
+ path-concatenation is the concatenation taking care of / characters.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error.
+
+ Example:
+    if TZ_SYS_HOME == "/opt/home" then calling
+
+       tzplatform_mkpath(TZ_SYS_HOME,"yes")
+
+    will return "/opt/home/yes"
+*/
+extern
+const char* tzplatform_mkpath(enum tzplatform_variable id, const char *path);
+
+/*
+ Return the string resulting of the path-concatenation of string value of the
+ tizen plaform variable 'id' and the given strings 'path' and 'path2'.
+
+ path-concatenation is the concatenation taking care of / characters.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error.
+
+ Example:
+    if TZ_SYS_HOME == "/opt/home" then calling
+
+       tzplatform_mkpath3(TZ_SYS_HOME,"yes","no")
+
+    will return "/opt/home/yes/no"
+*/
+extern
+const char* tzplatform_mkpath3(enum tzplatform_variable id, const char *path,
+                                                            const char *path2);
+
+/*
+ Return the string resulting of the path-concatenation of string value of the
+ tizen plaform variable 'id' and the given strings 'path', 'path2' and 'path3'.
+
+ path-concatenation is the concatenation taking care of / characters.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error.
+
+ Example:
+    if TZ_SYS_HOME == "/opt/home" then calling
+
+       tzplatform_mkpath4(TZ_SYS_HOME,"yes","no","/maybe")
+
+    will return "/opt/home/yes/no/maybe"
+*/
+extern
+const char* tzplatform_mkpath4(enum tzplatform_variable id, const char *path,
+                                        const char *path2, const char *path3);
+
+/*
+ Return the uid for a given user name, stored in variable <id>
+ Retun -1 in case of error.
+
+ Example:
+    if TZ_USER_NAME=="app" then calling:
+
+       tzplatform_getuid(TZ_USER_NAME)
+
+    will return the uid of the user 'app'
+*/
+extern
+uid_t tzplatform_getuid(enum tzplatform_variable id);
+
+/*
+ Return the gid for a given user name, stored in variable <id>
+ Retun -1 in case of error.
+
+ Example:
+    if TZ_USER_NAME=="app" then calling:
+
+       tzplatform_getuid(TZ_USER_NAME)
+
+    will return the gid of the user 'app'
+*/
+extern
+gid_t tzplatform_getgid(enum tzplatform_variable id);
+
+/*------------------------------ CONTEXTUAL API --------------------------*/
+
+struct tzplatform_context;
+
+/*
+ Creates a new platform 'context'.
+ Return 0 in case of success or a negative value in case of error.
+*/
+extern
+int tzplatform_context_create(struct tzplatform_context **context);
+
+/*
+ Destroys the platform 'context' previously created with 'tzplatform_context_create'.
+ The destroyed context must not be used after calling this function.
+*/
+extern
+void tzplatform_context_destroy(struct tzplatform_context *context);
+
+/*
+ Enforces the removal of the previously evaluated tizen platform variables.
+*/
+extern
+void tzplatform_context_reset(struct tzplatform_context *context);
+
+/*
+ Set the user used for UID/EUID/USER/EUSER computations.
+ Using uid==(uid_t)-1 reset the context as if tzplatform_context_reset_user
+ was called.
+
+ Returns 0 if uid is valid or -1 if not valid.
+*/
+extern
+int tzplatform_context_set_user(struct tzplatform_context *context, uid_t uid);
+
+/*
+ Get the user set to the context.
+*/
+extern
+uid_t tzplatform_context_get_user(struct tzplatform_context *context);
+
+/*
+ Reset the user context to use the values returned by
+ getuid and geteuid.
+*/
+extern
+void tzplatform_context_reset_user(struct tzplatform_context *context);
+
+/*
+ Return the read-only string value of the tizen plaform variable 'id'.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error or when 'id' isn't defined.
+*/
+extern
+const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id);
+
+/*
+ Return the integer value of the tizen plaform variable 'id'.
+*/
+extern
+int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id);
+
+/*
+ Return the string resulting of the concatenation of string value of the
+ tizen plaform variable 'id' and the given string 'str'.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error.
+
+ Example:
+    if TZ_SYS_HOME == "/opt/home" then calling
+
+       tzplatform_context_mkstr(context, TZ_SYS_HOME,"-yes")
+
+    will return "/opt/home-yes"
+*/
+extern
+const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str);
+
+/*
+ Return the string resulting of the path-concatenation of string value of the
+ tizen plaform variable 'id' and the given string 'path'.
+
+ path-concatenation is the concatenation taking care of / characters.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error.
+
+ Example:
+    if TZ_SYS_HOME == "/opt/home" then calling
+
+       tzplatform_context_mkpath(context, TZ_SYS_HOME,"yes")
+
+    will return "/opt/home/yes"
+*/
+extern
+const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path);
+
+/*
+ Return the string resulting of the path-concatenation of string value of the
+ tizen plaform variable 'id' and the given strings 'path' and 'path2'.
+
+ path-concatenation is the concatenation taking care of / characters.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error.
+
+ Example:
+    if TZ_SYS_HOME == "/opt/home" then calling
+
+       tzplatform_context_mkpath3(context, TZ_SYS_HOME,"yes","no")
+
+    will return "/opt/home/yes/no"
+*/
+extern
+const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path,
+                                                            const char *path2);
+
+/*
+ Return the string resulting of the path-concatenation of string value of the
+ tizen plaform variable 'id' and the given strings 'path', 'path2' and 'path3'.
+
+ path-concatenation is the concatenation taking care of / characters.
+
+ The returned value is an allocated unique string that MUST not be freed.
+
+ Can return NULL in case of internal error.
+
+ Example:
+    if TZ_SYS_HOME == "/opt/home" then calling
+
+       tzplatform_context_mkpath4(context, TZ_SYS_HOME,"yes","no","/maybe")
+
+    will return "/opt/home/yes/no/maybe"
+*/
+extern
+const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path,
+                                        const char *path2, const char *path3);
+
+/*
+ Return the uid for a given user name, stored in variable <id>
+ Retun -1 in case of error.
+
+ Example:
+    if TZ_USER_NAME=="app" then calling:
+
+       tzplatform_context_getuid(context, TZ_USER_NAME)
+
+    will return the uid of the user 'app'
+*/
+extern
+uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id);
+
+/*
+ Return the gid for a given user name, stored in variable <id>
+ Retun -1 in case of error.
+
+ Example:
+    if TZ_USER_NAME=="app" then calling:
+
+       tzplatform_context_getuid(context, TZ_USER_NAME)
+
+    will return the gid of the user 'app'
+*/
+extern
+gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id);
+
+
+/*
+ Return 1 if given uid is in the system admin group (named "system")
+ Return 0 if not
+ Return -1 in case of error.
+
+ Example:
+        tzplatform_has_system_group(1000)
+
+    will return 0 or 1 depends on right of given uid.
+    
+   NOTE :
+   * If you pass the -1 value to this function it will take the current uid given
+   * by the POSIX function getuid(); 
+   
+   *** WARNING : ***
+   * This is a temporary feature
+   * This will be managed by Cynara
+    
+*/
+extern
+int tzplatform_has_system_group(uid_t uid);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LIBTIZEN_PLATFORM_WRAPPER */
+
diff --git a/src/tzplatform_config.sym b/src/tzplatform_config.sym
new file mode 100644 (file)
index 0000000..1b94e85
--- /dev/null
@@ -0,0 +1,57 @@
+TPC {
+       global:
+               _context_getenv_int_tzplatform_;
+               _context_getenv_tzplatform_;
+               _context_getgid_tzplatform_;
+               _context_getuid_tzplatform_;
+               _context_mkpath_tzplatform_;
+               _context_mkpath3_tzplatform_;
+               _context_mkpath4_tzplatform_;
+               _context_mkstr_tzplatform_;
+               _getenv_int_tzplatform_;
+               _getenv_tzplatform_;
+               _getgid_tzplatform_;
+               _getid_tzplatform_;
+               _getname_tzplatform_;
+               _getuid_tzplatform_;
+               _mkpath_tzplatform_;
+               _mkpath3_tzplatform_;
+               _mkpath4_tzplatform_;
+               _mkstr_tzplatform_;
+
+
+               tzplatform_context_create;
+               tzplatform_context_destroy;
+               tzplatform_context_getenv;
+               tzplatform_context_getenv_int;
+               tzplatform_context_getgid;
+               tzplatform_context_getuid;
+               tzplatform_context_get_user;
+               tzplatform_context_mkpath;
+               tzplatform_context_mkpath3;
+               tzplatform_context_mkpath4;
+               tzplatform_context_mkstr;
+               tzplatform_context_reset;
+               tzplatform_context_reset_user;
+               tzplatform_context_set_user;
+               tzplatform_getcount;
+               tzplatform_getenv;
+               tzplatform_getenv_int;
+               tzplatform_getgid;
+               tzplatform_getid;
+               tzplatform_getname;
+               tzplatform_getuid;
+               tzplatform_get_user;
+               tzplatform_has_system_group;
+               tzplatform_mkpath;
+               tzplatform_mkpath3;
+               tzplatform_mkpath4;
+               tzplatform_mkstr;
+               tzplatform_reset;
+               tzplatform_reset_user;
+               tzplatform_set_user;
+
+       local:
+               *;
+};
+
diff --git a/src/tzplatform_get.c b/src/tzplatform_get.c
new file mode 100644 (file)
index 0000000..b37a5eb
--- /dev/null
@@ -0,0 +1,216 @@
+/*
+ * Copyright (C) 2013-2014 Intel Corporation.
+ * 
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Authors:
+ *   José Bollo <jose.bollo@open.eurogiciel.org>
+ *   Stéphane Desneux <stephane.desneux@open.eurogiciel.org>
+ *   Jean-Benoit Martin <jean-benoit.martin@open.eurogiciel.org>
+ *
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <pwd.h>
+
+#include <tzplatform_config.h>
+
+#define basename(x) (x)
+
+static const char usage [] = "type '%s --help' to get help.\n";
+
+static const char help [] = "\
+\n\
+usage: %s [options...] [keys...]\n\
+\n\
+options:\n\
+\n\
+-a --all        all keys (must not be followed by keys)\n\
+-n --not        not in the keys\n\
+-e --export     prefix with export\n\
+-l --list       prints only the key names, not the values\n\
+-s --space      separate with spaces instead of new-lines\n\
+-q --query      silently check that given variables are existing\n\
+-c --continue   continue to process if error\n\
+-u --user  id   set the user using its 'id' (name or numeric)\n\
+\n\
+";
+
+int main(int argc, char **argv)
+{
+       char *progname = *argv++, *user = 0;
+       int all = 0, not = 0, query = 0, export = 0, space = 0, list = 0, cont = 0;
+       int i, n, *sel, p;
+       enum tzplatform_variable id;
+       struct passwd pwd, *spw;
+       char buf[1024];
+
+       /* parse args */
+       while(*argv && **argv=='-') {
+               char c, *opt = 1+*argv++;
+               while(opt) {
+                       if (*opt == '-') {
+                               char *x = 0;
+                               c = *++opt;
+                               switch(c) {
+                                       case 'a': x = "all"; break;
+                                       case 'n': x = "not"; break;
+                                       case 'q': x = "query"; break;
+                                       case 'e': x = "export"; break;
+                                       case 's': x = "space"; break;
+                                       case 'l': x = "list"; break;
+                                       case 'c': x = "continue"; break;
+                                       case 'h': x = "help"; break;
+                                       case 'u': x = "user"; break;
+                               }
+                               if (!x || strcmp(x,opt))
+                                       c = 0;
+                               opt = 0;
+                       }
+                       else {
+                               c = *opt;
+                               if (!*++opt)
+                                       opt = 0;
+                       }
+                       switch(c) {
+                       case 'a': all = 1; break;
+                       case 'n': not = 1; break;
+                       case 'q': query = 1; break;
+                       case 'e': export = 1; break;
+                       case 's': space = 1; break;
+                       case 'l': list = 1; break;
+                       case 'c': cont = 1; break;
+                       case 'u': user = *argv; if (user) argv++; break;
+                       case 'h':
+                               fprintf( stdout, help, basename(progname));
+                               return 0;
+                       default:
+                               fprintf( stderr, usage, basename(progname));
+                               return 1;
+                       }
+               }
+       }
+
+       /* some checks */
+       if (query) {
+               if (all) {
+                       fprintf( stderr, 
+                               "error! --all and --query aren't compatibles.\n");
+                       return 1;
+               }
+               if (not) {
+                       fprintf( stderr, 
+                               "error! --not and --query aren't compatibles.\n");
+                       return 1;
+               }
+               if (list) {
+                       fprintf( stderr, 
+                               "error! --list and --query aren't compatibles.\n");
+                       return 1;
+               }
+               if (cont) {
+                       fprintf( stderr, 
+                               "error! --continue and --query aren't compatibles.\n");
+                       return 1;
+               }
+               if (space) {
+                       fprintf( stderr, 
+                               "warning! --space option ignored for queries.\n");
+               }
+               if (export) {
+                       fprintf( stderr, 
+                               "warning! --export option ignored for queries.\n");
+               }
+       }
+       if (all) {
+               if (*argv) {
+                       fprintf( stderr, 
+                               "error! --all doesn't accept any key.\n");
+                       return 1;
+               }
+               /* all is like not nothing!! */
+               not = 1;
+       }
+
+       /* process */
+       n = tzplatform_getcount();
+       sel = calloc( sizeof(int), n);
+       if (sel == NULL) {
+               fprintf( stderr, "error! out of memory!\n");
+               return 1;
+       }
+
+       /* get the variables from the list */
+       while (*argv) {
+               id = tzplatform_getid( *argv);
+               if (id == _TZPLATFORM_VARIABLES_INVALID_) {
+                       if (query)
+                               return 1;
+                       fprintf( stderr, "error! %s isn't a variable.\n", *argv);
+                       if (!cont)
+                               return 1;
+               }
+               else {
+                       sel[(int)id] = 1;
+               }
+               argv++;
+       }
+
+       /* finished for queries */
+       if (query) 
+               return 0;
+
+       /* set the user */
+       if (user) {
+               for (i=0 ; '0' <= user[i] && user[i] <= '9' ; i++);
+               if (user[i])
+                       getpwnam_r(user, &pwd, buf, sizeof(buf), &spw);
+               else
+                       getpwuid_r((uid_t)atoi(user), &pwd, buf, sizeof(buf), &spw);
+               if (!spw) {
+                       fprintf( stderr, "error! %s isn't standing for a valid user.\n", user);
+                       if (!cont)
+                               return 1;
+               } else {
+                       i = tzplatform_set_user(spw->pw_uid);
+                       if (i) {
+                               fprintf( stderr, "error! can't set the valid user %s.\n", user);
+                               if (!cont)
+                                       return 1;
+                       }
+               }
+       }
+
+       /* emits the result */
+       for (p = i = 0 ; i < n ; i++) {
+               if (sel[i] != not) {
+                       if (p++) 
+                               printf( space ? " " : export ? "\nexport " : "\n");
+                       else if (export)
+                               printf( "export ");
+                       id = (enum tzplatform_variable) i;
+                       printf( "%s", tzplatform_getname(id));
+                       if (!list)      
+                               printf( "=%s", tzplatform_getenv(id));
+               }
+       }
+       if (p)
+               printf("\n");
+       return 0;
+}
+