1 /**************************************************************************
5 Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
7 Contact: Boram Park <boram1288.park@samsung.com>
8 Sangjin LEE <lsj119@samsung.com>
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:
18 The above copyright notice and this permission notice (including the
19 next paragraph) shall be included in all copies or substantial portions
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.
30 **************************************************************************/
38 #include <windowstr.h>
42 #include "xdbg_dbus_server.h"
43 #include "xdbg_module.h"
44 #include "xdbg_module_command.h"
45 #include "xdbg_module_evlog.h"
46 #include "xdbg_module_clist.h"
49 #include <sys/socket.h>
51 DevPrivateKeyRec debug_client_key;
53 static XDbgDbusServerMethod method;
56 _debugClientInfo (ClientPtr client)
58 int fd = XaceGetConnectionNumber (client);
59 ModuleClientInfo *info = GetClientInfo (client);
61 /* For local clients, try and determine the executable name */
62 if (XaceIsLocal (client))
65 socklen_t len = sizeof (creds);
66 char path[PATH_MAX + 1] = {0,};
69 memset (&creds, 0, sizeof (creds));
70 if (getsockopt (fd, SOL_SOCKET, SO_PEERCRED, &creds, &len) < 0)
73 snprintf (path, PATH_MAX + 1, "/proc/%d/cmdline", creds.pid);
74 fd = open (path, O_RDONLY);
78 bytes = read (fd, path, PATH_MAX + 1);
83 strncpy (info->command, path, PATH_MAX);
84 info->command[PATH_MAX] = '\0';
86 info->pid = creds.pid;
87 info->uid = creds.uid;
88 info->gid = creds.gid;
90 info->index = client->index;
95 info->index = client->index;
96 strncpy (info->command, "REMOTE", PATH_MAX);
97 info->command[PATH_MAX] = '\0';
103 XDBG_ERROR (MXDBG, "Failed to make client info(index:%d)\n",
109 _traceClientState (CallbackListPtr *list, pointer closure, pointer calldata)
111 NewClientInfoRec *clientinfo = (NewClientInfoRec*)calldata;
112 ClientPtr client = clientinfo->client;
113 ModuleClientInfo *info = GetClientInfo (client);
114 XDbgModule *pMod = (XDbgModule *)closure;
115 static char* clientState[]=
117 "ClientStateInitial",
118 "ClientStateAuthenticating",
119 "ClientStateRunning",
120 "ClientStateRetained",
122 "ClientStateCheckingDebug",
123 "ClientStateCheckedDebug"
129 if ((client->clientState == ClientStateInitial) || (client->clientState == ClientStateGone))
131 if (client->clientState == ClientStateInitial)
133 _debugClientInfo (client);
136 xDbgModuleClistTraceAdd (pMod, info);
139 if (client->clientState == ClientStateGone)
142 xDbgModuleClistTraceRemove (pMod, info);
145 XDBG_SECURE (MXDBG, "id:%d, conn_fd:%d, pid:%d, uid:%d, name:%s (%s)\n",
146 info->index, info->conn_fd, info->pid, info->uid, info->command,
147 clientState[client->clientState]);
151 XDBG_DEBUG (MXDBG, "id:%d, conn_fd:%d, pid:%d, uid:%d, name:%s (%s)\n",
152 info->index, info->conn_fd, info->pid, info->uid, info->command,
153 clientState[client->clientState]);
157 xDbgModuleMain (XDbgModule *pMod)
161 if (!dixRegisterPrivateKey (DebugClientKey, PRIVATE_CLIENT, sizeof (ModuleClientInfo)))
163 XDBG_ERROR (MXDBG, "failed: dixRegisterPrivateKey\n");
168 xDbgModuleClistTraceInit (pMod);
170 ret &= AddCallback (&ClientStateCallback, _traceClientState, pMod);
174 XDBG_ERROR (MXDBG, "failed: AddCallback\n");
178 xDbgModuleEvlogInstallHooks (pMod);
180 if (!xDbgDBusServerConnect ())
182 XDBG_ERROR (MXDBG, "failed: xDbgDBusServerConnect\n");
186 snprintf (method.name, sizeof (method.name), "%s", XDBG_DBUS_METHOD);
187 method.func = xDbgModuleCommand;
190 xDbgDBusServerAddMethod (&method);
196 xDbgModuleMainExit (XDbgModule *pMod)
198 DeleteCallback (&ClientStateCallback, _traceClientState, pMod);
200 xDbgModuleEvlogUninstallHooks (pMod);
202 xDbgDBusServerRemoveMethod (&method);
204 free (pMod->trace_options);
206 xDbgDBusServerDisconnect ();