使用erlang打开设备文件 [英] Opening a device file using erlang

查看:152
本文介绍了使用erlang打开设备文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在Solaris上正在尝试以下操作:

 
Erlang(BEAM)仿真器版本5.6 [source] [64位] [async-threads:0] [kernel-poll:false]

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

如上所述,erlang文件驱动程序在打开时没有问题没有打开终端设备文件!!



无法得到结论,因为文件驱动程序能够打开一个空文件。



有没有其他方式打开终端设备文件?



谢谢

解决方案

更新:我可以使用端口解决下述限制。例如,这里是一个示例程序,将hello world打印到 / dev / stdout

   - 模(测试)。 
-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)& !定义(OSE)
/ *
*对于UNIX,这里是一些丑陋的代码,允许
* / dev / null作为文件打开。
*
*假设:/ dev / null的i节点号不能为零。
* /
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)&& !定义(OSE)
}
#endif
}

如果文件不是常规文件(这是 ISREG(statbuf)) EISDIR 错误>检查),,除非该文件特别是 / dev / null 文件(3)文档说明:

  eisdir:
命名文件不是常规文件。它可能是一个目录,一个
fifo或一个设备。

所以实际上是这样做的。我不知道为什么这个限制存在,尽管如此,这可能与性能有关,因为设备驱动程序通常会比普通文件阻止更多的时间。


Is there any way to open a terminal device file in erlang ?

I am on Solaris and I am trying the following::

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!!

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:

-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:

    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. The file(3) documentation states:

     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.

这篇关于使用erlang打开设备文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆