通过posix_spawn进行同步

Rsync through posix_spawn

本文关键字:同步 spawn posix 通过      更新时间:2023-10-16

我有一个正在运行的 rsync 守护进程,我可以成功执行以下命令:

rsync --port=1873 -avWh 127.0.0.1::jackfruit_peers/data.0a6/home/v/data

但从代码中:

local_dir =/home/v/data

remote_dir = 127.0.0.1::jackfruit_peers/data.0a6

    pid_t child_pid;
    char cmd[] = "rsync -avWh --port=1873";
    char *argv[] = {cmd, remote_dir, local_dir,
                    (char*) 0}; 
    if (0 != posix_spawn(&child_pid, "/usr/bin/rsync", NULL, NULL, argv, environ)) {
        logger::error("posix spawn");
        return ERR_INTERNAL_SERVER_ERROR;
    }   

我收到错误:

rsync:无法连接到 127.0.0.1

(127.0.0.1):连接被拒绝 (111) rsync 错误:客户端服务器上的套接字 IO(代码 10)错误.c(128) [接收器=3.1.0] 2015-01-16 15:27:08.421.732 28623 0x7fbc01914010信息 唤醒其子 pid = 30060 的父母。错误=0

知道为什么吗?

编辑:rsync 句柄定义为:

 [jackfruit_peers]
    comment = for data transfer
    path = /home/jackfruit/
    read only = yes
    timeout  =  60

更改为这种丑陋的格式,它可以工作:

   pid_t child_pid;
    char a1[] = "rsync";
    char a2[] = "-avWh";
    char a3[] = "--port=1873";
    char *argv[] = {a1, a2, a3, remote_dir, local_dir,
                   (char*)0};
    if (0 != posix_spawn(&child_pid, "/usr/bin/rsync", NULL, NULL, argv, environ)) {
        logger::error("posix spawn");
        return ERR_INTERNAL_SERVER_ERROR;
    }   

当我使用实际的字符串常量代替 a1、a2、a3 时,我得到了从 const char* 到 char* 的不推荐使用的字符串转换,因此我愚蠢地将所有内容分解为:

char cmd[] = "rsync -avWh --port=1873";