Node.js:使用 fs.writeFileSync 写入系统文件

Node.js: Writing to system files with fs.writeFileSync

我正在尝试使用 fs.writeFileSync 写入 /sys/kernel/config/usb_gadget 下的系统文件,但是当写入 "" 作为内容时,文件保持不变(原始内容完整)并且结果

Error: EBUSY: resource busy or locked, write
    at Object.writeSync (fs.js:581:3)
    at Object.writeFileSync (fs.js:1275:26)
    at Socket.<anonymous> (/opt/sterling/ip-kvm-interface/app.js:249:6)
    at Socket.emit (events.js:210:5)
    at /opt/sterling/ip-kvm-interface/node_modules/socket.io/lib/socket.js:528:12
    at processTicksAndRejections (internal/process/task_queues.js:75:11) {
  errno: -16,
  syscall: 'write',
  code: 'EBUSY'
}

在写一些其他内容的时候。目标写入文件的权限为 777。

fs.writeFileSync 是否无法写入 sys 下的文件,还是我遗漏了其他内容?

不使用 fsuser /sys/kernel/config/usb_gadget/kvm-gadget/UDC returns 什么都没有(即使节点进程是 运行)并且 lsof | grep /sys/kernel/config/usb_gadget/kvm-gadget/UDC 也 returns 什么都没有。

我是否必须 spawn 一个 echo 过程才能让它工作(不是首选,但我想到了 - 因为不确定如何将其转换为同步任务)?

https://github.com/nodejs/help/issues/2459

Are there undocumented limitations to fs.writeFileSync that I am unaware of?

Nothing specific to fs.writeFileSync(), you can get the same error with a plain C program.

/sys/kernel/config/usb_gadget is not a real file, it's a communication channel with the kernel's usb gadget driver. It's that driver that is returning the error.

(I could point you to the line of code if you're really interested. It's drivers/usb/gadget/configfs.c in the kernel source tree in any case.)