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

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

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

Related

What's "EIO: i/o error, write" in node.js and how do I eliminate it?

I have a node.js app using elasticsearch to gather error data and notify relevant stack holders. It starts to give me errors lately (no change has made to the code):
write EIO
Error: write EIO
at afterWriteDispatched (internal/stream_base_commons.js:154:25)
at writeGeneric (internal/stream_base_commons.js:145:3)
at WriteStream.Socket._writeGeneric (net.js:783:11)
at WriteStream.Socket._write (net.js:795:8)
at doWrite (_stream_writable.js:385:12)
at writeOrBuffer (_stream_writable.js:367:5)
at WriteStream.Writable.write (_stream_writable.js:307:12)
at Stdio.write (/project/node_modules/elasticsearch/src/lib/loggers/stdio.js:58:6)
at Stdio.onError (/project/node_modules/elasticsearch/src/lib/loggers/stdio.js:70:8)
at Log.wrapper (/project/node_modules/lodash/lodash.js:4929:19)
at Log.emit (events.js:315:20)
at Log.error (/project/node_modules/elasticsearch/src/lib/log.js:239:17)
at checkRespForFailure (/project/node_modules/elasticsearch/src/lib/transport.js:298:18)
at HttpConnector.<anonymous> (/project/node_modules/elasticsearch/src/lib/connectors/http.js:171:7)
at ClientRequest.wrapper (/project/node_modules/lodash/lodash.js:4929:19)
at ClientRequest.emit (events.js:315:20)
I suspect this has to do with some type of I/O error on the disk. What can I do to eliminate the error? I am using Node.js v14.0.0 and elasticsearch (npm package) 16.7.1.
This can be caused by multiple reasons:
If you are using Windows is most likely a unicode issue you can fix by executing chp command like this chcp 850 but if you need Unicode characters then iconv-lite module should help
When trying to write to a closed terminal
Disk related issues, like the disk is full or OS issues... when trying to write the output
Try to reinstall the modules to the latest version and from scratch since maybe you are using a buggy module or it wasn't installed correctly
This turns out to be a bug in ElasticSearch's log feature due to the fact it is writing to the stdio synchronously. We will either have to turn off the log or use a third party logger.

SQLITE_IOEERR: in Meteor

Retrying after error { [Error: SQLITE_IOERR: disk I/O error] errno: 10, code:
'SQLITE_IOERR' }
Retrying after error { [Error: SQLITE_IOERR: disk I/O error] errno: 10, code:
'SQLITE_IOERR' }
/home/kdibbs/.meteor/packages/meteor-tool/.1.4.2.1r0536n++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/lib/node_modules/meteor-promise/promise_server.js:190
throw error;
^
Error: SQLITE_IOERR: disk I/O error
at Error (native)
=> awaited here:
at Promise.await (/home/kdibbs/.meteor/packages/meteor-tool/.1.4.2.1r0536n++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/lib/node_modules/meteor-promise/promise_server.js:39:12)
at Db._execute (/tools/packaging/catalog/catalog-remote.js:355:8)
at /tools/packaging/catalog/catalog-remote.js:144:10
at Db._retry (/tools/packaging/catalog/catalog-remote.js:156:16)
at new Db (/tools/packaging/catalog/catalog-remote.js:143:8)
at RemoteCatalog.initialize (/tools/packaging/catalog/catalog-remote.js:694:15)
at /tools/cli/main.js:815:20
So I ran this meteor program a few days ago, then I made a few more users and a group on my machine..All of which didn't affect my normal user...And now I'm getting this error? Any clues?
Thanks.
Did you try searching for this? Looking at the SQLite reference here https://www.sqlite.org/rescode.html#ioerr
It sounds like one of your disks is giving an error - something like too many files open, or perhaps it's getting full?
(10) SQLITE_IOERR
The SQLITE_IOERR result code says that the operation could not finish
because the operating system reported an I/O error. A full disk drive
will normally give an SQLITE_FULL error rather than an SQLITE_IOERR
error.
There are many different extended result codes for I/O errors that
identify the specific I/O operation that failed.

Error: spawn EMFILE Node js

I am getting this error. While a person logs in to the application, logs are entered into a file using utilityWrapper. This file is read/Written when ever some activity is happening in the application. Then suddenly after some time
'throw errnoException(process._errno, 'spawn');
Error: spawn EMFILE
at errnoException (child_process.js:988:11)
at ChildProcess.spawn (child_process.js:935:11)
at Object.exports.spawn (child_process.js:723:9)
at Object.Logger (/web/utilityWrapper.js:21:17)
at process.env.NODE_TLS_REJECT_UNAUTHORIZED (/web/server.js:141:17)
at process.EventEmitter.emit (events.js:95:17)
at process._fatalException (node.js:272:26)
This error come up.
Please Help me.
This had happened due to various reasons. The code had many console.logs, which was printing many things on the command prompt on which the server was running. As it was writing to a file, it was using a file descriptor(fd). The ulimit of file descriptor is 1024.
using the command
lsof - -i -n -P | grep nodejs
I was able to capture the information about each fd.
Fd count used to increase when a user loogged into the application.The ldap sockets and mysql db connections also consumed a large amount of fd's.
So guys keep these points in mind when you get this kind of Error.

Running imagemagick on Azure with node.js

I have a website on Azure and can't seem to run imagemagick.
I have the binaries there, and have changed my javascript files to point to the imagemagick binaries. The binaries seem to be running, but they don't accept any input using stdin. When I try to write to stdin I get an error about the socket being closed
Error: This socket is closed.
at Socket._write (net.js:635:19)
at doWrite (_stream_writable.js:221:10)
at writeOrBuffer (_stream_writable.js:211:5)
at Socket.Writable.write (_stream_writable.js:180:11)
at Socket.write (net.js:613:40)
at Duplex.ImageMagick._write (\\100.68.148.84\volume-9-default\f9e21fc24431ae1e3688\d6a9b08558464073a94176eb39d78b10\site\wwwroot\node_modules\imagemagick-stream\index.js:47:17)
at Duplex.<anonymous> (\\100.68.148.84\volume-9-default\f9e21fc24431ae1e3688\d6a9b08558464073a94176eb39d78b10\site\wwwroot\node_modules\imagemagick-stream\index.js:52:12)
at Duplex.g (events.js:175:14)
at Duplex.EventEmitter.emit (events.js:95:17)
at Duplex._spawn (\\100.68.148.84\volume-9-default\f9e21fc24431ae1e3688\d6a9b08558464073a94176eb39d78b10\site\wwwroot\node_modules\imagemagick-stream\index.js:184:8)
Is there a fix for this? Is it failing because I am running a website and not a cloud service? I know that it is possible to get working somhow, because I've seen a few examples, but the examples are out of date and dont go over server configuration that much
Thanks for your help.

Node.js stdout flush

Maybe I'm duplicating this question, but the answer provided refers probably to an older version of node (i'm using v0.5.10), because fs.writeSync(1, "Foo\n"); alone doesn't solve my problem (the output doesn't get flushed when executing the script from a shell script) and fs.fsyncSync(1); is not documented and when I try it node rises the exception:
return binding.fsync(fd);
^
Error: UNKNOWN, Operation complete;
at Object.fsyncSync (fs.js:353:18)
at Interface.<anonymous> (C:\Dev\Git\org.github.pistacchio.streamcommunication\nodebot.js:36:12)
at Interface.emit (events.js:67:17)
at Interface._onLine (readline.js:162:10)
at Interface._line (readline.js:426:8)
at Interface._ttyWrite (readline.js:603:14)
at ReadStream.<anonymous> (readline.js:82:12)
at ReadStream.emit (events.js:88:20)
at ReadStream._emitKey (tty.js:309:10)
at ReadStream.onData (tty.js:66:12)
You want it to be blocking, but I guess streamlinejs should work for you, too:
var done = process.stdout.write(str);
if (!done) {
// this effectively means "wait for this
// event to fire", but it doesn't block everything
process.stdout.on('drain', _)
}
doSome(otherStuff)
You can paste this code here to see what it will be compiled to when you call it. Also, you'll have to install streamline with npm install streamline -g, to put your code in a file which has a name ending with _.js and to run your code with node-streamline filename_.

Resources