/* compile: gcc -o resolve-by-fd -lsystemd resolve-by-fd.c */ /* run: systemd-socket-activate -l @abstract -l /tmp/ss.sock -l 3333 */ /* activate: nc localhost 3333 */ #include #include #include #include #include #include #include int main() { char **names = NULL; int sd_sockets; sd_sockets= sd_listen_fds_with_names(0, &names); while (sd_sockets--) { int stype= 0, accepting= 0, getnameinfo_err; socklen_t l; union { struct sockaddr sa; struct sockaddr_storage storage; struct sockaddr_in in; struct sockaddr_in6 in6; struct sockaddr_un un; } addr; socklen_t addrlen= sizeof(addr); char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; int fd= SD_LISTEN_FDS_START + sd_sockets; getsockname(fd, &addr.sa, &addrlen); getnameinfo(&addr.sa, addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); if (sbuf[0] == '\0') sbuf[0] = '@'; printf("Using systemd activated socket %s port %-*s\n", hbuf, addrlen - offsetof(struct sockaddr_un, sun_path), sbuf); } return 0; }