bool _LSTransportShmInit(_LSTransportShm** shm, bool public_bus, LSError* lserror) { _LSTransportShm* ret_shm = g_new0(_LSTransportShm, 1); if (!ret_shm) { _LSErrorSetOOM(lserror); goto error; } ret_shm->data = _LSTransportShmInitOnce(public_bus, lserror); if (ret_shm->data == MAP_FAILED) { goto error; } *shm = ret_shm; return true; error: if (ret_shm) g_free(ret_shm); return false; }
/** ******************************************************************************* * @brief Get the executable path for a given pid. * * @param pid IN pid * @param lserror OUT set on error * * @retval executable path on success * @retval NULL on failure ******************************************************************************* */ static char* _LSTransportPidToExe(pid_t pid, LSError *lserror) { GError *error = NULL; char *ret = NULL; char *root = NULL; char *exe = NULL; char *proc_exe_path = g_strdup_printf("/proc/%d/exe", pid); char *proc_root_path = g_strdup_printf("/proc/%d/root",pid); if (!proc_exe_path || !proc_root_path) { _LSErrorSetOOM(lserror); goto cleanup; } exe = g_file_read_link(proc_exe_path, &error); if (!exe) { _LSErrorSetFromGError(lserror, error); goto cleanup; } root = g_file_read_link(proc_root_path, &error); if (!root) { _LSErrorSetFromGError(lserror, error); goto cleanup; } int rootlen = strlen(root); if ((rootlen > 1) && (strncmp(exe, root, rootlen) == 0)) { /* /proc/<pid>/root is not a link to "/" so subtract * it from the exe path (it's probably an app running in jail) */ ret = g_strdup(exe + rootlen); } else { ret = g_strdup(exe); } cleanup: if (proc_exe_path) g_free(proc_exe_path); if (proc_root_path) g_free(proc_root_path); if (exe) g_free(exe); if (root) g_free(root); return ret; }
/** ******************************************************************************* * @brief Get the command line for a given pid. * * @param pid IN pid * @param lserror OUT set on error * * @retval command line on success * @retval NULL on failure ******************************************************************************* */ static char* _LSTransportPidToCmdLine(pid_t pid, LSError *lserror) { GError *error = NULL; char *cmd_line = NULL; int i = 0; gsize len = 0; char *proc_cmd_line_path = g_strdup_printf("/proc/%d/cmdline", pid); if (!proc_cmd_line_path) { _LSErrorSetOOM(lserror); goto cleanup; } bool ret = g_file_get_contents(proc_cmd_line_path, &cmd_line, &len, &error); if (!ret) { _LSErrorSetFromGError(lserror, error); goto cleanup; } /* /proc/PID/cmdline has ASCII NUL instead of spaces, so replace all of * them except for the last one */ for (i = 0; i < ((int)len) - 1; i++) { if (cmd_line[i] == '\0') { /* If we get two NULs in a row, we're at the end. * g_file_get_contents() seems to return a larger size than * necessary (calls fstat to determine size) */ if (cmd_line[i + 1] == '\0') { break; } else { cmd_line[i] = ' '; } } } cleanup: if (proc_cmd_line_path) g_free(proc_cmd_line_path); return cmd_line; }