android - 如何在 Android 中创建命名管道 (mkfifo)?

标签 android android-ndk named-pipes

我在 Android 中创建命名管道时遇到问题,下面的示例说明了我的困境:

res = mkfifo("/sdcard/fifo9000", S_IRWXO);
if (res != 0)
{
    LOG("Error while creating a pipe (return:%d, errno:%d)", res, errno);
}

代码总是打印:

Error while creating a pipe (return:-1, errno:1)

我无法弄清楚为什么会失败。该应用程序具有 android.permission.WRITE_EXTERNAL_STORAGE 权限。我可以在同一位置创建具有完全相同名称的普通文件,但管道创建失败。有问题的管道应该可以从多个应用程序访问。

  1. 我怀疑没有人可以在/sdcard 中创建管道。这样做的最佳地点在哪里?
  2. 我应该设置什么模式桅杆(第二个参数)?
  3. 应用程序是否需要任何额外权限?

最佳答案

Roosmaa's answer是正确的——mkfifo() 只是调用 mknod() 来创建一个特殊文件,而 FAT32 不支持它。

作为替代方案,您可能需要考虑使用 Linux 的“抽象 namespace ”UNIX 域套接字。它们应该大致相当于一个命名管道。您可以通过名称访问它们,但它们不是文件系统的一部分,因此您不必处理各种权限问题。请注意,套接字是双向的。

由于它是一个套接字,您可能需要 INTERNET 权限。不确定。

这里有一小段客户端/服务器示例代码:

#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stddef.h>
#include <sys/socket.h>
#include <sys/un.h>

/*
 * Create a UNIX-domain socket address in the Linux "abstract namespace".
 *
 * The socket code doesn't require null termination on the filename, but
 * we do it anyway so string functions work.
 */
int makeAddr(const char* name, struct sockaddr_un* pAddr, socklen_t* pSockLen)
{
    int nameLen = strlen(name);
    if (nameLen >= (int) sizeof(pAddr->sun_path) -1)  /* too long? */
        return -1;
    pAddr->sun_path[0] = '\0';  /* abstract namespace */
    strcpy(pAddr->sun_path+1, name);
    pAddr->sun_family = AF_LOCAL;
    *pSockLen = 1 + nameLen + offsetof(struct sockaddr_un, sun_path);
    return 0;
}

int main(int argc, char** argv)
{
    static const char* message = "hello, world!";
    struct sockaddr_un sockAddr;
    socklen_t sockLen;
    int result = 1;

    if (argc != 2 || (argv[1][0] != 'c' && argv[1][0] != 's')) {
        printf("Usage: {c|s}\n");
        return 2;
    }

    if (makeAddr("com.whoever.xfer", &sockAddr, &sockLen) < 0)
        return 1;
    int fd = socket(AF_LOCAL, SOCK_STREAM, PF_UNIX);
    if (fd < 0) {
        perror("client socket()");
        return 1;
    }

    if (argv[1][0] == 'c') {
        printf("CLIENT %s\n", sockAddr.sun_path+1);

        if (connect(fd, (const struct sockaddr*) &sockAddr, sockLen) < 0) {
            perror("client connect()");
            goto bail;
        }
        if (write(fd, message, strlen(message)+1) < 0) {
            perror("client write()");
            goto bail;
        }
    } else if (argv[1][0] == 's') {
        printf("SERVER %s\n", sockAddr.sun_path+1);
        if (bind(fd, (const struct sockaddr*) &sockAddr, sockLen) < 0) {
            perror("server bind()");
            goto bail;
        }
        if (listen(fd, 5) < 0) {
            perror("server listen()");
            goto bail;
        }
        int clientSock = accept(fd, NULL, NULL);
        if (clientSock < 0) {
            perror("server accept");
            goto bail;
        }
        char buf[64];
        int count = read(clientSock, buf, sizeof(buf));
        close(clientSock);
        if (count < 0) {
            perror("server read");
            goto bail;
        }
        printf("GOT: '%s'\n", buf);
    }
    result = 0;

bail:
    close(fd);
    return result;
}

关于android - 如何在 Android 中创建命名管道 (mkfifo)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2740321/

相关文章:

android - NDK 应用程序 onDestroy 清理 - 如何分离当前线程

Android NDK 将长值传递给 native 方法

wcf - 您如何配置 WCF 以支持使用 net.pipe 的主机和客户端在同一进程中的 FaultContract?

c++ - Windows 服务与 NamedPipe 之间的访问安全

java - 为什么我的值没有从内部类添加到全局 ArrayList

android - iOS 会破坏旋转 View 吗?

go - os.Create 和 os.Open 不能与 gomobile 一起使用并 react native

c# - 只有一个客户端可以连接到命名管道

android - 将自定义字体设置为对话框

java - 无法将文本设置为 TextWatcher 中的 EditView