Skip to content

Instantly share code, notes, and snippets.

@Cloudef
Created August 18, 2013 17:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Cloudef/6262720 to your computer and use it in GitHub Desktop.
Save Cloudef/6262720 to your computer and use it in GitHub Desktop.
/* gcc `pkg-config --cflags --libs libsystemd-login` systemd-active-user.c -o systemd-active-user
* get current active user id that has process in 'foreground' according to documentation */
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
uid_t *uids = NULL, uactiveid = 0;
int i, num = sd_get_uids(&uids);
for (i = 0; i < num; ++i) {
char *state = NULL;
sd_uid_get_state(uids[i], &state);
if (!strcmp(state, "active")) uactiveid = uids[i];
if (state) free(state);
}
if (uids) free(uids);
printf("%u\n", uactiveid);
return EXIT_SUCCESS;
}
/* vim: set ts=8 sw=4 tw=0 :*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment