无法在XDG_RUNTIME_DIR或HOME目录创建UNIX域套接字问题
UNIX域套接字无法连接XDG_RUNTIME_DIR或HOME目录问题
我无法在XDG_RUNTIME_DIR或个人HOME目录中连接UNIX域套接字(绑定操作似乎可行),当前操作位于HOME目录下的路径/home/behe/Documents/development/beh/wallpaper_select。
测试情况
- 将套接字路径设为
/tmp目录时可正常工作 - 设为
XDG_RUNTIME_DIR或HOME目录时连接失败 - 使用
nc -U可连接服务器,但自定义客户端连接被拒绝
重现步骤
- 将
client.c和server.c放入HOME目录下的任意路径 - 编译并运行服务端:
gcc server.c -o server && ./server - 编译并运行客户端:
gcc client.c -o client && ./client - 切换不同测试路径验证问题
环境信息
- HOME值:
/home/myusername - XDG_RUNTIME_DIR值:
/run/user/1000 - 文件系统:btrfs
代码
client.c
#include <sys/socket.h> #include <sys/un.h> #include <string.h> #include <unistd.h> #include <stdio.h> #include <stdlib.h> int main() { const char* xdg_dir = getenv("XDG_RUNTIME_DIR"); const char* home = getenv("HOME"); const char* hello_c = "/hello_c"; const char* prefix = "/beh/wallpaper_daemon.sock"; char* S_path = malloc(strlen(xdg_dir)+strlen(prefix)+1); //test0 //strcpy(S_path,"hello_c"); //works //test 1 //strcpy(S_path,xdg_dir); //strcat(S_path,prefix); //doesn't work //test2 strcpy(S_path,home); strcat(S_path,hello_c); //doesn't work //test3 //strcpy(S_path,"/tmp/hello_c"); //works char buf[2400]; printf("%s\nand its length: %li\ndo I have access: %i\n",S_path,strlen(S_path),access(S_path,F_OK)); int client_socket = socket(AF_UNIX,SOCK_STREAM,0); struct sockaddr_un tosend; memset(&tosend,0,sizeof(tosend)); tosend.sun_family = AF_UNIX; strlcpy(tosend.sun_path,S_path,sizeof(tosend.sun_path)); strlcpy(buf,"this is working finally",sizeof(buf)); printf("%s\n",tosend.sun_path); int connection_status = connect(client_socket,(struct sockaddr *) &tosend,sizeof(struct sockaddr)); if (connection_status == -1) perror("connection failed: "); if (write(client_socket,buf,sizeof(buf)) == -1) perror("write failed: "); free(S_path); return 0; }
server.c
#include "sys/types.h" #include <stdio.h> #include <sys/socket.h> #include <string.h> #include <sys/un.h> #include <unistd.h> #include <stdlib.h> #include <sys/stat.h> int main() { const char* xdg_dir = getenv("XDG_RUNTIME_DIR"); char* prefix[] = {"/beh","/wallpaper_daemon.sock"}; const char* home = getenv("HOME"); const char* hello_c = "/hello_c"; char* S_path = malloc(strlen(xdg_dir)+strlen(prefix[0])+strlen(prefix[1])+1); //test0 //strcpy(S_path,"hello_c"); //works //test1 //strcpy(S_path,xdg_dir); //strcat(S_path,prefix[0]); //printf("%s\n",S_path); //mkdir(S_path,0700); //strcat(S_path,prefix[1]); //doesn't work //test2 strcpy(S_path,home); strcat(S_path,hello_c); //doesn't work //test3 //strcpy(S_path,"/tmp/hello_c"); //works printf("%s\nand its length: %li\n",S_path,strlen(S_path)); //get a socket for server int S_sock = socket(AF_UNIX,SOCK_STREAM,0); if (S_sock == -1) perror("getting socket failed: "); struct sockaddr_un S_sock_F; //clearing out the ram for the structure memset(&S_sock_F,0,sizeof(struct sockaddr_un)); S_sock_F.sun_family= AF_UNIX; strlcpy(S_sock_F.sun_path,S_path,sizeof(S_sock_F.sun_path)); int S_bind_state = bind(S_sock, (struct sockaddr *)&S_sock_F, sizeof(S_sock_F)); if (S_bind_state == -1) perror("binding failed: "); int S_listen_state = listen(S_sock,5); if (S_listen_state == -1) perror("listening failed: "); //int S_accept = accept(S_sock,(struct sockaddr *)&S_sock_F,sizeof(struct sockaddr)); char buf[2400]; int S_accept = accept(S_sock,NULL,NULL); //waiting for clients ssize_t msglen; msglen = read(S_accept,buf,sizeof(buf)); printf("%s\n",buf); //perror(""); close(S_sock); unlink(S_sock_F.sun_path); free(S_path); return 0; }
内容的提问来源于stack exchange,提问作者Beh_12517843
相关产品推荐
相关产品推荐

