2010-12-23 70 views
5

有没有什么办法在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的文件驾驶员看见在打开一个空FLE没有问题,但不会打开一个终端设备文件!

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

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

感谢

+0

解决方法可能有所帮助:您可以自己写一个包装器,例如在C或Python中,你开始作为一个端口。 – ZeissS 2010-12-24 01:38:43

+0

@ZeissS:是的,那样做。但我在想为什么不这样呢?我可以在Perl中做到这一点。 – Arunmu 2010-12-24 03:40:53

回答

8

更新:我能解决使用以下端口描述的限制。例如,下面是一个示例程序输出“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/nullfile(3)文档指出:

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

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