4 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 * @desc Resourced initialization
28 #include "edbus-handler.h"
32 #include "module-data.h"
33 #include "proc-main.h"
34 #include "proc-monitor.h"
35 #include "swap-common.h"
43 static struct daemon_opts opts = { 1,
46 COUNTER_UPDATE_PERIOD,
48 RESOURCED_DEFAULT_STATE,
51 #define SWAP_MAX_ARG_SIZE 16
53 static char swap_arg[SWAP_ARG_END][SWAP_MAX_ARG_SIZE] = { "swapoff",
56 static void print_root_usage()
58 puts("You must be root to start it.");
61 static void print_usage()
63 puts("resourced [Options]");
64 puts(" Application options:");
66 ("-u [--update-period] - time interval for updating,"
67 " %d by default\n", opts.update_period);
69 ("-f [--flush-period] - time interval for storing data at database,"
70 "%d by default\n", opts.flush_period);
71 printf("-s [--start-daemon] - start as daemon, %d by default\n",
73 puts("-v [--version] - program version");
74 puts("-h [--help] - application help");
75 printf("-c string [--swapcontrol=string] - control swap policy and "
76 "select sting %s, %s by default\n",
77 swap_arg[SWAP_OFF], swap_arg[SWAP_ON]);
80 static void print_version()
82 printf("Version number: %d.%d.%d\n",
83 MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION);
86 static int parse_cmd(int argc, char **argv)
88 const char *optstring = ":hvu:s:f:cw";
89 const struct option options[] = {
90 {"help", no_argument, 0, 'h'},
91 {"version", no_argument, 0, 'v'},
92 {"update-period", required_argument, 0, 'u'},
93 {"flush-period", required_argument, 0, 'f'},
94 {"start-daemon", required_argument, 0, 's'},
95 {"swapcontrol", required_argument, 0, 'c'},
96 {"enable-watchodg", no_argument, 0, 'w'},
99 int longindex, retval, i;
102 getopt_long(argc, argv, optstring, options, &longindex)) != -1)
107 return RESOURCED_ERROR_FAIL;
110 return RESOURCED_ERROR_FAIL;
112 opts.update_period = atoi(optarg);
115 opts.flush_period = atoi(optarg);
118 opts.start_daemon = atoi(optarg);
121 for (i = 0; i < SWAP_ARG_END; i++)
122 if (optarg && !strncmp(optarg, swap_arg[i],
123 SWAP_MAX_ARG_SIZE)) {
124 opts.enable_swap = i;
125 _D("argment swaptype = %s",
133 proc_set_watchdog_state(PROC_WATCHDOG_ENABLE);
136 printf("Unknown option %c\n", (char)retval);
138 return RESOURCED_ERROR_FAIL;
140 return RESOURCED_ERROR_NONE;
143 static int assert_root(void)
147 return RESOURCED_ERROR_FAIL;
149 return RESOURCED_ERROR_NONE;
152 static void sig_term_handler(int sig)
154 struct shared_modules_data *shared_data = get_shared_modules_data();
156 opts.state |= RESOURCED_FORCIBLY_QUIT_STATE;
157 _SD("sigterm or sigint received");
158 if (shared_data && shared_data->carg && shared_data->carg->ecore_timer) {
159 /* save data on exit, it's impossible to do in fini
160 * module function, due it executes right after ecore stopped */
161 reschedule_count_timer(shared_data->carg, 0);
163 /* Another way it's introduce another timer and quit main loop
164 * in it with waiting some event. */
165 sleep(TIME_TO_SAFE_DATA);
168 ecore_main_loop_quit();
171 static void add_signal_handler(void)
173 signal(SIGTERM, sig_term_handler);
174 signal(SIGINT, sig_term_handler);
177 int resourced_init(struct daemon_arg *darg)
181 ret_value_msg_if(darg == NULL, RESOURCED_ERROR_INVALID_PARAMETER,
182 "Invalid daemon argument\n");
183 ret_code = assert_root();
184 ret_value_if(ret_code < 0, RESOURCED_ERROR_FAIL);
187 ret_code = parse_cmd(darg->argc, darg->argv);
188 ret_value_msg_if(ret_code < 0, RESOURCED_ERROR_FAIL,
189 "Error parse cmd arguments\n");
190 _D("argment swaptype = %s", swap_arg[opts.enable_swap]);
191 add_signal_handler();
193 return RESOURCED_ERROR_NONE;
196 int resourced_deinit(struct daemon_arg *darg)
200 ret_value_msg_if(darg == NULL, RESOURCED_ERROR_INVALID_PARAMETER,
201 "Invalid daemon argument\n");
202 return RESOURCED_ERROR_NONE;
205 void set_daemon_net_block_state(const enum traffic_restriction_type rst_type,
206 const struct counter_arg *carg)
208 ret_msg_if(carg == NULL,
209 "Please provide valid counter arg!");
211 if (rst_type == RST_SET)
212 opts.state |= RESOURCED_NET_BLOCKED_STATE; /* set bit */
214 opts.state &=(~RESOURCED_NET_BLOCKED_STATE); /* nulify bit */
215 ecore_timer_thaw(carg->ecore_timer);