Created
December 31, 2019 01:17
-
-
Save iquiw/3f98e56e0a6b0e52e32bd88133a0878d to your computer and use it in GitHub Desktop.
UID of unix domain socket
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/socket.h> | |
#include <sys/stat.h> | |
#include <sys/un.h> | |
#include <err.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <unistd.h> | |
const char SOCK_PATH[] = "/tmp/foo-socket"; | |
int main(int argc, char *argv[]) | |
{ | |
int ss = socket(AF_LOCAL, SOCK_STREAM, 0); | |
if (ss < 0) { | |
err(1, "server socket"); | |
} | |
struct sockaddr_un sun; | |
sun.sun_len = sizeof(sun); | |
sun.sun_family = AF_LOCAL; | |
strcpy(sun.sun_path, SOCK_PATH); | |
if (bind(ss, (const struct sockaddr *) &sun, sizeof(sun)) < 0) { | |
err(1, "bind"); | |
} | |
if (listen(ss, 1) < 0) { | |
err(1, "listen"); | |
} | |
int cs = socket(AF_LOCAL, SOCK_STREAM, 0); | |
if (cs < 0) { | |
err(1, "client socket"); | |
} | |
if (connect(cs, (const struct sockaddr *) &sun, sizeof(sun)) < 0) { | |
err(1, "connect"); | |
} | |
struct stat st; | |
if (fstat(cs, &st) < 0) { | |
err(1, "fstat"); | |
} | |
printf("uid = %d\n", st.st_uid); | |
close(cs); | |
close(ss); | |
unlink(SOCK_PATH); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment