support Pause/Resume with new connection
[framework/web/download-provider.git] / src / download-provider-main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <glib.h>
5 #include <glib-object.h>
6 #include <pthread.h>
7
8 #include "download-provider-config.h"
9 #include "download-provider-log.h"
10
11 GMainLoop *gMainLoop = 0;       // need for libsoup, decided the life-time by mainloop.
12 pthread_t gRequestThreadPid;
13
14 int lock_download_provider_pid(char *path);
15 void *run_manage_download_server(void *args);
16
17 void TerminateDaemon(int signo)
18 {
19         TRACE_DEBUG_INFO_MSG("Received SIGTERM");
20         if (g_main_loop_is_running(gMainLoop))
21                 g_main_loop_quit(gMainLoop);
22 }
23
24 static gboolean CreateThreadFunc(void *data)
25 {
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);
31                 return FALSE;
32         }
33         // create thread for receiving the client request.
34         if (pthread_create
35                 (&gRequestThreadPid, &thread_attr, run_manage_download_server,
36                 data) != 0) {
37                 TRACE_DEBUG_MSG
38                         ("failed to create pthread for run_manage_download_server");
39                 if (g_main_loop_is_running(gMainLoop))
40                         g_main_loop_quit(gMainLoop);
41         }
42         return FALSE;
43 }
44
45 int main()
46 {
47         if (chdir("/") < 0) {
48                 TRACE_DEBUG_MSG("failed to call setsid or chdir");
49                 exit(EXIT_FAILURE);
50         }
51 #if 0
52         // close all console I/O
53         close(STDIN_FILENO);
54         close(STDOUT_FILENO);
55         close(STDERR_FILENO);
56 #endif
57
58         if (signal(SIGTERM, TerminateDaemon) == SIG_ERR) {
59                 TRACE_DEBUG_MSG("failed to register signal callback");
60                 exit(EXIT_FAILURE);
61         }
62         // write IPC_FD_PATH. and lock
63         if (lock_download_provider_pid(DOWNLOAD_PROVIDER_LOCK_PID) < 0) {
64                 TRACE_DEBUG_MSG
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);
68                 exit(EXIT_FAILURE);
69         }
70         // if exit socket file, delete it
71         if (access(DOWNLOAD_PROVIDER_IPC, F_OK) == 0) {
72                 unlink(DOWNLOAD_PROVIDER_IPC);
73         }
74         // libsoup need mainloop.
75
76         gMainLoop = g_main_loop_new(NULL, 0);
77
78         g_type_init();
79
80         g_idle_add(CreateThreadFunc, gMainLoop);
81
82         g_main_loop_run(gMainLoop);
83
84         TRACE_DEBUG_INFO_MSG("Download-Provider will be terminated.");
85
86         pthread_cancel(gRequestThreadPid);
87         pthread_join(gRequestThreadPid, NULL);
88
89         // if exit socket file, delete it
90         if (access(DOWNLOAD_PROVIDER_IPC, F_OK) == 0) {
91                 unlink(DOWNLOAD_PROVIDER_IPC);
92         }
93         // delete pid file
94         if (access(DOWNLOAD_PROVIDER_LOCK_PID, F_OK) == 0) {
95                 unlink(DOWNLOAD_PROVIDER_LOCK_PID);
96         }
97         exit(EXIT_SUCCESS);
98 }