Preparation of cynara for better usage of systemd.
authorAdam Malinowski <a.malinowsk2@partner.samsung.com>
Thu, 22 May 2014 11:15:34 +0000 (13:15 +0200)
committerRafal Krypa <r.krypa@samsung.com>
Thu, 3 Jul 2014 12:19:08 +0000 (14:19 +0200)
Change-Id: Ie210b1422ace6ebe45a3804fd52b701fe9fe13f3

packaging/cynara.spec
src/service/main/main.cpp
systemd/cynara.service

index bbf664a..52ffb9a 100644 (file)
@@ -11,6 +11,7 @@ Source1003:    libcynara-admin.manifest
 BuildRequires: cmake
 BuildRequires: zip
 BuildRequires: pkgconfig(libsystemd-daemon)
+BuildRequires: pkgconfig(libsystemd-journal)
 %{?systemd_requires}
 
 %description
@@ -76,7 +77,7 @@ export FFLAGS="$FFLAGS -DTIZEN_DEBUG_ENABLE"
 export LDFLAGS+="-Wl,--rpath=%{_libdir}"
 
 %cmake . -DVERSION=%{version} \
-        -DCMAKE_BUILD_TYPE=%{?build_type:%build_type}%{!?build_type:RELEASE} \
+        -DCMAKE_BUILD_TYPE=%{?build_type:%build_type}%{!?build_type:DEBUG} \
         -DCMAKE_VERBOSE_MAKEFILE=ON
 make %{?jobs:-j%jobs}
 
@@ -84,24 +85,37 @@ make %{?jobs:-j%jobs}
 rm -rf %{buildroot}
 %make_install
 
-mkdir -p %{buildroot}/usr/lib/systemd/system/multi-user.target.wants
-ln -s ../cynara.service %{buildroot}/usr/lib/systemd/system/multi-user.target.wants/cynara.service
 mkdir -p %{buildroot}/usr/lib/systemd/system/sockets.target.wants
 ln -s ../cynara.socket %{buildroot}/usr/lib/systemd/system/sockets.target.wants/cynara.socket
 ln -s ../cynara-admin.socket %{buildroot}/usr/lib/systemd/system/sockets.target.wants/cynara-admin.socket
 
 %post
+USER=%{name}
+GROUP=%{name}
+
+### Add file capabilities if needed
+### setcap/getcap binary are useful. To use them you must install libcap and libcap-tools packages
+### In such case uncomment Requires with those packages
+
 systemctl daemon-reload
+
 if [ $1 = 1 ]; then
-    # installation
-    systemctl start cynara.service
-fi
 
-if [ $1 = 2 ]; then
-    # update
-    systemctl restart cynara.service
+    id -g $GROUP 2> /dev/null
+    if [ $? -eq 1 ]; then
+        groupadd $GROUP
+    fi
+
+    id -u $USER 2> /dev/null
+    if [ $? -eq 1 ]; then
+    useradd -m $USER
+    fi
+
+    systemctl enable %{name}.service
 fi
 
+systemctl restart %{name}.service
+
 /sbin/ldconfig
 
 %preun
@@ -111,10 +125,14 @@ if [ $1 = 0 ]; then
 fi
 
 %postun
+USER=%{name}
+GROUP=%{name}
 if [ $1 = 0 ]; then
-    # unistall
+    userdel -r $USER 2> /dev/null
+    groupdel $GROUP 2> /dev/null
     systemctl daemon-reload
 fi
+
 /sbin/ldconfig
 
 %post -n libcynara-client -p /sbin/ldconfig
@@ -138,7 +156,6 @@ fi
 %license LICENSE
 %attr(755,root,root) /usr/bin/cynara
 %{_libdir}/libcynara-commons.so*
-%attr(-,root,root) /usr/lib/systemd/system/multi-user.target.wants/cynara.service
 %attr(-,root,root) /usr/lib/systemd/system/cynara.service
 %attr(-,root,root) /usr/lib/systemd/system/cynara.target
 %attr(-,root,root) /usr/lib/systemd/system/sockets.target.wants/cynara.socket
index b34def0..7a6ffac 100644 (file)
@@ -1,6 +1,8 @@
 /*
  *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
  *
+ *  Contact: Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ *
  *  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
 /*
  * @file        main.cpp
  * @author      Lukasz Wojciechowski <l.wojciechow@partner.samsung.com>
+ * @author      Adam Malinowski <a.malinowsk2@partner.samsung.com>
  * @version     1.0
  * @brief       Main Cynara daemon file
  */
 
-int main(void) {
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/prctl.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <log/log.h>
+
+#include <systemd/sd-journal.h>
+#include <systemd/sd-daemon.h>
+
+// Temporary "exit tool"
+// After implementing the real main we must provide better way to inform daemon
+// about our will.
+static int goodbye = 0;
+
+// Handle kill message from systemd
+// Typically signal SIGTERM is used - depends on configuration in service file
+void kill_handler(int sig) {
+    (void) sig;
+    LOGD("Cynara service is going down now");
+    // As said above, this exitting way should be changed
+    goodbye = 1;
+}
+
+int main(int argc, char **argv) {
+    (void) argc;
+    (void) argv;
+
+    int ret;
+    struct sigaction act;
+    int it = 0; // TODO: remove me soon
+
+    LOGI("Cynara service is started");
+
+    // Install kill handler - TERM signal will be delivered form systemd to kill this service
+    memset(&act, 0, sizeof(act));
+    act.sa_handler = &kill_handler;
+    if ((ret = sigaction(SIGTERM, &act, NULL)) < 0) {
+        LOGE("sigaction failed [%d]", ret);
+        return 1;
+    }
+
+    init_log();
+
+    // Do some initialization here
+    // Now notify systemd that cynara is ready to fulfil its destiny
+    ret = sd_notify(0, "READY=1");
+    if (ret == 0) {
+        LOGW("Cynara was not configured to notify its status");
+    } else if (ret < 0) {
+        LOGE("sd_notify failed [%d]", ret);
+    }
+
+    LOGD("Starting the real job");
+
+    while (!goodbye) {
+        TEMP_FAILURE_RETRY(sleep(1));
+
+        LOGD("Iteration: % 5d", it++);
+        // TODO: remove me soon
+    }
+
+    LOGD("Cynara service is stopped");
+
     return 0;
 }
 
index a88ea16..847a294 100644 (file)
@@ -1,11 +1,24 @@
 [Unit]
-Description=Start the cynara service
+Description=Cynara service
 
 [Service]
-Type=notify
 ExecStart=/usr/bin/cynara
+
+Type=notify
+
+KillMode=process
+TimeoutStopSec=3
+Restart=always
+
 Sockets=cynara.socket
 Sockets=cynara-admin.socket
 
+UMask=0000
+User=cynara
+Group=cynara
+NoNewPrivileges=true
+
+#Environment="CYNARA_LOG_LEVEL=LOG_DEBUG"
+
 [Install]
 WantedBy=multi-user.target