当我以普通用户身份手动运行脚本时,一切正常。但是如果收到一封电子邮件并将其通过管道传输到 Go 脚本中,我将无法打开串口,因为文件不存在。
postfix 1239 1025 0 13:20 ? 00:00:00 pipe -n watchParadox -t unix flags=F user=watch argv=/usr/local/bin/watch -paradox
watch 1240 1239 0 13:20 ? 00:00:00 /usr/local/bin/watch -paradox
脚本在已添加到 dialout 组的 watch 用户下运行,为了确定,postfix 用户也在 dialout 中。
在我的脚本中,我运行了 ls -la 命令来找出哪些文件确实存在:
drwxr-xr-x 6 root root 380 Feb 25 13:19 .
dr-xr-xr-x. 18 root root 4096 Feb 22 17:53 ..
lrwxrwxrwx 1 root root 11 Feb 25 13:19 core -> /proc/kcore
lrwxrwxrwx 1 root root 13 Feb 25 13:19 fd -> /proc/self/fd
crw-rw-rw- 1 root root 1, 7 Feb 25 13:19 full
drwxr-xr-x 2 root root 0 Feb 25 13:19 hugepages
lrwxrwxrwx 1 root root 28 Feb 25 13:19 log -> /run/systemd/journal/dev-log
drwxrwxrwt 2 root root 40 Feb 25 13:19 mqueue
crw-rw-rw- 1 root root 1, 3 Feb 25 13:19 null
lrwxrwxrwx 1 root root 8 Feb 25 13:19 ptmx -> pts/ptmx
drwxr-xr-x 2 root root 0 Feb 25 13:19 pts
crw-rw-rw- 1 root root 1, 8 Feb 25 13:19 random
drwxrwxrwt 2 root root 40 Feb 25 13:19 shm
lrwxrwxrwx 1 root root 15 Feb 25 13:19 stderr -> /proc/self/fd/2
lrwxrwxrwx 1 root root 15 Feb 25 13:19 stdin -> /proc/self/fd/0
lrwxrwxrwx 1 root root 15 Feb 25 13:19 stdout -> /proc/self/fd/1
crw-rw-rw- 1 root root 5, 0 Feb 25 13:19 tty
crw-rw-rw- 1 root root 1, 9 Feb 25 13:19 urandom
我确定以下文件存在,但在执行后缀管道时未显示。
crw-rw---- 1 root dialout 166, 0 25. úno 13.19 ttyACM0
我在文件上尝试了 chmod 777,但在那个部门没有运气。通过 https://github.com/tarm/serial 打开文件使用 Go 函数的库:
os.OpenFile("ttyACM0", syscall.O_RDWR|syscall.O_NOCTTY|syscall.O_NONBLOCK, 0666)
结果为
ttyACM0: no such file or directory
问题不在于路径,因为我尝试了相对路径(使用 chdir)和绝对路径,结果完全相同。
我什至禁用了 SELinux,这不是我想做的,但为了寻找解决方案,我尝试了一切。
代码没有问题,因为脚本的手动执行正确通过。我认为 Linux 设置有问题。
最佳答案
ttyACM0 的正确路径是/dev/ttyACM0。您定义一个 udev 规则以在附加文件时设置文件模式。
如果您的程序无法访问它,则可能是您的程序运行了 chrooted()。
关于linux -/dev/ttyACM0 Postfix 管道时文件不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42456328/