unix-socket

How to reliably unlink() a Unix domain socket in Go programming language

老子叫甜甜 提交于 2019-12-05 01:58:57
I have a Go program hosting a simple HTTP service on localhost:8080 so I can connect my public nginx host to it via the proxy_pass directive, as a reverse proxy to serve part of my site's requests. This is all working great, no problems there. I want to convert the Go program to host the HTTP service on a Unix domain socket instead of a local TCP socket for improved security and to reduce the unnecessary protocol overhead of TCP. PROBLEM : The problem is that Unix domain sockets cannot be reused once they are bind() to, even after program termination. The second time (and every time after) I

Is there a way to get the uid of the other end of a unix socket connection

末鹿安然 提交于 2019-12-04 11:56:31
问题 Is there a way for a UNIX domain socket listener to only accept connection from certain user ( chmod / chown does not work for abstract socket afaik), or in another word, get the uid of the incoming connection (on Linux)? Dbus, which uses abstract unix socket on Linux, has a function GetConnectionUnixUser which is used by polkit to determine the caller. So I suppose the dbus-daemon must have a way to do that. Does anyone know how that works? 回答1: The easiest way to check peer credentials is

How to know whether any process is bound to a Unix domain socket?

牧云@^-^@ 提交于 2019-12-04 07:51:45
问题 I'm writing a Unix domain socket server for Linux. A peculiarity of Unix domain sockets I quickly found out is that, while creating a listening Unix socket creates the matching filesystem entry, closing the socket doesn't remove it. Moreover, until the filesystem entry is removed manually, it's not possible to bind() a socket to the same path again : bind() fails with EADDRINUSE if the path it is given already exists in the filesystem. As a consequence, the socket's filesystem entry needs to

C unix domain sockets, recvfrom() doesn't set struct sockaddr* src_addr

喜夏-厌秋 提交于 2019-12-04 05:58:10
I'm writing an application that listens for UDP packets over a unix domain socket. Consider the following code block. int sockfd; struct sockaddr_un servaddr; sockfd = socket(AF_LOCAL, SOCK_DGRAM, 0); if(sockfd < 0) { perror("socket() failed"); } unlink(port); bzero(&servaddr, sizeof(servaddr)); servaddr.sun_family = AF_LOCAL; strcpy(servaddr.sun_path, port); if(bind(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) { perror("bind() failed"); close(sockfd); } int n; struct sockaddr_un cliaddr; socklen_t len = sizeof(cliaddr); discovery_msgs client_message; bzero(&client_message,

How to view and edit the ephemeral port range on Linux?

匆匆过客 提交于 2019-12-04 05:01:36
In my Linux system ephemeral port range is showing different ranges as follows $ cat /proc/sys/net/ipv4/ip_local_port_range 32768 61000  cat /etc/sysctl.conf | grep net.ipv4.ip_local_port_range net.ipv4.ip_local_port_range = 9000 65500 Which will be the effective ephemeral port range in my system? Following command will list the ephemeral port range in Linux system sysctl -A | grep ip_local_port_range If we don't want to reboot, after editing /etc/sysctl.conf file if we execute following command it will be effective. sysctrl -p /etc/sysctl.conf . The truth of the matter of effective range is

How to make two-directional unix domain sockets with SOCK_DGRAM?

走远了吗. 提交于 2019-12-04 03:58:22
问题 I am trying to write a simple Unix datagram server/client, and am having some problems. What I want is a server that listens on a datagram socket and sends a reply to every message received, to the original sender. I decided to try first using socat to be the "server" and writing the client in C. I am running socat like this: socat UNIX-DGRAM:/tmp/test.socket,fork EXEC:echo To the best of my understanding this should listen on /tmp/test.socket and reply to everything that is received with the

Unix Sockets : AF_LOCAL vs AF_INET

99封情书 提交于 2019-12-04 03:44:25
I'm just starting with socket programming in UNIX and I was reading the man pages for the socket system call. I'm a bit confused about the AF_LOCAL argument and when it is used. The manual just says local communication. Wouldn't an AF_INET format also work for local communication? AF_LOCAL uses UNIX domain sockets which are local to the filesystem and can be used for internal communications. AF_INET is an IP socket. AF_LOCAL will not incur some performance penalties related to sending data over IP. See this old but very nice discussion of the topic. 来源: https://stackoverflow.com/questions

Getting “Address already in use” error using Unix socket

[亡魂溺海] 提交于 2019-12-04 03:25:10
Writing the C source below using Unix local sockets I got an error about the address already in use. After having checked man 7 Unix for further informations I tried to create a sub-folder where executing my program (obviously modifying the sun_path field on the current folder) but the error was ever the same. Is there someone able to help me? Source code: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> #include <sys/un.h> #include <unistd.h> #include <errno.h> #define MAXLEN 128 int main (int argc, char *argv[]){ struct sockaddr_un

Identify program that connects to a Unix Domain Socket

天涯浪子 提交于 2019-12-04 01:28:40
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 the connection or not (based on the user/group settings). Is this possible under Linux, and if so, how? Yes, this is possible on Linux, but it won't be very portable. It's achieved using what is called "ancillary data" with sendmsg / recvmsg . Use SO_PASSCRED with setsockopt Use SCM_CREDENTIALS and the struct ucred structure This structure is defined in Linux: struct ucred { pid_t pid; /* process ID of the sending process */

What are the differences from running PHP-FPM over an Unix Socket vs a TCP/IP Socket?

不问归期 提交于 2019-12-03 23:37:34
There are these two ways of running PHP-FPM. I know that nothing is bullet-proof in tech, but what are the pros and cons from both methods? The difference is mainly the added overhead of using the full network stack to "pack" and "unpack" every piece of data. Mind you that the overhead is negligible for most deployments Using a socket (e.g. listen = '/tmp/php-fpm.sock') makes sense when both the front-end (e.g. Nginx) and php-fpm are in the same box and You have the option to scale horizontally both the front and back-end together (say you are building a container with both and you can create