update the latest source
[platform/core/connectivity/smartcard-service.git] / server / smartcard-daemon.cpp
1 /*
2 * Copyright (c) 2012 Samsung Electronics Co., Ltd All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17
18 /* standard library header */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <vector>
25 #include <dirent.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28
29 /* SLP library header */
30
31 /* local header */
32 #include "Debug.h"
33 #include "Channel.h"
34 #include "ServerResource.h"
35 #include "ServerSEService.h"
36
37 /* definition */
38 using namespace std;
39 using namespace smartcard_service_api;
40
41 /* global variable */
42
43 ServerResource *serverResource = NULL;
44
45 static void daemonize(void)
46 {
47         pid_t pid, sid;
48
49         /* already a daemon */
50         if (getppid() == 1)
51                 return;
52
53         /* Fork off the parent process */
54         pid = fork();
55         if (pid < 0)
56         {
57                 exit(EXIT_FAILURE);
58         }
59
60         if (pid > 0)
61         {
62                 exit(EXIT_SUCCESS); /*Killing the Parent Process*/
63         }
64
65         /* At this point we are executing as the child process */
66         umask(0);
67
68         /* Create a new SID for the child process */
69         sid = setsid();
70         if (sid < 0)
71         {
72                 exit(EXIT_FAILURE);
73         }
74
75         /* Change the current working directory. */
76         if ((chdir("/")) < 0)
77         {
78                 exit(EXIT_FAILURE);
79         }
80
81         close(STDIN_FILENO);
82         close(STDOUT_FILENO);
83         close(STDERR_FILENO);
84 }
85
86 int main()
87 {
88         GMainLoop* loop = NULL;
89
90         daemonize();
91
92         if (!g_thread_supported())
93         {
94                 g_thread_init(NULL);
95         }
96
97         serverResource = &ServerResource::getInstance();
98         ServerIPC::getInstance()->createListenSocket();
99
100         loop = g_main_new(TRUE);
101         g_main_loop_run(loop);
102
103         return 0;
104 }