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.
21 #include <systemd/sd-daemon.h>
30 #include <sys/types.h>
32 #include <sys/sysinfo.h>
35 #include <glib-object.h>
46 #ifndef DAEMON_VERSION
47 #define DAEMON_VERSION "unknown"
50 static Server *_server;
52 void tcore_log(enum tcore_log_type type, enum tcore_log_priority priority, const char *tag, const char *fmt, ...)
58 vsnprintf(buf, 1023, fmt, ap);
61 __dlog_print(type, priority, tag, buf);
64 static gboolean load_plugins(Server *s, const char *path, int flag_test_load)
75 struct tcore_plugin_define_desc *desc;
77 if ((path == NULL) || (s == NULL))
80 dir = g_dir_open(path, 0, NULL);
84 while ((file = g_dir_read_name(dir)) != NULL) {
85 if (g_str_has_prefix(file, "lib") == TRUE
86 || g_str_has_suffix(file, ".so") == FALSE)
89 filename = g_build_filename(path, file, NULL);
91 handle = dlopen(filename, RTLD_NOW);
93 dbg("fail to load '%s': %s", filename, dlerror());
99 dbg("success to load '%s'", filename);
105 desc = dlsym(handle, "plugin_define_desc");
107 dbg("fail to load symbol: %s", dlerror());
113 dbg("%s plugin", desc->name);
114 dbg(" - path = %s", filename);
115 dbg(" - version = %d", desc->version);
116 dbg(" - priority = %d", desc->priority);
118 memset(&stat_buf, 0, sizeof(struct stat));
119 if (stat(filename, &stat_buf) == 0) {
120 if (ctime_r(&stat_buf.st_mtime, file_date) != NULL) {
121 if (strlen(file_date) > 1)
122 file_date[strlen(file_date)-1] = '\0';
124 dbg(" - date = %s", file_date);
129 if (desc->load() == FALSE) {
130 dbg("false return from load(). skip this plugin");
137 p = tcore_plugin_new(s, desc, filename, handle);
138 tcore_server_add_plugin(s, p);
140 dbg("%s added", desc->name);
145 info("plugin load finished");
147 list = tcore_server_ref_plugins(s);
148 for (; list; list = list->next) {
153 desc = (struct tcore_plugin_define_desc *)tcore_plugin_get_description(p);
157 if (desc->init == NULL)
160 if (desc->init(p) == FALSE) {
161 dbg("plugin(%s) init failed.", tcore_plugin_get_filename(p));
165 info("plugin init finished");
170 static void usage(const char *name)
172 printf("Usage: %s [OPTION]... [PLUGIN_PATH]\n", name);
174 printf(" -T, --testload\t run with plugin load test mode and exit\n");
175 printf(" -h, --help\t\t display this help and exit\n");
179 static void on_signal_usr1(int signo)
184 monitor_server_state(_server);
187 int main(int argc, char *argv[])
189 struct sigaction sigact_usr1;
191 int flag_test_load = 0;
194 struct option options[] = {
196 { "testload", 0, &flag_test_load, 1 },
199 char *plugin_path = "/usr/lib/telephony/plugins/";
203 if (sysinfo(&info) == 0) {
204 info("uptime: %ld secs", info.uptime);
207 info("daemon version: %s", DAEMON_VERSION);
209 tcore_ver = tcore_util_get_version();
210 info("libtcore version: %s", tcore_ver);
213 sigact_usr1.sa_handler = on_signal_usr1;
214 sigemptyset(&sigact_usr1.sa_mask);
215 sigaddset(&sigact_usr1.sa_mask, SIGUSR1);
216 sigact_usr1.sa_flags = 0;
218 if (sigaction(SIGUSR1, &sigact_usr1, NULL) < 0) {
219 warn("sigaction(SIGUSR1) failed.");
223 opt = getopt_long(argc, argv, "hT", options, &opt_index);
250 plugin_path = argv[optind];
252 info("plugin_path: [%s]", plugin_path);
254 #if !GLIB_CHECK_VERSION(2,35,0)
257 #if !GLIB_CHECK_VERSION (2, 31, 0)
261 s = tcore_server_new();
263 err("server_new failed.");
268 if (load_plugins(s, plugin_path, flag_test_load) == FALSE)
274 info("server mainloop start");
276 /* Notification to systemd */
277 sd_notify(0, "READY=1");
279 if (tcore_server_run(s) == FALSE) {
280 err("server_run failed.");
289 tcore_server_free(s);