5 * Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
38 #include <sys/types.h>
46 static GMainLoop *event_loop = NULL;
48 static char *option_debug = NULL;
49 static gboolean option_stderr = FALSE;
51 static gboolean parse_debug(const char *key, const char *value,
52 gpointer user_data, GError **error)
55 option_debug = g_strdup(value);
57 option_debug = g_strdup("*");
62 static GOptionEntry options[] = {
63 { "debug", 'd', G_OPTION_FLAG_OPTIONAL_ARG,
64 G_OPTION_ARG_CALLBACK, parse_debug,
65 "Enable debug information output", "DEBUG" },
66 { "stderr", 's', 0, G_OPTION_ARG_NONE, &option_stderr,
67 "Write log information to stderr" },
71 static void sig_term(int sig)
73 g_main_loop_quit(event_loop);
76 int main(int argc, char *argv[])
78 GOptionContext *context;
82 context = g_option_context_new(NULL);
83 g_option_context_add_main_entries(context, options, NULL);
85 g_option_context_parse(context, &argc, &argv, &gerr);
87 g_printerr("%s\n", gerr->message);
92 g_option_context_free(context);
96 /* current user is 'root' */
97 #define INHOUSE_USER "app"
98 #define INHOUSE_GROUP "app"
99 #define BUFFER_SIZE 200
101 char buffer[BUFFER_SIZE] = {0};
104 struct group *gr_result;
106 struct passwd *pw_result;
108 if (getpwnam_r(INHOUSE_USER, &pw, buffer, BUFFER_SIZE, &pw_result) != 0) {
109 DBG("Failed to find user '%s'.", INHOUSE_USER);
112 memset(buffer, 0x00, BUFFER_SIZE);
114 if (getgrnam_r(INHOUSE_GROUP, &gr, buffer, BUFFER_SIZE, &gr_result) != 0) {
115 DBG("Failed to find group '%s'.", INHOUSE_GROUP);
118 if (pw_result && gr_result) {
119 DBG("Found user '%s' (UID %lu) and group '%s' (GID %lu).",
120 INHOUSE_USER, (unsigned long) pw.pw_uid,
121 INHOUSE_GROUP, (unsigned long) gr.gr_gid);
123 if (pw.pw_gid != gr.gr_gid)
124 DBG("GID of user '%s' and of group '%s' don't match.", INHOUSE_USER, INHOUSE_GROUP);
126 if (initgroups(INHOUSE_USER, gr.gr_gid) != 0)
127 DBG("Failed to change group list");
129 if(setgid(gr.gr_gid) != 0)
130 DBG("[ERR] fail to execute setgid().\n");
132 if(setuid(pw.pw_uid) != 0)
133 DBG("[ERR] fail to execute setuid().\n");
137 event_loop = g_main_loop_new(NULL, FALSE);
139 __obex_log_init("obex-client", option_debug, !option_stderr);
141 if (manager_init() < 0)
144 DBG("Entering main loop");
146 memset(&sa, 0, sizeof(sa));
147 sa.sa_handler = sig_term;
148 sigaction(SIGINT, &sa, NULL);
149 sigaction(SIGTERM, &sa, NULL);
151 g_main_loop_run(event_loop);
155 g_main_loop_unref(event_loop);
157 __obex_log_cleanup();