add a tool for preload tpk install 26/60126/6 accepted/tizen/common/20160308.142654 accepted/tizen/ivi/20160308.100818 accepted/tizen/mobile/20160308.100734 accepted/tizen/tv/20160308.100745 accepted/tizen/wearable/20160308.100802 submit/tizen/20160308.022332
authorjongmyeongko <jongmyeong.ko@samsung.com>
Tue, 23 Feb 2016 12:59:04 +0000 (21:59 +0900)
committerjongmyeongko <jongmyeong.ko@samsung.com>
Fri, 4 Mar 2016 00:26:21 +0000 (09:26 +0900)
Change-Id: I87fe3e12dae579f3f387d6774981971f5f06b8a3
Signed-off-by: jongmyeongko <jongmyeong.ko@samsung.com>
CMakeLists.txt
packaging/pkgmgr-tool.spec
src/install_preload_tpk.c [new file with mode: 0644]

index 8123f615a081db9066733b98e289b7d68f60ef7e..44cef0b018ef76801f53ed3c487a4b71a3071467 100644 (file)
@@ -62,6 +62,10 @@ ADD_EXECUTABLE(pkg_initdb src/pkg_initdb.c)
 TARGET_LINK_LIBRARIES(pkg_initdb ${pkgs_initdb_LDFLAGS})
 INSTALL(TARGETS pkg_initdb DESTINATION bin)
 
+ADD_EXECUTABLE(install_preload_tpk src/install_preload_tpk.c)
+TARGET_LINK_LIBRARIES(install_preload_tpk ${pkgs_test_LDFLAGS})
+INSTALL(TARGETS install_preload_tpk DESTINATION bin)
+
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/10_package-manager-add.post  DESTINATION ${SYSCONF_INSTALL_DIR}/gumd/useradd.d/)
 
 INSTALL(FILES ${CMAKE_CURRENT_SOURCE_DIR}/data/mime.wac.xml DESTINATION /usr/share/mime/packages/)
index 24e84be2e519916233e951c33dfafd81a08c1bc4..dc4a2a5efdcdbaae1e596f00967ed6ee12f1bb1b 100644 (file)
@@ -57,6 +57,7 @@ chsmack -a '*' %{TZ_SYS_RW_PACKAGES}
 
 %posttrans
 pkg_initdb
+install_preload_tpk
 
 %files
 %manifest %{name}.manifest
@@ -69,6 +70,7 @@ pkg_initdb
 %{_bindir}/pkg_getsize
 %{_bindir}/pkg_clearcache
 %{_bindir}/pkginfo
+%attr(0755,root,root) %{_bindir}/install_preload_tpk
 %{_datadir}/mime/packages/mime.wac.xml
 %{_datadir}/mime/packages/mime.tpk.xml
 %attr(0700,root,root) /etc/package-manager/pkgmgr-unzip-tpk.sh
diff --git a/src/install_preload_tpk.c b/src/install_preload_tpk.c
new file mode 100644 (file)
index 0000000..5bdbdce
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#define _GNU_SOURCE
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <errno.h>
+
+#include <tzplatform_config.h>
+
+#define OWNER_ROOT 0
+#define GLOBAL_USER tzplatform_getuid(TZ_SYS_GLOBALAPP_USER)
+#define BUFSZE 4096
+
+#ifdef _E
+#undef _E
+#endif
+#define _E(fmt, arg...) fprintf(stderr, "[TPK_PRELOAD_INSTALL][E][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
+
+#ifdef _D
+#undef _D
+#endif
+#define _D(fmt, arg...) fprintf(stderr, "[TPK_PRELOAD_INSTALL][D][%s,%d] "fmt"\n", __FUNCTION__, __LINE__, ##arg);
+
+#define BACKEND_CMD "/usr/bin/tpk-backend"
+
+static int _install_preload_tpk(uid_t uid, const char *directory)
+{
+       DIR *dir;
+       struct dirent file_info, *result;
+       int ret;
+       char buf[BUFSZE];
+
+       dir = opendir(directory);
+       if (!dir) {
+               _E("Failed to access the [%s] because %s", directory,
+                               strerror_r(errno, buf, sizeof(buf)));
+               return -1;
+       }
+
+       _D("Loading tpk files from %s", directory);
+
+       for (ret = readdir_r(dir, &file_info, &result);
+                       ret == 0 && result != NULL;
+                       ret = readdir_r(dir, &file_info, &result)) {
+               if (file_info.d_name[0] == '.')
+                       continue;
+
+               snprintf(buf, sizeof(buf), "%s/%s", directory, file_info.d_name);
+               _D("tpk file %s", buf);
+
+               pid_t pid = fork();
+               if (pid == 0) {
+                       setuid(uid);
+                       execl(BACKEND_CMD, BACKEND_CMD, "-i", buf, "--preload",
+                             (char*)NULL);
+               } else if (pid < 0) {
+                       _E("failed to fork and execute %s!", BACKEND_CMD);
+                       closedir(dir);
+                       return -1;
+               }
+               if (pid > 0) {
+                       int status = 0;
+                       waitpid(pid, &status, 0);
+               }
+       }
+
+       closedir(dir);
+
+       return 0;
+}
+
+static int _is_authorized(uid_t uid)
+{
+       /* install_preload_tpk should be called by as root privilege. */
+       if ((uid_t) OWNER_ROOT == uid)
+               return 1;
+       else
+               return 0;
+}
+
+int main(int argc, char *argv[])
+{
+       int ret;
+       const char *dir = tzplatform_mkpath(TZ_SYS_RO_APP, ".preload-tpk");
+       uid_t uid = getuid();
+
+       if (!_is_authorized(uid)) {
+               _E("You are not an authorized user!");
+               return -1;
+       }
+
+       return _install_preload_tpk(uid, dir);
+}