9ca765617a68f797758c5465169c83cc9aa16d81
[profile/ivi/obexd.git] / client / main.c
1 /*
2  *
3  *  OBEX Client
4  *
5  *  Copyright (C) 2007-2010  Marcel Holtmann <marcel@holtmann.org>
6  *
7  *
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.
12  *
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.
17  *
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
21  *
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <signal.h>
32 #include <syslog.h>
33
34 #include <glib.h>
35 #include <gdbus.h>
36
37 #ifdef TIZEN_PATCH
38 #include <sys/types.h>
39 #include <grp.h>
40 #include <pwd.h>
41 #include <unistd.h>
42 #endif
43 #include "log.h"
44 #include "manager.h"
45
46 static GMainLoop *event_loop = NULL;
47
48 static char *option_debug = NULL;
49 static gboolean option_stderr = FALSE;
50
51 static gboolean parse_debug(const char *key, const char *value,
52                                 gpointer user_data, GError **error)
53 {
54         if (value)
55                 option_debug = g_strdup(value);
56         else
57                 option_debug = g_strdup("*");
58
59         return TRUE;
60 }
61
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" },
68         { NULL },
69 };
70
71 static void sig_term(int sig)
72 {
73         g_main_loop_quit(event_loop);
74 }
75
76 int main(int argc, char *argv[])
77 {
78         GOptionContext *context;
79         struct sigaction sa;
80         GError *gerr = NULL;
81
82         context = g_option_context_new(NULL);
83         g_option_context_add_main_entries(context, options, NULL);
84
85         g_option_context_parse(context, &argc, &argv, &gerr);
86         if (gerr != NULL) {
87                 g_printerr("%s\n", gerr->message);
88                 g_error_free(gerr);
89                 exit(EXIT_FAILURE);
90         }
91
92         g_option_context_free(context);
93
94 #ifdef TIZEN_PATCH
95         if (getuid() == 0) {
96                 /* current user is 'root' */
97                 #define INHOUSE_USER "app"
98                 #define INHOUSE_GROUP "app"
99                 #define BUFFER_SIZE 200
100
101                 char buffer[BUFFER_SIZE] = {0};
102
103                 struct group gr;
104                 struct group *gr_result;
105                 struct passwd pw;
106                 struct passwd *pw_result;
107
108                 if (getpwnam_r(INHOUSE_USER, &pw, buffer, BUFFER_SIZE, &pw_result) != 0) {
109                     DBG("Failed to find user '%s'.", INHOUSE_USER);
110                 }
111
112                 memset(buffer, 0x00, BUFFER_SIZE);
113
114                 if (getgrnam_r(INHOUSE_GROUP, &gr, buffer, BUFFER_SIZE, &gr_result) != 0) {
115                     DBG("Failed to find group '%s'.", INHOUSE_GROUP);
116                 }
117
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);
122
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);
125
126                         if (initgroups(INHOUSE_USER, gr.gr_gid) != 0)
127                             DBG("Failed to change group list");
128
129                         if(setgid(gr.gr_gid) != 0)
130                                 DBG("[ERR] fail to execute setgid().\n");
131
132                         if(setuid(pw.pw_uid) != 0)
133                                 DBG("[ERR] fail to execute setuid().\n");
134                 }
135         }
136 #endif
137         event_loop = g_main_loop_new(NULL, FALSE);
138
139         __obex_log_init("obex-client", option_debug, !option_stderr);
140
141         if (manager_init() < 0)
142                 exit(EXIT_FAILURE);
143
144         DBG("Entering main loop");
145
146         memset(&sa, 0, sizeof(sa));
147         sa.sa_handler = sig_term;
148         sigaction(SIGINT, &sa, NULL);
149         sigaction(SIGTERM, &sa, NULL);
150
151         g_main_loop_run(event_loop);
152
153         manager_exit();
154
155         g_main_loop_unref(event_loop);
156
157         __obex_log_cleanup();
158
159         return 0;
160 }