Initialize Tizen 2.3
[adaptation/xorg/driver/xserver-xorg-module-xdbg.git] / module / xdbg_module_main.c
1 /**************************************************************************
2
3 xdbg
4
5 Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
6
7 Contact: Boram Park <boram1288.park@samsung.com>
8          Sangjin LEE <lsj119@samsung.com>
9
10 Permission is hereby granted, free of charge, to any person obtaining a
11 copy of this software and associated documentation files (the
12 "Software"), to deal in the Software without restriction, including
13 without limitation the rights to use, copy, modify, merge, publish,
14 distribute, sub license, and/or sell copies of the Software, and to
15 permit persons to whom the Software is furnished to do so, subject to
16 the following conditions:
17
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
20 of the Software.
21
22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
25 IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
27 TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
28 SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29
30 **************************************************************************/
31
32 #ifdef HAVE_CONFIG_H
33 #include "config.h"
34 #endif
35
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <windowstr.h>
39 #include <xacestr.h>
40 #include <xdbg.h>
41
42 #include "xdbg_dbus_server.h"
43 #include "xdbg_module.h"
44 #include "xdbg_module_command.h"
45 #include "xdbg_module_evlog.h"
46
47 #define __USE_GNU
48 #include <sys/socket.h>
49
50 DevPrivateKeyRec debug_client_key;
51
52 static XDbgDbusServerMethod method;
53
54 static void
55 _debugClientInfo (ClientPtr client)
56 {
57     int fd = XaceGetConnectionNumber (client);
58     ModuleClientInfo *info = GetClientInfo (client);
59
60     /* For local clients, try and determine the executable name */
61     if (XaceIsLocal (client))
62     {
63         struct ucred creds;
64         socklen_t len = sizeof (creds);
65         char path[PATH_MAX + 1] = {0,};
66         int bytes;
67
68         memset (&creds, 0, sizeof (creds));
69         if (getsockopt (fd, SOL_SOCKET, SO_PEERCRED, &creds, &len) < 0)
70             goto finish;
71
72         snprintf (path, PATH_MAX + 1, "/proc/%d/cmdline", creds.pid);
73         fd = open (path, O_RDONLY);
74         if (fd < 0)
75             goto finish;
76
77         bytes = read (fd, path, PATH_MAX + 1);
78         close (fd);
79         if (bytes <= 0)
80             goto finish;
81
82         strncpy (info->command, path, PATH_MAX);
83         info->command[PATH_MAX] = '\0';
84
85         info->pid = creds.pid;
86         info->uid = creds.uid;
87         info->gid = creds.gid;
88         info->conn_fd = fd;
89         info->index = client->index;
90     }
91     else
92     {
93         info->pid = -1;
94         info->index = client->index;
95         strncpy (info->command, "REMOTE", PATH_MAX);
96         info->command[PATH_MAX] = '\0';
97     }
98
99     return;
100
101 finish:
102     XDBG_ERROR (MXDBG, "Failed to make client info(index:%d)\n",
103                  client->index);
104     return;
105 }
106
107 static void
108 _traceClientState (CallbackListPtr *list, pointer closure, pointer calldata)
109 {
110     NewClientInfoRec *clientinfo = (NewClientInfoRec*)calldata;
111     ClientPtr client = clientinfo->client;
112     ModuleClientInfo *info = GetClientInfo (client);
113     static char* clientState[]=
114     {
115         "ClientStateInitial",
116         "ClientStateAuthenticating",
117         "ClientStateRunning",
118         "ClientStateRetained",
119         "ClientStateGone",
120         "ClientStateCheckingDebug",
121         "ClientStateCheckedDebug"
122     };
123
124     if (!info)
125         return;
126
127     if ((client->clientState == ClientStateInitial) || (client->clientState == ClientStateGone))
128     {
129         if (client->clientState == ClientStateInitial)
130               _debugClientInfo (client);
131
132         XDBG_SECURE (MXDBG, "id:%d, conn_fd:%d, pid:%d, uid:%d, name:%s (%s)\n",
133               info->index, info->conn_fd, info->pid, info->uid, info->command,
134               clientState[client->clientState]);
135         return;
136     }
137
138     XDBG_DEBUG (MXDBG, "id:%d, conn_fd:%d, pid:%d, uid:%d, name:%s (%s)\n",
139                  info->index, info->conn_fd, info->pid, info->uid, info->command,
140                  clientState[client->clientState]);
141 }
142
143 Bool
144 xDbgModuleMain (XDbgModule *pMod)
145 {
146     Bool ret = TRUE;
147
148     if (!dixRegisterPrivateKey (DebugClientKey, PRIVATE_CLIENT, sizeof (ModuleClientInfo)))
149     {
150         XDBG_ERROR (MXDBG, "failed: dixRegisterPrivateKey\n");
151         return FALSE;
152     }
153
154     ret &= AddCallback (&ClientStateCallback, _traceClientState, pMod);
155
156     if (!ret)
157     {
158         XDBG_ERROR (MXDBG, "failed: AddCallback\n");
159         return FALSE;
160     }
161
162     xDbgModuleEvlogInstallHooks (pMod);
163
164     if (!xDbgDBusServerConnect ())
165     {
166         XDBG_ERROR (MXDBG, "failed: xDbgDBusServerConnect\n");
167         return FALSE;
168     }
169
170     snprintf (method.name, sizeof (method.name), "%s", XDBG_DBUS_METHOD);
171     method.func = xDbgModuleCommand;
172     method.data = pMod;
173
174     xDbgDBusServerAddMethod (&method);
175
176     return TRUE;
177 }
178
179 void
180 xDbgModuleMainExit (XDbgModule *pMod)
181 {
182     DeleteCallback (&ClientStateCallback, _traceClientState, pMod);
183
184     xDbgModuleEvlogUninstallHooks (pMod);
185
186     xDbgDBusServerRemoveMethod (&method);
187
188     xDbgDBusServerDisconnect ();
189 }