Remove initscripts and add PIDFile to service file
[platform/core/system/power-manager.git] / main.c
1 /*
2  * power-manager
3  * Copyright (c) 2012 Samsung Electronics Co., Ltd.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16 */
17
18 /**
19  * @file        main.c
20  * @version     0.1
21  * @brief       Power Manager main file
22  *
23  * Process the user options, Daemonize the process, Start Main loop
24  */
25
26 /**
27  * @addtogroup POWER_MANAGER
28  * @{
29  *
30  * @section execution How to execute 
31  *   # powermanager {options...} <br>
32  *
33  * Options: <br><br>
34  *  &nbsp; -f --foreground<br>
35  *  &nbsp; &nbsp; Run as foreground process<br> <br>
36  *  &nbsp; -d --direct<br>
37  *  &nbsp; &nbsp; Start without notification<br> <br>
38  *
39  * @}
40  */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <getopt.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <string.h>
48 #include <limits.h>
49 #include <unistd.h>
50
51 #include "pm_core.h"
52
53 /**
54  * Print the usage
55  *
56  * @internal
57  */
58 void usage()
59 {
60         printf("Options: \n");
61         printf("  -f, --foreground         Run as foreground process\n");
62         printf("  -d, --direct             Start without notification\n");
63         printf
64             ("  -x, --xdpms              With LCD-onoff control by x-dpms \n");
65         printf("\n");
66
67         exit(0);
68 }
69
70 /**
71  * Pid file path
72  */
73 #define DEFAULT_PID_PATH "/var/run/power-manager.pid"
74
75 int main(int argc, char *argv[])
76 {
77         int c;
78         int runflags = 0;       /* run as daemon */
79         unsigned int flags = 0x0;       /* 0 : start with noti */
80
81         while (1) {
82                 int option_index = 0;
83                 static struct option long_options[] = {
84                         {"foreground", no_argument, NULL, 'f'},
85                         {"direct", no_argument, NULL, 'd'},
86                         {"xdpms", no_argument, NULL, 'x'},
87                         {0, 0, 0, 0}
88                 };
89
90                 c = getopt_long(argc, argv, "fdx", long_options, &option_index);
91                 if (c == -1)
92                         break;
93
94                 switch (c) {
95                 case 'f':
96                         runflags = 1;
97                         break;
98
99                 case 'd':
100                         flags = flags | WITHOUT_STARTNOTI;      /* 0x1 : start without noti */
101                         break;
102
103                 case 'x':
104                         flags = flags | FLAG_X_DPMS;    /* 0x2 : X control LCD onoff */
105                         break;
106
107                 default:
108                         usage();
109                         break;
110                 }
111         }
112
113         if (optind < argc)
114                 usage();
115
116         if (access(DEFAULT_PID_PATH, F_OK) == 0) {      /* pid file exist */
117                 printf
118                     ("Check the PM is running. If it isn't, delete \"%s\" and retry.\n",
119                      DEFAULT_PID_PATH);
120                 return -1;
121         }
122
123         if (!runflags)
124                 daemonize();
125
126         writepid(DEFAULT_PID_PATH);
127
128         /* this function is main loop, defined in pm_core.c */
129         start_main(flags);
130
131         unlink(DEFAULT_PID_PATH);
132         return 0;
133 }