简体   繁体   English

使用erlang打开设备文件

[英]Opening a device file using erlang

Is there any way to open a terminal device file in erlang ? 有没有办法在erlang中打开终端设备文件?

I am on Solaris and I am trying the following:: 我在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>

As can be seen above the erlang file driver has no problem in opening a null fle but does not open a terminal device file!! 从上面可以看出,erlang文件驱动程序在打开空文件时没有问题,但是没有打开终端设备文件!

Unable to come to a conclusion as the file driver is able to open a null file. 由于文件驱动程序能够打开空文件,因此无法得出结论。

Is there any other way to open terminal device files ? 有没有其他方法可以打开终端设备文件?

Thanks 谢谢

Update : I was able to work around the limitation described below using a port. 更新 :我能够使用端口解决下面描述的限制。 For example, here is a sample program that prints "hello world" to /dev/stdout : 例如,这是一个示例程序,它将“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"}}.

This is a bit inconvenient because a port doesn't act like a regular file, but at least it's one way to get the job done. 这有点不方便,因为端口不像常规文件那样,但至少它是完成工作的一种方法。


In efile_openfile() (in erts/emulator/drivers/unix/unix_efile.c ) there is the following code: 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
    }

This code (confusingly) returns the EISDIR error if the file is not a regular file (which is the ISREG(statbuf) check), unless the file specifically is /dev/null . 如果文件不是常规文件(即ISREG(statbuf)检查),则此代码(容易引起混淆)返回EISDIR错误, 除非文件具体为/dev/null The file(3) documentation states: file(3)文档说明:

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

so it's actually documented to do that. 所以事实证明这样做。 I'm not sure why that restriction exists, though—perhaps it's got something to do with performance because device drivers might block for more time than an ordinary file generally will. 我不确定为什么存在这种限制 - 也许它与性能有关,因为设备驱动程序可能会阻塞比普通文件更多的时间。

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

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