Adding signature checking 02/27902/2 accepted/tizen/common/20140924.112708 accepted/tizen/ivi/20140927.095037 submit/tizen_common/20140923.154519 submit/tizen_common/20140924.073135 submit/tizen_ivi/20140926.000000
authorJosé Bollo <jose.bollo@open.eurogiciel.org>
Thu, 18 Sep 2014 14:41:16 +0000 (16:41 +0200)
committerJosé Bollo <jose.bollo@open.eurogiciel.org>
Tue, 23 Sep 2014 07:46:04 +0000 (09:46 +0200)
The couple tizen-platform-wrapper/tizen-platform-config is
intended to be a foundation of what is the tizen configuration.

As such, each change of the set of keys SHOULD implies a full
rebuild of tizen. It is what normally happens via the set
of build dependencies. But some developper encountered problems
because they were taking part of the system, having different
versions of configuration. Especially, it happened was installing
a fresh rpm built with GBS on an old image. This problem is reported
in the following file:

Bug-Tizen: TC-1626

This commit modify the behaviour of the generated library but
doesn't modify its API. The new principle is to add a signup that
it linked to the client and checked by the shared library. If the
client's signup misfits library's one, a critical error is printed
to using syslog and the program is aborted.

Also, the modularity of the source is improved.

Change-Id: I5c2e5a990ebf17f893b669c272fda99d0d0b2de1
Signed-off-by: José Bollo <jose.bollo@open.eurogiciel.org>
32 files changed:
configure.ac
packaging/tizen-platform-wrapper.spec
src/Makefile.am
src/buffer.c
src/buffer.h
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
src/foreign.h
src/hashing.c [new file with mode: 0644]
src/hashing.h [new file with mode: 0644]
src/heap.c
src/heap.h
src/init.c [moved from src/tzplatform_config.c with 50% similarity]
src/init.h [new file with mode: 0644]
src/parser.c
src/parser.h
src/passwd.c
src/passwd.h
src/scratch.c
src/scratch.h
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
src/tzplatform_config.h
src/tzplatform_config.sym [new file with mode: 0644]
src/tzplatform_get.c
tizen-platform-wrapper.pc.in

index 7b4e1ce..3e4ad62 100644 (file)
@@ -2,7 +2,7 @@
 # Process this file with autoconf to produce a configure script.
 
 AC_PREREQ([2.69])
-AC_INIT([tizen-platform-wrapper], [1.0])
+AC_INIT([tizen-platform-wrapper], [2.0])
 AM_INIT_AUTOMAKE([-Wall -Werror foreign])
 AC_CONFIG_SRCDIR([src/parser.h])
 AC_CONFIG_HEADERS([src/config.h])
index 027a699..d017853 100644 (file)
@@ -1,5 +1,5 @@
 Name:           tizen-platform-wrapper
-Version:        1.0
+Version:        2.0
 Release:        0
 License:        LGPL-2.0
 Summary:        A toolkit to generate the libtizen-platform-config library
@@ -8,6 +8,7 @@ Group:          Development/Tools
 Source:         %{name}-%{version}.tar.bz2
 Source1001:     %{name}.manifest
 Requires:       gperf
+Requires:       /usr/bin/sha256sum
 
 %description
 A toolkit to generate the libtizen-platform-config library in tizen-platform-config.
index bf994a3..987247a 100644 (file)
@@ -4,6 +4,7 @@ tzplatform_tool_SOURCES = buffer.c \
                           foreign.c \
                           heap.c \
                           parser.c \
+                          sha256sum.c \
                           toolbox.c
 
 dist_pkgdata_DATA = buffer.c \
@@ -16,7 +17,16 @@ dist_pkgdata_DATA = buffer.c \
                     parser.h \
                     scratch.c \
                     scratch.h \
-                    tzplatform_config.c \
+                    context.c \
+                    context.h \
+                    hashing.c \
+                    hashing.h \
+                    init.c \
+                    init.h \
+                    shared-api.c \
+                    shared-api.h \
+                    static-api.c \
+                    tzplatform_config.sym \
                     tzplatform_config.h \
                     tzplatform_get.c \
                     passwd.h \
index 1a72e76..92e9bf3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
index cd86040..28b43be 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
diff --git a/src/build.sh b/src/build.sh
new file mode 100755 (executable)
index 0000000..2ccaa48
--- /dev/null
@@ -0,0 +1,20 @@
+#!/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-config.so buffer.o   foreign.o  heap.o  parser.o  scratch.o context.o  hashing.o  init.o  passwd.o  shared-api.o
+d gcc -o get tzplatform_get.o static-api.o -L. -ltzplatform-config
+
+
diff --git a/src/context.c b/src/context.c
new file mode 100644 (file)
index 0000000..4b1e49d
--- /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"
+
+
+inline 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)
+inline 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)
+inline 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..2612f19
--- /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_];
+};
+
+inline uid_t get_uid(struct tzplatform_context *context);
+
+#if _FOREIGN_HAS_(EUID)
+inline uid_t get_euid(struct tzplatform_context *context);
+#endif
+
+#if _FOREIGN_HAS_(GID)
+inline gid_t get_gid(struct tzplatform_context *context);
+#endif
+
+#endif
+
index dab1c21..a19f6ad 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
index 3c9066f..646a41d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
diff --git a/src/hashing.c b/src/hashing.c
new file mode 100644 (file)
index 0000000..c22b4c0
--- /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_];
+
+inline 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..5ce816e
--- /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
+
+inline int hashid(const char *text, unsigned int len);
+const char *keyname(int id);
+
+#endif
+
index 2e4365d..d091f51 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
index d9713e5..744e6c0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
similarity index 50%
rename from src/tzplatform_config.c
rename to src/init.c
index 2b27401..76868cc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
@@ -39,6 +39,7 @@
 #include <stdarg.h>
 #include <alloca.h>
 #include <pwd.h>
+#include <assert.h>
 
 #ifndef NOT_MULTI_THREAD_SAFE
 #include <pthread.h>
@@ -56,6 +57,9 @@
 #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_(EHOME) \
                     || _FOREIGN_HAS_(EUSER) )
 
-#define _USER_NOT_SET_  ((uid_t)-1)
-
 /* local and static variables */
 static const char metafilepath[] = CONFIGPATH;
 static const char emptystring[] = "";
-static const char *var_names[_TZPLATFORM_VARIABLES_COUNT_];
-
-enum STATE { RESET=0, ERROR, VALID };
-
-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_];
-};
 
 /* structure for reading config files */
 struct reading {
@@ -93,16 +82,6 @@ struct reading {
     size_t offsets[_TZPLATFORM_VARIABLES_COUNT_];
 };
 
-static struct tzplatform_context global_context = {
-#ifndef NOT_MULTI_THREAD_SAFE
-    .mutex = PTHREAD_MUTEX_INITIALIZER,
-#endif
-    .state = RESET,
-    .user = _USER_NOT_SET_
-};
-
-#include "hash.inc"
-
 /* write the error message */
 static void writerror( const char *format, ...)
 {
@@ -114,37 +93,6 @@ static void writerror( const char *format, ...)
     fprintf( stderr, "\n");
 }
 
-static 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)
-static 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)
-static gid_t get_gid(struct tzplatform_context *context)
-{
-    return getgid();
-}
-#endif
-
 #if _HAS_IDS_
 /* fill the foreign variables for ids */
 static void foreignid( struct reading *reading)
@@ -333,15 +281,15 @@ static const char *getcb( struct parsing *parsing,
 {
     struct parsinfo info;
     const char *result;
-    const struct varassoc *vara;
     size_t offset;
     struct reading *reading = parsing->data;
+    int id;
 
     /* try to find a tzplatform variable */
-    vara = hashvar( key, length);
-    if (vara) {
+    id = hashid(key, length);
+    if (id >= 0) {
         /* found: try to use it */
-        offset = reading->offsets[(int)vara->id];
+        offset = reading->offsets[id];
         if (offset != HNULL)
             result = heap_address( &reading->context->heap, offset);
         else 
@@ -370,16 +318,16 @@ static int putcb( struct parsing *parsing,
             size_t begin_pos, size_t end_pos)
 {
     struct parsinfo info;
-    const struct varassoc *vara;
     size_t offset;
     char *string;
     struct reading *reading = parsing->data;
+    int id;
 
     /* try to find a tzplatform variable */
-    vara = hashvar( key, key_length);
-    if (vara) {
+    id = hashid( key, key_length);
+    if (id >= 0) {
         /* check that the variable isn't already defined */
-        offset = reading->offsets[(int)vara->id];
+        offset = reading->offsets[id];
         if (offset != HNULL) {
             reading->errcount++;
             parse_utf8_info( parsing, &info, begin_pos);
@@ -396,7 +344,7 @@ static int putcb( struct parsing *parsing,
         }
         else {
             /* record the variable value */
-            reading->offsets[(int)vara->id] = offset;
+            reading->offsets[id] = offset;
             string = heap_address( &reading->context->heap, offset);
             memcpy( string, value, value_length);
             string[value_length] = 0;
@@ -415,7 +363,7 @@ static int putcb( struct parsing *parsing,
 }
 
 /* initialize the environment */
-static void initialize(struct tzplatform_context *context)
+inline void initialize(struct tzplatform_context *context)
 {
     struct buffer buffer;
     struct parsing parsing;
@@ -475,367 +423,9 @@ static void initialize(struct tzplatform_context *context)
             context->values[i] = heap_address( &context->heap, offset);
         else
             writerror( "the variable %s isn't defined in file %s",
-                tzplatform_getname((enum tzplatform_variable)i), metafilepath);
+                keyname(i), metafilepath);
             /* TODO undefined variable */;
     }
     context->state = VALID;
 }
 
-inline static void lock(struct tzplatform_context *context)
-{
-#ifndef NOT_MULTI_THREAD_SAFE
-    pthread_mutex_lock( &context->mutex);
-#endif
-}
-
-inline static void unlock(struct tzplatform_context *context)
-{
-#ifndef NOT_MULTI_THREAD_SAFE
-    pthread_mutex_unlock( &context->mutex);
-#endif
-}
-
-static const char *get_lock(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    const char *result;
-    int offset;
-
-    lock( context);
-    offset = (int)id;
-    if (offset < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= offset) {
-        /*error("invalid id"); TODO*/
-        result = NULL;
-    }
-    else {
-        if (context->state == RESET)
-            initialize( context);
-        result = context->state == ERROR ? NULL : context->values[offset];
-    }
-    return result;
-}
-
-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);
-}
-
-int tzplatform_getcount()
-{
-    return (int)_TZPLATFORM_VARIABLES_COUNT_;
-}
-
-const char* tzplatform_getname(enum tzplatform_variable id)
-{
-    const struct varassoc *iter, *end;
-    const char *result;
-    int offset;
-
-    offset = (int)id;
-    if (offset < 0 || (int)_TZPLATFORM_VARIABLES_COUNT_ <= offset) {
-        /*error("invalid id"); TODO*/
-        result = NULL;
-    }
-    else {
-        if (!var_names[0]) {
-            iter = namassoc;
-            end = iter + (sizeof namassoc / sizeof namassoc[0]);
-            while (iter != end) {
-                if (iter->offset >= 0) 
-                    var_names[(int)iter->id] = varpool + iter->offset;
-                iter++;
-            }
-        }
-        result = var_names[offset];
-    }
-    return result;
-}
-
-enum tzplatform_variable tzplatform_getid(const char *name)
-{
-    const struct varassoc *vara = hashvar( name, strlen(name));
-    return vara ? vara->id : _TZPLATFORM_VARIABLES_INVALID_;
-}
-
-const char* tzplatform_getenv(enum tzplatform_variable id) 
-{
-    return tzplatform_context_getenv( &global_context, id);
-}
-
-const char* tzplatform_context_getenv(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    const char *array[2];
-    const char *result = get_lock( context, id);
-    if (result != NULL) {
-        array[0] = result;
-        array[1] = NULL;
-        result = scratchcat( 0, array);
-    }
-    unlock( context);
-    return result;
-}
-
-int tzplatform_getenv_int(enum tzplatform_variable id)
-{
-    return tzplatform_context_getenv_int( &global_context, id);
-}
-
-int tzplatform_context_getenv_int(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    const char *value = get_lock( context, id);
-    int result = value==NULL ? -1 : atoi(value);
-    unlock( context);
-    return result;
-}
-
-const char* tzplatform_mkstr(enum tzplatform_variable id, const char * str)
-{
-    return tzplatform_context_mkstr( &global_context, id, str);
-}
-
-const char* tzplatform_context_mkstr(struct tzplatform_context *context, enum tzplatform_variable id, const char *str)
-{
-    const char *array[3];
-    const char *result = get_lock( context, id);
-    if (result != NULL) {
-        array[0] = result;
-        array[1] = str;
-        array[2] = NULL;
-        result = scratchcat( 0, array);
-    }
-    unlock( context);
-    return result;
-}
-
-const char* tzplatform_mkpath(enum tzplatform_variable id, const char * path)
-{
-    return tzplatform_context_mkpath( &global_context, id, path);
-}
-
-const char* tzplatform_context_mkpath(struct tzplatform_context *context, enum tzplatform_variable id, const char *path)
-{
-    const char *array[3];
-    const char *result = get_lock( context, id);
-    if (result != NULL) {
-        array[0] = result;
-        array[1] = path;
-        array[2] = NULL;
-        result = scratchcat( 1, array);
-    }
-    unlock( context);
-    return result;
-}
-
-const char* tzplatform_mkpath3(enum tzplatform_variable id, const char * path,
-                                                        const char* path2)
-{
-    return tzplatform_context_mkpath3( &global_context, id, path, path2);
-}
-
-const char* tzplatform_context_mkpath3(struct tzplatform_context *context, enum tzplatform_variable id, const char *path,
-                                                            const char *path2)
-{
-    const char *array[4];
-    const char *result = get_lock( context, id);
-    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* tzplatform_mkpath4(enum tzplatform_variable id, const char * path,
-                                          const char* path2, const char *path3)
-{
-    return tzplatform_context_mkpath4( &global_context, id, path, path2, path3);
-}
-
-const char* tzplatform_context_mkpath4(struct tzplatform_context *context, enum tzplatform_variable id, const char *path,
-                                        const char *path2, const char *path3)
-{
-    const char *array[5];
-    const char *result = get_lock( context, id);
-    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 tzplatform_getuid(enum tzplatform_variable id)
-{
-    return tzplatform_context_getuid( &global_context, id);
-}
-
-uid_t tzplatform_context_getuid(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    uid_t result = (uid_t)-1;
-    const char *value = get_lock( context, id);
-    if (value != NULL) {
-        pw_get_uid( value, &result);
-    }
-    unlock( context);
-    return result;
-}
-
-gid_t tzplatform_getgid(enum tzplatform_variable id)
-{
-    return tzplatform_context_getgid( &global_context, id);
-}
-
-gid_t tzplatform_context_getgid(struct tzplatform_context *context, enum tzplatform_variable id)
-{
-    gid_t result = (uid_t)-1;
-    const char *value = get_lock( context, id);
-    if (value != NULL) {
-        pw_get_gid( value, &result);
-    }
-    unlock( context);
-    return result;
-}
-
-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)
-{
-    int result;
-
-    result = 0;
-    lock( context);
-    if (context->user != uid) {
-           if (uid != _USER_NOT_SET_ && !pw_has_uid( uid))
-            result = -1;
-        else {
-            if (context->state == VALID)
-                heap_destroy( &context->heap);
-            context->state = RESET;
-            context->user = uid;
-        }
-    }
-    unlock( context);
-
-    return result;
-}
-
-uid_t tzplatform_get_user()
-{
-    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_);
-}
-
-#ifdef TEST
-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/init.h b/src/init.h
new file mode 100644 (file)
index 0000000..446431c
--- /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
+
+inline void initialize(struct tzplatform_context *context);
+
+#endif
+
index 0b95916..fa8d94d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
index d8390f4..0ee30e8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
index 5aa32b8..287d375 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
index d3c1d1c..f9de75c 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
index 7c71da4..6f25b54 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
index c571348..3b15bcb 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
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..054d093
--- /dev/null
@@ -0,0 +1,184 @@
+/*
+ * 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 "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);
+}
+
+#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
+
+
index bc7911d..0c48f36 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
@@ -38,6 +38,7 @@
 #include "heap.h"
 #include "buffer.h"
 #include "foreign.h"
+#include "sha256sum.h"
 
 /*======================================================================*/
 
@@ -87,6 +88,7 @@ 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\
 ";
 
@@ -113,7 +115,7 @@ static char genh_tail[] = "\
 static char gperf_head[] = "\
 struct varassoc {\n\
   int offset;\n\
-  enum tzplatform_variable id;\n\
+  int id;\n\
 };\n\
 %%\n\
 ";
@@ -132,6 +134,16 @@ static char rpm_head[] = "\
 \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 */
@@ -150,7 +162,7 @@ static int errcount = 0;
 static int dependant = 0;
 
 /* action to perform */
-static enum { CHECK, PRETTY, GENC, GENH, RPM } action = CHECK;
+static enum { CHECK, PRETTY, GENC, GENH, RPM, SIGNUP } action = CHECK;
 
 /* output of error */
 static int notstderr = 0;
@@ -477,6 +489,51 @@ static const char *getcb( struct parsing *parsing,
 
 /*======================================================================*/
 
+/* 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)
 {
@@ -512,6 +569,12 @@ 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;
@@ -535,6 +598,12 @@ static int genc(FILE *output)
     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;
@@ -592,6 +661,12 @@ 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;
@@ -605,6 +680,58 @@ static int rpm( FILE *output)
     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)
+            return status;
+        status = sha256sum_add_data(sum, &term, 1);
+        if (status < 0)
+            return status;
+    }
+
+    status = sha256sum_get(sum, signup);
+    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()
 {
@@ -657,6 +784,9 @@ static int process()
     case RPM:
         rpm( stdout);
         break;
+    case SIGNUP:
+        signup( stdout);
+        break;
     }
 
     buffer_destroy( &buffer);
@@ -699,7 +829,11 @@ static int arguments( char **argv)
             action = RPM;
             argv++;
         }
-        else if (0 == strcmp( *argv, "help")) {
+        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;
index ca78b35..5a98226 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
@@ -230,8 +230,6 @@ void tzplatform_context_destroy(struct tzplatform_context *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_context_reset(struct tzplatform_context *context);
@@ -247,6 +245,7 @@ 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);
diff --git a/src/tzplatform_config.sym b/src/tzplatform_config.sym
new file mode 100644 (file)
index 0000000..49146b8
--- /dev/null
@@ -0,0 +1,38 @@
+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_get_user;
+               tzplatform_context_reset;
+               tzplatform_context_reset_user;
+               tzplatform_context_set_user;
+               tzplatform_getname;
+               tzplatform_get_user;
+               tzplatform_reset;
+               tzplatform_reset_user;
+               tzplatform_set_user;
+
+       local:
+               *;
+};
+
index f254f54..4273460 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2013 Intel Corporation.
+ * 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
index 3df2ca4..59224d9 100644 (file)
@@ -2,10 +2,11 @@ prefix=@prefix@
 exec_prefix=@exec_prefix@
 libdir=@libdir@
 includedir=@includedir@
-srcdir=@datadir@
+datarootdir=@datarootdir@
+datadir=@datadir@
 Name: @PACKAGE_NAME@
 Description: Tizen Platform Wrapper library.
 Version: @PACKAGE_VERSION@
 URL: @PACKAGE_URL@
 Libs: -L${libdir} 
-Cflags: -I${includedir}/
\ No newline at end of file
+Cflags: -I${includedir}/