An interface is provided for figuring out the PID and process name of a client. Make some existing functionality from SELinux and IA extensions available for general use.
Signed-off-by: Rami Ylimäki <rami.ylim...@vincit.fi> --- configure.ac | 15 ++- dix/Makefile.am | 1 + dix/client.c | 345 ++++++++++++++++++++++++++++++++++++++++++ dix/main.c | 3 + hw/xfree86/loader/sdksyms.sh | 1 + include/Makefile.am | 1 + include/client.h | 60 ++++++++ include/dix-config.h.in | 3 + include/os.h | 3 + os/access.c | 76 +++++++++ 10 files changed, 507 insertions(+), 1 deletions(-) create mode 100644 dix/client.c create mode 100644 include/client.h diff --git a/configure.ac b/configure.ac index 1a1f2d3..056d68f 100644 --- a/configure.ac +++ b/configure.ac @@ -645,6 +645,7 @@ AC_ARG_ENABLE(vbe, AS_HELP_STRING([--enable-vbe], [Build Xorg with VB AC_ARG_ENABLE(int10-module, AS_HELP_STRING([--enable-int10-module], [Build Xorg with int10 module (default: enabled)]), [INT10MODULE=$enableval], [INT10MODULE=yes]) AC_ARG_ENABLE(windowswm, AS_HELP_STRING([--enable-windowswm], [Build XWin with WindowsWM extension (default: no)]), [WINDOWSWM=$enableval], [WINDOWSWM=no]) AC_ARG_ENABLE(libdrm, AS_HELP_STRING([--enable-libdrm], [Build Xorg with libdrm support (default: enabled)]), [DRM=$enableval],[DRM=yes]) +AC_ARG_ENABLE(clientids, AS_HELP_STRING([--disable-clientids], [Build Xorg with client ID tracking (default: enabled)]), [CLIENTIDS=$enableval], [CLIENTIDS=yes]) dnl DDXes. AC_ARG_ENABLE(xorg, AS_HELP_STRING([--enable-xorg], [Build Xorg server (default: auto)]), [XORG=$enableval], [XORG=auto]) @@ -999,6 +1000,18 @@ if test "x$RES" = xyes; then REQUIRED_MODULES="$REQUIRED_MODULES $RESOURCEPROTO" fi +# The XRes extension may support client ID tracking only if it has +# been specifically enabled. Client ID tracking is implicitly not +# supported if XRes extension is disabled. +AC_MSG_CHECKING([whether to track client ids]) +if test "x$RES" = xyes && test "x$CLIENTIDS" = xyes; then + AC_DEFINE(CLIENTIDS, 1, [Support client ID tracking]) +else + CLIENTIDS=no +fi +AC_MSG_RESULT([$CLIENTIDS]) +AM_CONDITIONAL(CLIENTIDS, [test "x$CLIENTIDS" = xyes]) + if test "x$GLX" = xyes; then PKG_CHECK_MODULES([XLIB], [x11]) PKG_CHECK_MODULES([GL], $GLPROTO $LIBGL) @@ -1527,7 +1540,7 @@ if test "x$XNEST" = xyes; then if test "x$have_xnest" = xno; then AC_MSG_ERROR([Xnest build explicitly requested, but required modules not found.]) fi - XNEST_LIBS="$FB_LIB $FIXES_LIB $MI_LIB $XEXT_LIB $DBE_LIB $RECORD_LIB $GLX_LIBS $RANDR_LIB $RENDER_LIB $DAMAGE_LIB $MIEXT_DAMAGE_LIB $MIEXT_SHADOW_LIB $XI_LIB $XKB_LIB $XKB_STUB_LIB $COMPOSITE_LIB $DIX_LIB $MAIN_LIB $OS_LIB" + XNEST_LIBS="$FB_LIB $FIXES_LIB $MI_LIB $XEXT_LIB $DBE_LIB $RECORD_LIB $GLX_LIBS $RANDR_LIB $RENDER_LIB $DAMAGE_LIB $MIEXT_DAMAGE_LIB $MIEXT_SHADOW_LIB $XI_LIB $XKB_LIB $XKB_STUB_LIB $COMPOSITE_LIB $MAIN_LIB $DIX_LIB $OS_LIB" XNEST_SYS_LIBS="$XNESTMODULES_LIBS $GLX_SYS_LIBS" AC_SUBST([XNEST_LIBS]) AC_SUBST([XNEST_SYS_LIBS]) diff --git a/dix/Makefile.am b/dix/Makefile.am index 5e2dad7..49e41d0 100644 --- a/dix/Makefile.am +++ b/dix/Makefile.am @@ -8,6 +8,7 @@ libmain_la_SOURCES = \ libdix_la_SOURCES = \ atom.c \ colormap.c \ + client.c \ cursor.c \ deprecated.c \ devices.c \ diff --git a/dix/client.c b/dix/client.c new file mode 100644 index 0000000..1016a56 --- /dev/null +++ b/dix/client.c @@ -0,0 +1,345 @@ +/* + * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). All + * rights reserved. + * Copyright (c) 1993, 2010, Oracle and/or its affiliates. All rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/** + * @file + * + * This file contains functionality for identifying clients by various + * means. The primary purpose of identification is to simply aid in + * finding out which clients are using X server and how they are using + * it. For example, it's often necessary to monitor what requests + * clients are executing (to spot bad behaviour) and how they are + * allocating resources in X server (to spot excessive resource + * usage). + * + * This framework automatically allocates information, that can be + * used for client identification, when a client connects to the + * server. The information is freed when the client disconnects. The + * allocated information is just a collection of various IDs, such as + * PID and process name for local clients, that are likely to be + * useful in analyzing X server usage. + * + * Users of the framework can query ID information about clients at + * any time. To avoid repeated polling of IDs the users can also + * subscribe for notifications about the availability of ID + * information. Use GetClient* to query information and + * GetClientIds*Cbs to register for notifications. + * + * Author: Rami Ylimäki <rami.ylim...@vincit.fi> + */ + +#include "client.h" + +#ifdef CLIENTIDS + +#include "os.h" +#include "dixstruct.h" + +/* Key for identifying ID information for a client. */ +static DevPrivateKeyRec ClientIdsPrivKeyRec; +static DevPrivateKey ClientIdsPrivKey = &ClientIdsPrivKeyRec; + +/** + * @return Client private holding PID and command line string. Error + * (NULL) if PID is not available for the client. + */ +ClientIdsPrivatePtr GetClientIds(ClientPtr client) +{ + PrivatePtr *privates = &(client)->devPrivates; + pointer priv = dixLookupPrivate(privates, ClientIdsPrivKey); + return (ClientIdsPrivatePtr) priv; +} + +/* Called after PID and command line string have been determined for a + * client (all clients, including remote clients, except server + * client). You may call GetClientPid and GetClientCmd after this + * notification. */ +static CallbackListPtr ClientIdsReservedCbs = NULL; + +/** + * @return Publisher of client ID allocation notifications. + * + * @see AddCallback + */ +CallbackListPtr *GetClientIdsReservedCbs(void) +{ + return &ClientIdsReservedCbs; +} + +/* Called before PID and command line string will be invalidated for a + * client (all clients, including remote clients, except server + * client). GetClientPid and GetClientCmd will return errors when + * called after this notification. */ +static CallbackListPtr ClientIdsReleasedCbs = NULL; + +/** + * @return Publisher of client ID deallocation notifications. + * + * @see AddCallback + */ +CallbackListPtr *GetClientIdsReleasedCbs(void) +{ + return &ClientIdsReleasedCbs; +} + +/** + * Try to determine a PID for a client from its connection + * information. This should be called only once when new client has + * connected, use GetClientPid to determine the PID at other times. + * + * @param[in] client Connection linked to some process. + * + * @return PID of the client. Error (-1) if PID can't be determined + * for the client. + * + * @see GetClientPid + */ +static pid_t DetermineClientPid(ClientPtr client) +{ + /* The implementation is essentially a wrapper. However, we wan't + * to keep the wrapper for documentation purposes since it + * clarifies the intended usage pattern. */ + return GetPidFromClient(client); +} + +/** + * Try to determine a command line string for a client based on its + * PID. Note that mapping PID to a command hasn't been implemented for + * some operating systems. This should be called only once when a new + * client has connected, use GetClientCmd to determine the string at + * other times. + * + * @param[in] pid Process ID of a client. + * + * @return Client command line string. Error (NULL) if command line + * string can't be determined for the client. You must release + * the string by calling free when it's not used anymore. + * + * @see GetClientCmd + */ +static const char *DetermineClientCmd(pid_t pid) +{ + /* The implementation is essentially a wrapper. However, we wan't + * to keep the wrapper for documentation purposes since it + * clarifies the intended usage pattern. */ + return GetCommandFromPid(pid); +} + +/** + * Called when a new client connects. Allocates a private for the + * client and fills it with ID information. + * + * @param[in] client Recently connected client. + */ +static void ReserveClientIds(ClientPtr client) +{ + ClientIdsPrivatePtr priv = NULL; + pid_t pid = -1; + + if (client == NullClient) + return; + + /* Allocate private structure only if PID is available. */ + pid = DetermineClientPid(client); + if (pid != -1) + { + priv = malloc(sizeof(ClientIdsPrivateRec)); + if (priv) + { + priv->pid = pid; + priv->cmdline = DetermineClientCmd(pid); + } + } + + DebugF("client(%lx): Reserved pid(%d).\n", + client->clientAsMask, priv ? priv->pid : -1); + DebugF("client(%lx): Reserved cmdline(%s).\n", + client->clientAsMask, (priv && priv->cmdline) ? priv->cmdline : "NULL"); + + dixSetPrivate(&(client)->devPrivates, ClientIdsPrivKey, priv); +} + +/** + * Called when an existing client disconnects. Frees client ID + * information as well as the client private. + * + * @param[in] client Recently disconnected client. + */ +static void ReleaseClientIds(ClientPtr client) +{ + ClientIdsPrivatePtr priv = NULL; + + if (client == NullClient) + return; + + priv = GetClientIds(client); + if (!priv) + return; + + DebugF("client(%lx): Released pid(%d).\n", + client->clientAsMask, priv ? priv->pid : -1); + DebugF("client(%lx): Released cmdline(%s).\n", + client->clientAsMask, (priv && priv->cmdline) ? priv->cmdline : "NULL"); + + free((void *) priv->cmdline); /* const char * */ + free(priv); + dixSetPrivate(&(client)->devPrivates, ClientIdsPrivKey, NULL); +} + +/** + * Called when new client connects or existing client disconnects. + * + * @param[in] pcbl Publisher of client state change notifications. + * @param[in] nulldata Unused private callback data. + * @param[in] calldata Information about client whose state changed. + * + * @see GetClientIdsReservedCbs + * @see GetClientIdsReleasedCbs + */ +static void ClientIdsChanged(CallbackListPtr *pcbl, pointer nulldata, pointer calldata) +{ + NewClientInfoRec *pci = (NewClientInfoRec *)calldata; + ClientPtr client = pci->client; + + switch (client->clientState) + { + case ClientStateGone: + case ClientStateRetained: + /* Notify subscribers that client IDs will be released before + * they are actually released. */ + CallCallbacks(&ClientIdsReleasedCbs, client); + ReleaseClientIds(client); + break; + case ClientStateInitial: + /* Reserve client IDs and notify subscribers about new IDs + * afterwards. */ + ReserveClientIds(client); + CallCallbacks(&ClientIdsReservedCbs, client); + break; + default: + break; + } +} + +/** + * Starts tracking of client connections and disconnections. After + * initialization, PID and command line strings are determined and + * cached for each connected client. Call this before any clients have + * connected. + * + * @param[in] server Recently initialized server client. + */ +void InitClientIds(ClientPtr server) +{ + if (!dixRegisterPrivateKey(ClientIdsPrivKey, PRIVATE_CLIENT, 0)) + FatalError("Can't register client IDs private.\n"); + + if (!AddCallback(&ClientStateCallback, ClientIdsChanged, NULL)) + FatalError("Can't track client IDs.\n"); + + ReserveClientIds(server); +} + +/** + * Stops tracking clients. Call this after all clients have + * disconnected. + * + * @param[in] server Server client that is about to be cleaned up. + */ +void CloseClientIds(ClientPtr server) +{ + if (!DeleteCallback(&ClientStateCallback, ClientIdsChanged, NULL)) + LogMessage(X_ERROR, "Can't stop tracking client IDs.\n"); + + DeleteCallbackList(&ClientIdsReservedCbs); + DeleteCallbackList(&ClientIdsReleasedCbs); + + ReleaseClientIds(server); +} + +/** + * Get cached PID of a client. + * + * param[in] client Client whose PID has been already cached. + * + * @return Cached client PID. Error (-1) if called: + * - before ClientIdsReservedCbs notification + * - after ClientIdsReleasedCbs notification + * - for remote clients + * + * @see DetermineClientPid + */ +pid_t GetClientPid(ClientPtr client) +{ + ClientIdsPrivatePtr priv = NULL; + + if (client == NullClient) + return -1; + + priv = GetClientIds(client); + if (!priv) + return -1; + + return priv->pid; +} + +/** + * Get cached command line string of a client. + * + * param[in] client Client whose command line string caching has been + * attempted previously. + * + * @return Cached client command line. Error (NULL) if called: + * - before ClientIdsReservedCbs notification + * - after ClientIdsReleasedCbs notification + * - for remote clients + * - on OS that doesn't support mapping of PID to command line + * + * @see DetermineClientCmd + */ +const char *GetClientCmd(ClientPtr client) +{ + ClientIdsPrivatePtr priv = NULL; + + if (client == NullClient) + return NULL; + + priv = GetClientIds(client); + if (!priv) + return NULL; + + return priv->cmdline; +} + +#else /* CLIENTIDS */ + +void InitClientIds(ClientPtr server) +{ +} + +void CloseClientIds(ClientPtr server) +{ +} + +#endif /* CLIENTIDS */ diff --git a/dix/main.c b/dix/main.c index 5c46dc1..b46433b 100644 --- a/dix/main.c +++ b/dix/main.c @@ -104,6 +104,7 @@ Equipment Corporation. #include "extnsionst.h" #include "privates.h" #include "registry.h" +#include "client.h" #ifdef PANORAMIX #include "panoramiXsrv.h" #else @@ -260,6 +261,7 @@ int main(int argc, char *argv[], char *envp[]) InitCoreDevices(); InitInput(argc, argv); InitAndStartDevices(); + InitClientIds(serverClient); dixSaveScreens(serverClient, SCREEN_SAVER_FORCER, ScreenSaverReset); @@ -325,6 +327,7 @@ int main(int argc, char *argv[], char *envp[]) screenInfo.numScreens = i; } + CloseClientIds(serverClient); dixFreePrivates(serverClient->devPrivates, PRIVATE_CLIENT); serverClient->devPrivates = NULL; diff --git a/hw/xfree86/loader/sdksyms.sh b/hw/xfree86/loader/sdksyms.sh index 13c5ae5..6463182 100755 --- a/hw/xfree86/loader/sdksyms.sh +++ b/hw/xfree86/loader/sdksyms.sh @@ -264,6 +264,7 @@ cat > sdksyms.c << EOF #include "colormap.h" #include "colormapst.h" #include "hotplug.h" +#include "client.h" #include "cursor.h" #include "cursorstr.h" #include "dix.h" diff --git a/include/Makefile.am b/include/Makefile.am index e76de05..06cf46f 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -4,6 +4,7 @@ sdk_HEADERS = \ bstore.h \ bstorestr.h \ callback.h \ + client.h \ closestr.h \ closure.h \ colormap.h \ diff --git a/include/client.h b/include/client.h new file mode 100644 index 0000000..2a1387b --- /dev/null +++ b/include/client.h @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). All + * rights reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +/* Author: Rami Ylimäki <rami.ylim...@vincit.fi> */ + +#ifndef CLIENT_H +#define CLIENT_H + +#ifdef HAVE_DIX_CONFIG_H +#include <dix-config.h> +#endif +#include "dix.h" + +/* Initialize and clean up. */ +void InitClientIds(ClientPtr server); +void CloseClientIds(ClientPtr server); + +#ifdef CLIENTIDS /* whether configured with client ID tracking */ + +#include <sys/types.h> + +/* Client specific ID information. The corresponding client private + * will be NULL if PID is not available for the client. */ +typedef struct +{ + pid_t pid; /* always zero or positive */ + const char *cmdline; /* NULL if not available */ +} ClientIdsPrivateRec, *ClientIdsPrivatePtr; + +/* Register to notifications. */ +extern _X_EXPORT CallbackListPtr *GetClientIdsReservedCbs(void); +extern _X_EXPORT CallbackListPtr *GetClientIdsReleasedCbs(void); + +/* Query client IDs. */ +extern _X_EXPORT ClientIdsPrivatePtr GetClientIds(ClientPtr client); +extern _X_EXPORT pid_t GetClientPid(ClientPtr client); +extern _X_EXPORT const char *GetClientCmd(ClientPtr client); + +#endif /* CLIENTIDS */ +#endif /* CLIENT_H */ diff --git a/include/dix-config.h.in b/include/dix-config.h.in index 6a33264..c7c01dc 100644 --- a/include/dix-config.h.in +++ b/include/dix-config.h.in @@ -276,6 +276,9 @@ /* Support X resource extension */ #undef RES +/* Support client ID tracking in X resource extension */ +#undef CLIENTIDS + /* Support MIT-SCREEN-SAVER extension */ #undef SCREENSAVER diff --git a/include/os.h b/include/os.h index efa202c..81f6f98 100644 --- a/include/os.h +++ b/include/os.h @@ -51,6 +51,7 @@ SOFTWARE. #include "misc.h" #include <stdarg.h> +#include <sys/types.h> /* pid_t */ #define SCREEN_SAVER_ON 0 #define SCREEN_SAVER_OFF 1 @@ -369,6 +370,8 @@ typedef struct { extern _X_EXPORT int GetLocalClientCreds(ClientPtr, LocalClientCredRec **); extern _X_EXPORT void FreeLocalClientCreds(LocalClientCredRec *); +extern _X_EXPORT pid_t GetPidFromClient(ClientPtr client); +extern _X_EXPORT const char *GetCommandFromPid(pid_t pid); extern _X_EXPORT int ChangeAccessControl(ClientPtr /*client*/, int /*fEnabled*/); diff --git a/os/access.c b/os/access.c index 0279259..50cd127 100644 --- a/os/access.c +++ b/os/access.c @@ -1297,6 +1297,82 @@ FreeLocalClientCreds(LocalClientCredRec *lcc) } } +/** + * Find out the PID of a client. + * + * @param[in] client Any null, server, local or remote client. + * + * @return PID of given client. Error (-1) if the PID is not + * available, which is the case for remote clients for + * example. + */ +pid_t GetPidFromClient(ClientPtr client) +{ + LocalClientCredRec *lcc = NULL; + pid_t pid = -1; + + if (client == NullClient) + return pid; + + if (client == serverClient) + return getpid(); + + if (GetLocalClientCreds(client, &lcc) != -1) + { + if (lcc->fieldsSet & LCC_PID_SET) + pid = lcc->pid; + FreeLocalClientCreds(lcc); + } + + return pid; +} + +/** + * Map a client PID to a command line string. Currently only systems + * with /proc/pid/cmdline are supported. + * + * @param[in] pid Process ID of a client. + * + * @return Command line string of the given process. Error (NULL) if + * the command line string can't be determined from the + * PID. You must release the string by calling free when it's + * not used anymore. + */ +const char *GetCommandFromPid(pid_t pid) +{ + char path[PATH_MAX + 1]; + char *cmd = NULL; + int fd = 0; + int bytes = 0; + + if (pid == -1) + return NULL; + + if (snprintf(path, sizeof(path), "/proc/%d/cmdline", pid) < 0) + return NULL; + + fd = open(path, O_RDONLY); + if (fd < 0) + return NULL; + bytes = read(fd, path, sizeof(path)); + if (bytes <= 0) + return NULL; + if (close(fd) < 0) + return NULL; + + /* We are only interested in the process name. We don't care about + * its arguments. Allocate space only for the process name. */ + path[bytes - 1] = '\0'; + bytes = strlen(path) + 1; + cmd = malloc(bytes); + if (cmd == NULL) + return NULL; + strncpy(cmd, path, bytes); + cmd[bytes - 1] = '\0'; + + return cmd; +} + static int AuthorizedClient(ClientPtr client) { -- 1.6.3.3 _______________________________________________ xorg-devel@lists.x.org: X.Org development Archives: http://lists.x.org/archives/xorg-devel Info: http://lists.x.org/mailman/listinfo/xorg-devel