Adding uninstallation of widgets
authorJosé Bollo <jose.bollo@iot.bzh>
Thu, 4 Feb 2016 14:43:49 +0000 (15:43 +0100)
committerJosé Bollo <jose.bollo@iot.bzh>
Thu, 4 Feb 2016 14:48:32 +0000 (15:48 +0100)
Change-Id: I8558a77312590181de5313c89ea4c9bdb9b477c7
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/CMakeLists.txt
src/afm-system-daemon.c
src/secmgr-wrap.c
src/secmgr-wrap.h
src/simulation/security-manager.h
src/wgtpkg-install.c
src/wgtpkg-uninstall.c [new file with mode: 0644]
src/wgtpkg-uninstall.h [new file with mode: 0644]

index 9379142..0b055e8 100644 (file)
@@ -75,6 +75,7 @@ add_library(wgtpkg
        wgtpkg-files.c
        wgtpkg-install.c
        wgtpkg-permissions.c
+       wgtpkg-uninstall.c
        wgtpkg-workdir.c
        wgtpkg-xmlsec.c
        wgtpkg-zip.c
index f02ff3d..ab87c97 100644 (file)
@@ -31,6 +31,7 @@
 #include "afm-db.h"
 #include "wgt-info.h"
 #include "wgtpkg-install.h"
+#include "wgtpkg-uninstall.h"
 
 static const char appname[] = "afm-system-daemon";
 static const char *rootdir = NULL;
@@ -117,7 +118,34 @@ static void on_install(struct jreq *jreq, struct json_object *req)
 
 static void on_uninstall(struct jreq *jreq, struct json_object *req)
 {
-       jbus_reply_error_s(jreq, "\"not yet implemented\"");
+       const char *idaver;
+       const char *root;
+       int rc;
+
+       /* scan the request */
+       switch (json_object_get_type(req)) {
+       case json_type_string:
+               idaver = json_object_get_string(req);
+               root = rootdir;
+               break;
+       case json_type_object:
+               idaver = j_get_string(req, "id", NULL);
+               if (idaver != NULL) {
+                       root = j_get_string(req, "root", rootdir);
+                       break;
+               }
+       default:
+               jbus_reply_error_s(jreq, error_bad_request);
+               return;
+       }
+
+       /* install the widget */
+       rc = uninstall_widget(idaver, root);
+       if (rc) {
+               jbus_reply_error_s(jreq, "\"uninstallation had error\"");
+               return;
+       }
+       jbus_reply_s(jreq, "true");
 }
 
 static int daemonize()
index 78680a5..fee9d64 100644 (file)
@@ -80,6 +80,17 @@ int secmgr_install()
        return retcode(rc);
 }
 
+int secmgr_uninstall()
+{
+       int rc;
+       assert(request != NULL);
+       rc = security_manager_app_uninstall(request);
+       if (rc != SECURITY_MANAGER_SUCCESS)
+               ERROR("security_manager_app_uninstall failed");
+       secmgr_cancel();
+       return retcode(rc);
+}
+
 int secmgr_permit(const char *permission)
 {
        int rc;
index 106b1b5..db5d0bb 100644 (file)
  limitations under the License.
 */
 
-int secmgr_init(const char *id);
-void secmgr_cancel();
-int secmgr_install();
-int secmgr_permit(const char *permission);
-int secmgr_path_public_read_only(const char *pathname);
-int secmgr_path_read_only(const char *pathname);
-int secmgr_path_read_write(const char *pathname);
+extern int secmgr_init(const char *id);
+extern void secmgr_cancel();
+extern int secmgr_install();
+extern int secmgr_uninstall();
+extern int secmgr_permit(const char *permission);
+extern int secmgr_path_public_read_only(const char *pathname);
+extern int secmgr_path_read_only(const char *pathname);
+extern int secmgr_path_read_write(const char *pathname);
 
-int secmgr_prepare_exec(const char *appid);
+extern int secmgr_prepare_exec(const char *appid);
index 6bc379a..59c356d 100644 (file)
@@ -52,6 +52,9 @@ static int diese = 0;
 #define security_manager_app_install(r) \
  (printf("security_manager_app_install(%p)\n",r), SECURITY_MANAGER_SUCCESS)
 
+#define security_manager_app_uninstall(r) \
+ (printf("security_manager_app_uninstall(%p)\n",r), SECURITY_MANAGER_SUCCESS)
+
 #define security_manager_prepare_app(a) \
  (printf("security_manager_prepare_app(%s)\n",a), SECURITY_MANAGER_SUCCESS)
 
index b8646fa..deb2741 100644 (file)
@@ -137,7 +137,7 @@ static int install_icon(const struct wgt_desc *desc)
        int rc;
 
        create_directory(FWK_ICON_DIR, 0755, 1);
-       rc = snprintf(link, sizeof link, "%s/%s@%s", FWK_ICON_DIR, desc->id, desc->ver);
+       rc = snprintf(link, sizeof link, "%s/%s", FWK_ICON_DIR, desc->idaver);
        if (rc >= sizeof link) {
                ERROR("link to long in install_icon");
                errno = EINVAL;
diff --git a/src/wgtpkg-uninstall.c b/src/wgtpkg-uninstall.c
new file mode 100644 (file)
index 0000000..7516c3f
--- /dev/null
@@ -0,0 +1,102 @@
+/*
+ Copyright 2015 IoT.bzh
+
+ author: José Bollo <jose.bollo@iot.bzh>
+
+ 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 <limits.h>
+#include <errno.h>
+#include <string.h>
+#include <stdio.h>
+#include <assert.h>
+#include <unistd.h>
+
+#include "verbose.h"
+#include "utils-dir.h"
+#include "secmgr-wrap.h"
+
+/* uninstall the widget of idaver */
+int uninstall_widget(const char *idaver, const char *root)
+{
+       char *id;
+       char *ver;
+       char path[PATH_MAX];
+       const char *at;
+       int rc, rc2;
+
+       NOTICE("-- UNINSTALLING widget of id %s from %s --", idaver, root);
+
+       /* find the last '@' of the id */
+       at = strrchr(idaver, '@');
+       if (at == NULL) {
+               ERROR("bad widget id '%s', no @", idaver);
+               errno = EINVAL;
+               return -1;
+       }
+       id = strndupa(idaver, at - idaver);
+       ver = strdupa(at + 1);
+
+       /* compute the path */
+       rc = snprintf(path, sizeof path, "%s/%s/%s", root, id, ver);
+       if (rc >= sizeof path) {
+               ERROR("bad widget id '%s', too long", idaver);
+               errno = EINVAL;
+               return -1;
+       }
+
+       /* removes the directory of the application */
+       rc = remove_directory(path, 1);
+       if (rc < 0) {
+               ERROR("error while removing directory '%s': %m", path);
+               return -1;
+       }
+
+       /* removes the icon of the application */
+       rc = snprintf(path, sizeof path, "%s/%s", FWK_ICON_DIR, idaver);
+       assert(rc < sizeof path);
+       rc = unlink(path);
+       if (rc < 0)
+               ERROR("can't removing '%s': %m", path);
+
+       /* removes the parent directory if empty */
+       rc2 = snprintf(path, sizeof path, "%s/%s", root, id);
+       assert(rc2 < sizeof path);
+       rc2 = rmdir(path);
+       if (rc < 0 && errno == ENOTEMPTY)
+               return rc;
+       if (rc < 0) {
+               ERROR("error while removing directory '%s': %m", path);
+               return -1;
+       }
+
+       /*
+        * parent directory removed: last occurrence of the application
+        * uninstall it for the security-manager
+        */
+       rc2 = secmgr_init(id);
+       if (rc2) {
+               ERROR("can't init security manager context");
+               return -1;
+       }
+       rc2 = secmgr_uninstall();
+       if (rc2) {
+               ERROR("can't uninstall security manager context");
+               return -1;
+       }
+       return rc;
+}
+
diff --git a/src/wgtpkg-uninstall.h b/src/wgtpkg-uninstall.h
new file mode 100644 (file)
index 0000000..f3b4c5b
--- /dev/null
@@ -0,0 +1,20 @@
+/*
+ Copyright 2015 IoT.bzh
+
+ author: José Bollo <jose.bollo@iot.bzh>
+
+ 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.
+*/
+
+extern int uninstall_widget(const char *idaver, const char *root);
+