4 * Copyright (c) 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Ja-young Gu <jygu@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
29 #include <sys/types.h>
31 #include <sys/sysinfo.h>
34 #include <glib-object.h>
45 #ifndef DAEMON_VERSION
46 #define DAEMON_VERSION "unknown"
49 static Server *_server;
51 void tcore_log(enum tcore_log_type type, enum tcore_log_priority priority, const char *tag, const char *fmt, ...)
57 vsnprintf(buf, 1023, fmt, ap);
60 __dlog_print(type, priority, tag, buf);
63 static gboolean load_plugins(Server *s, const char *path, int flag_test_load)
74 struct tcore_plugin_define_desc *desc;
79 dir = g_dir_open(path, 0, NULL);
83 while ((file = g_dir_read_name(dir)) != NULL) {
84 if (g_str_has_prefix(file, "lib") == TRUE
85 || g_str_has_suffix(file, ".so") == FALSE)
88 filename = g_build_filename(path, file, NULL);
90 handle = dlopen(filename, RTLD_NOW);
92 dbg("fail to load '%s': %s", filename, dlerror());
98 dbg("success to load '%s'", filename);
104 desc = dlsym(handle, "plugin_define_desc");
106 dbg("fail to load symbol: %s", dlerror());
112 dbg("%s plugin", desc->name);
113 dbg(" - path = %s", filename);
114 dbg(" - version = %d", desc->version);
115 dbg(" - priority = %d", desc->priority);
117 memset(&stat_buf, 0, sizeof(struct stat));
118 if (stat(filename, &stat_buf) == 0) {
119 if (ctime_r(&stat_buf.st_mtime, file_date) != NULL) {
120 if (strlen(file_date) > 1)
121 file_date[strlen(file_date)-1] = '\0';
123 dbg(" - date = %s", file_date);
128 if (desc->load() == FALSE) {
129 dbg("false return from load(). skip this plugin");
136 p = tcore_plugin_new(s, desc, filename, handle);
137 tcore_server_add_plugin(s, p);
139 dbg("%s added", desc->name);
144 info("plugin load finished");
146 list = tcore_server_ref_plugins(s);
147 for (; list; list = list->next) {
152 desc = (struct tcore_plugin_define_desc *)tcore_plugin_get_description(p);
159 if (!desc->init(p)) {
160 dbg("plugin(%s) init failed.", tcore_plugin_get_filename(p));
164 info("plugin init finished");
169 static void usage(const char *name)
171 printf("Usage: %s [OPTION]... [PLUGIN_PATH]\n", name);
173 printf(" -T, --testload\t run with plugin load test mode and exit\n");
174 printf(" -h, --help\t\t display this help and exit\n");
178 static void on_signal_usr1(int signo)
183 monitor_server_state(_server);
186 int main(int argc, char *argv[])
188 struct sigaction sigact_usr1;
190 int flag_test_load = 0;
193 struct option options[] = {
195 { "testload", 0, &flag_test_load, 1 },
198 char *plugin_path = "/usr/lib/telephony/plugins/";
202 if (sysinfo(&info) == 0) {
203 info("uptime: %ld secs", info.uptime);
206 info("daemon version: %s", DAEMON_VERSION);
208 tcore_ver = tcore_util_get_version();
209 info("libtcore version: %s", tcore_ver);
212 sigact_usr1.sa_handler = on_signal_usr1;
213 sigemptyset(&sigact_usr1.sa_mask);
214 sigaddset(&sigact_usr1.sa_mask, SIGUSR1);
215 sigact_usr1.sa_flags = 0;
217 if (sigaction(SIGUSR1, &sigact_usr1, NULL) < 0) {
218 warn("sigaction(SIGUSR1) failed.");
222 opt = getopt_long(argc, argv, "hT", options, &opt_index);
249 plugin_path = argv[optind];
251 info("plugin_path: [%s]", plugin_path);
254 #if !GLIB_CHECK_VERSION (2, 31, 0)
258 s = tcore_server_new();
260 err("server_new failed.");
265 if (!load_plugins(s, plugin_path, flag_test_load))
271 info("server mainloop start");
273 if (tcore_server_run(s) == FALSE) {
274 err("server_run failed.");
283 tcore_server_free(s);