4

I am trying to write to a system file under /sys/kernel/config/usb_gadget with fs.writeFileSync but when writing "" as the contents, the file remains unchanged (with original contents in tact) and results in

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'
}

when writing some other contents. Permissions for the destination write file are 777.

Is fs.writeFileSync incapable of writing to files under sys or am I missing something else?

Using fsuser /sys/kernel/config/usb_gadget/kvm-gadget/UDC returns nothing (even when Node process is running) and lsof | grep /sys/kernel/config/usb_gadget/kvm-gadget/UDC also returns nothing.

Am I going to have to spawn an echo process to get this to work (not preferred but crossed my mind - since not sure how I would convert it to synchronous task)?

Sterling Butters
  • 1,024
  • 3
  • 20
  • 41
  • Does `Error: EBUSY: resource busy or locked` not explain that the resource is in use and can't be written to by your process? – jfriend00 Jan 30 '20 at 23:57
  • Are you able to do these writes as the same user as your process from a shell? – Joe Jan 31 '20 at 00:25
  • @jfriend00 the entire extent of the error message is included in the OP – Sterling Butters Jan 31 '20 at 00:33
  • @Joe yes I can and without sudo – Sterling Butters Jan 31 '20 at 00:33
  • I just meant that the error message seems self explanatory. The resource is busy or locked. You will have to figure out why. – jfriend00 Jan 31 '20 at 00:37
  • @jfriend00 would I be able to write to the file (using echo) in shell per Joe's suggestion if the resource was busy? – Sterling Butters Jan 31 '20 at 00:38
  • Probably not. I don't know what OS you are on, but aren't there OS tools that will let you see what else may be using that file? Does your own process do something with a USB device that could be causing this? – jfriend00 Jan 31 '20 at 00:43
  • @jfriend00 my process does do something with USB but I CAN edit the file while it's "in use" – Sterling Butters Jan 31 '20 at 01:51
  • Can edit the file, how? From a separate editor app? – jfriend00 Jan 31 '20 at 01:52
  • `echo "" > file` results in a persistent change - using raspbian buster – Sterling Butters Jan 31 '20 at 01:53
  • Perhaps some custom `mode` or `flag` option to `fs.writeFileSync()` would make it more likely to work for you? As you can see in [the source](https://github.com/nodejs/node/blob/master/lib/fs.js#L1299), `fs.writeFileSync()` defaults to `mode: 0o666, flag: 'w' ` when you don't pass it anything. Run some experiments and try different modes or flags. – jfriend00 Jan 31 '20 at 01:55
  • @jfriend00 It appears that the mode only refers to the permissions used to access the file for which `0o666` should suffice and `0o777` does not either. the `flags` refer (in my case) to `'w'`: write or `'a'`: append for which I would need the former. Any other ideas? – Sterling Butters Feb 03 '20 at 18:00
  • Please leave some of your code. Maybe I can hep you. – AmerllicA Feb 04 '20 at 16:56
  • 1
    @AmerllicA I can post later today if you don't concur with the answer I provided. I.e. if the answer does not match the issue you anticipated, let me know – Sterling Butters Feb 05 '20 at 17:06
  • Dear bro @SterlingButters, If you think it is the right answer so it is, no doubt. but ticking your own answer doesn't return the bounty to your account reputation. I leave an upvote to your question an upvote to your answer. hope it motivates you to keep going on Stack Overflow. For more information contact me `amerllica@gmail.com` – AmerllicA Feb 05 '20 at 18:34
  • @AmerllicA yes it's kind of unfortunately it works that way - thank you for the up votes (Def makes me regret the bounty less) – Sterling Butters Feb 05 '20 at 22:38
  • Dear @SterlingButters, Do not regret, please give your email or send an email to my email address that I wrote it on above comment. I wanna say a private thing to you. – AmerllicA Feb 06 '20 at 09:09

1 Answers1

4

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.)

Sterling Butters
  • 1,024
  • 3
  • 20
  • 41