5 #include <glib-object.h>
8 #include "download-provider-config.h"
9 #include "download-provider-log.h"
11 GMainLoop *gMainLoop = 0; // need for libsoup, decided the life-time by mainloop.
12 pthread_t gRequestThreadPid;
14 int lock_download_provider_pid(char *path);
15 void *run_manage_download_server(void *args);
17 void TerminateDaemon(int signo)
19 TRACE_DEBUG_MSG("Received SIGTERM");
20 if (g_main_loop_is_running(gMainLoop))
21 g_main_loop_quit(gMainLoop);
24 static gboolean CreateThreadFunc(void *data)
26 pthread_attr_t thread_attr;
27 if (pthread_attr_init(&thread_attr) != 0) {
28 TRACE_DEBUG_MSG("failed to init pthread attr");
29 if (g_main_loop_is_running(gMainLoop))
30 g_main_loop_quit(gMainLoop);
33 // create thread for receiving the client request.
35 (&gRequestThreadPid, &thread_attr, run_manage_download_server,
38 ("failed to create pthread for run_manage_download_server");
39 if (g_main_loop_is_running(gMainLoop))
40 g_main_loop_quit(gMainLoop);
48 TRACE_DEBUG_MSG("failed to call setsid or chdir");
52 // close all console I/O
58 if (signal(SIGTERM, TerminateDaemon) == SIG_ERR) {
59 TRACE_DEBUG_MSG("failed to register signal callback");
62 // write IPC_FD_PATH. and lock
63 if (lock_download_provider_pid(DOWNLOAD_PROVIDER_LOCK_PID) < 0) {
65 ("It need to check download-provider is already alive");
66 TRACE_DEBUG_MSG("Or fail to create pid file in (%s)",
67 DOWNLOAD_PROVIDER_LOCK_PID);
70 // if exit socket file, delete it
71 if (access(DOWNLOAD_PROVIDER_IPC, F_OK) == 0) {
72 unlink(DOWNLOAD_PROVIDER_IPC);
74 // libsoup need mainloop.
76 gMainLoop = g_main_loop_new(NULL, 0);
80 g_idle_add(CreateThreadFunc, gMainLoop);
82 g_main_loop_run(gMainLoop);
84 TRACE_DEBUG_MSG("Download-Provider will be terminated.");
86 pthread_cancel(gRequestThreadPid);
87 pthread_join(gRequestThreadPid, NULL);
89 // if exit socket file, delete it
90 if (access(DOWNLOAD_PROVIDER_IPC, F_OK) == 0) {
91 unlink(DOWNLOAD_PROVIDER_IPC);
94 if (access(DOWNLOAD_PROVIDER_LOCK_PID, F_OK) == 0) {
95 unlink(DOWNLOAD_PROVIDER_LOCK_PID);