繁体   English   中英

使用erlang打开设备文件

[英]Opening a device file using erlang

有没有办法在erlang中打开终端设备文件?

我在Solaris上,我正在尝试以下::

Erlang (BEAM) emulator version 5.6 [source] [64-bit] [async-threads:0] [kernel-poll:false]

/xlcabpuser1/xlc/abp/arunmu/Dolphin/ebin
Eshell V5.6  (abort with ^G)
1> file:open("/dev/pts/2",[write]).
{error,eisdir}
2> file:open("/dev/null",[write]).
{ok,}
3>

从上面可以看出,erlang文件驱动程序在打开空文件时没有问题,但是没有打开终端设备文件!

由于文件驱动程序能够打开空文件,因此无法得出结论。

有没有其他方法可以打开终端设备文件?

谢谢

更新 :我能够使用端口解决下面描述的限制。 例如,这是一个示例程序,它将“hello world”打印到/dev/stdout

-module(test).
-export([main/1]).

main(X) ->
    P = open_port({spawn, "/bin/cat >/dev/stdout"}, [out]),
    P ! {self(), {command, "hello world"}}.

这有点不方便,因为端口不像常规文件那样,但至少它是完成工作的一种方法。


efile_openfile() (在erts/emulator/drivers/unix/unix_efile.c ),有以下代码:

    if (stat(name, &statbuf) >= 0 && !ISREG(statbuf)) {
#if !defined(VXWORKS) && !defined(OSE)
        /*
         * For UNIX only, here is some ugly code to allow
         * /dev/null to be opened as a file.
         *
         * Assumption: The i-node number for /dev/null cannot be zero.
         */
        static ino_t dev_null_ino = 0;

        if (dev_null_ino == 0) {
            struct stat nullstatbuf;

            if (stat("/dev/null", &nullstatbuf) >= 0) {
                dev_null_ino = nullstatbuf.st_ino;
            }
        }
        if (!(dev_null_ino && statbuf.st_ino == dev_null_ino)) {
#endif
            errno = EISDIR;
            return check_error(-1, errInfo);
#if !defined(VXWORKS) && !defined(OSE)
        }
#endif
    }

如果文件不是常规文件(即ISREG(statbuf)检查),则此代码(容易引起混淆)返回EISDIR错误, 除非文件具体为/dev/null file(3)文档说明:

     eisdir :
       The named file is not a regular file. It  may  be  a  directory,  a
       fifo, or a device.

所以事实证明这样做。 我不确定为什么存在这种限制 - 也许它与性能有关,因为设备驱动程序可能会阻塞比普通文件更多的时间。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM