You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无法在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可连接服务器,但自定义客户端连接被拒绝

重现步骤

  1. 将client.c和server.c放入HOME目录下的任意路径
  2. 编译并运行服务端:gcc server.c -o server && ./server
  3. 编译并运行客户端:gcc client.c -o client && ./client
  4. 切换不同测试路径验证问题

环境信息

  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 20:55:55