Identify program that connects to a Unix Domain Socket

前端 未结 3 1567
星月不相逢
星月不相逢 2021-02-07 17:37

I have a program that is listening to a Unix Domain Socket.

When a client connects to the socket I\'d like to find out which program connected and then decide if I allow

3条回答
  •  佛祖请我去吃肉
    2021-02-07 18:14

    I searched for this quite a bit, so I will show you this example on how to use SO_PEERCRED on a socket sock to get the pid/uid/gid of the peer of a socket:

    int len;
    struct ucred ucred;
    
    len = sizeof(struct ucred);
    
    if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) {
        //getsockopt failed
    }
    
    printf("Credentials from SO_PEERCRED: pid=%ld, euid=%ld, egid=%ld\n",
        (long) ucred.pid, (long) ucred.uid, (long) ucred.gid);
    

提交回复
热议问题