_public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) { assert_return(pid >= 0, -EINVAL); assert_return(uid, -EINVAL); return cg_pid_get_owner_uid(pid, uid); }
_public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) { int r; assert_return(pid >= 0, -EINVAL); assert_return(uid, -EINVAL); r = cg_pid_get_owner_uid(pid, uid); return IN_SET(r, -ENXIO, -ENOMEDIUM) ? -ENODATA : r; }
_public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) { struct ucred ucred; int r; assert_return(fd >= 0, -EINVAL); assert_return(uid, -EINVAL); r = getpeercred(fd, &ucred); if (r < 0) return r; return cg_pid_get_owner_uid(ucred.pid, uid); }
int main(int argc, char *argv[]) { const char *path, *type, *units; int r, q; if (argc > 1 && argc != 4) { log_error("This program takes three or no arguments."); return EXIT_FAILURE; } if (argc > 1) { arg_dest = argv[1]; arg_dest_late = argv[3]; } log_set_target(LOG_TARGET_SAFE); log_parse_environment(); log_open(); umask(0022); if (access("/dev/kdbus/control", F_OK) < 0) return 0; r = cg_pid_get_owner_uid(0, NULL); if (r >= 0) { path = "/usr/share/dbus-1/services"; type = "session"; units = USER_DATA_UNIT_PATH; } else if (r == -ENOENT) { path = "/usr/share/dbus-1/system-services"; type = "system"; units = SYSTEM_DATA_UNIT_PATH; } else { log_error("Failed to determine whether we are running as user or system instance: %s", strerror(-r)); return r; } r = parse_dbus_fragments(path, type); /* FIXME: One day this should just be pulled in statically from basic.target */ q = link_busnames_target(units); if (q < 0) r = q; q = link_compatibility(units); if (q < 0) r = q; return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; }
static void test_proc(void) { _cleanup_closedir_ DIR *d = NULL; struct dirent *de; int r; d = opendir("/proc"); assert_se(d); FOREACH_DIRENT(de, d, break) { _cleanup_free_ char *path = NULL, *path_shifted = NULL, *session = NULL, *unit = NULL, *user_unit = NULL, *machine = NULL, *slice = NULL; pid_t pid; uid_t uid = UID_INVALID; if (de->d_type != DT_DIR && de->d_type != DT_UNKNOWN) continue; r = parse_pid(de->d_name, &pid); if (r < 0) continue; if (is_kernel_thread(pid)) continue; cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &path); cg_pid_get_path_shifted(pid, NULL, &path_shifted); cg_pid_get_owner_uid(pid, &uid); cg_pid_get_session(pid, &session); cg_pid_get_unit(pid, &unit); cg_pid_get_user_unit(pid, &user_unit); cg_pid_get_machine_name(pid, &machine); cg_pid_get_slice(pid, &slice); printf(PID_FMT"\t%s\t%s\t"UID_FMT"\t%s\t%s\t%s\t%s\t%s\n", pid, path, path_shifted, uid, session, unit, user_unit, machine, slice); } }