Code fails sometimes and runs sometimes - node.js

I am a newbie as far as node.js is concerned.I wrote the following code to pipeline two linux commands.
This is my nodejs code:
var spawn = require('child_process').spawn,
ls = spawn('ls',['-lh','/usr']),
grep = spawn('grep',['bin']);
/*
ls.stdout.on('data',function(data){
console.log('stdout: '+data);
});
*/
ls.stdout.on('data',function(data){
console.log(""+data);
grep.stdin.write(data);
});
ls.stderr.on('data',function(data){
console.log('stderr: '+data);
});
ls.on('exit',function(code){
console.log('Exit code '+code);
grep.stdin.end();
})
// ------------------------------------
grep.stdout.on('data',function(data){
console.log('stdout: '+data);
});
grep.stderr.on('data',function(data){
console.log('stderr: '+data);
});
Now this code fails sometimes and runs sometimes.I'm getting confused now.
When it fails,it says:
Exit code 0
total 160K
drwxr-xr-x 2 root root 68K Oct 12 12:54 bin
drwxr-xr-x 2 root root 4.0K Jun 20 19:58 games
drwxr-xr-x 54 root root 4.0K Sep 24 17:52 include
drwxr-xr-x 252 root root 44K Oct 2 21:53 lib
drwxr-xr-x 10 root root 4.0K Apr 28 19:16 local
drwxr-xr-x 2 root root 12K Sep 18 15:51 sbin
drwxr-xr-x 362 root root 12K Sep 28 17:58 share
drwxr-xr-x 5 root root 4.0K Jul 7 23:39 src
events.js:66
throw arguments[1]; // Unhandled 'error' event
^
Error: This socket is closed.
at Socket._write (net.js:517:19)
at Socket.write (net.js:509:15)
at Socket.<anonymous> (/home/rajat/nodexperiments/full-spawn.js:13:13)
at Socket.EventEmitter.emit (events.js:88:17)
at Pipe.onread (net.js:395:14)
And when it runs,it says:
total 160K
drwxr-xr-x 2 root root 68K Oct 12 12:54 bin
drwxr-xr-x 2 root root 4.0K Jun 20 19:58 games
drwxr-xr-x 54 root root 4.0K Sep 24 17:52 include
drwxr-xr-x 252 root root 44K Oct 2 21:53 lib
drwxr-xr-x 10 root root 4.0K Apr 28 19:16 local
drwxr-xr-x 2 root root 12K Sep 18 15:51 sbin
drwxr-xr-x 362 root root 12K Sep 28 17:58 share
drwxr-xr-x 5 root root 4.0K Jul 7 23:39 src
Exit code 0
stdout: drwxr-xr-x 2 root root 68K Oct 12 12:54 bin
drwxr-xr-x 2 root root 12K Sep 18 15:51 sbin
Any Ideas?

This is quite clearly a race condition, because node.js is a highly parallel environment you have found a really nice example to demonstrate this.
ls.on('exit',function(code){
console.log('Exit code '+code);
grep.stdin.end();
})
ls fires the above event, before it has finished writing to grep, and then closes the socket that's being used to communicate. The hint your output gives you is that the exit(0) message coming from ls appears once at the top of the output, and once just above the error message.
You shouldn't be closing grep's stdin channel here.
But what about using .exec() instead of spawn on a bash script that does something like
#/bin/bash mybashscript
ls $1 | grep bin
and then using the JavaScript closure, such as
child = exec('mybashscript',['-lh','/usr']
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
With the events your code ordering is more difficult to read.

Related

How to use a Linux "platform_driver"?

There is an embedded system, and it provides functions in a struct of platform_driver:
static struct platform_driver infinity_wdt_driver = {
.probe = infinity_wdt_probe,
.remove = infinity_wdt_remove,
.shutdown = infinity_wdt_shutdown,
.driver = {
.owner = THIS_MODULE,
.name = "infinity-wdt",
.of_match_table = ms_watchdog_of_match_table,
},
};
module_platform_driver(infinity_wdt_driver);
In the infinity_wdt_probe it calls devm_kzalloc and devm_ioremap_resource:
static int infinity_wdt_probe(struct platform_device *pdev) {
...
wdt = devm_kzalloc(&pdev->dev, sizeof(*wdt), GFP_KERNEL);
...
wdt->reg_base = devm_ioremap_resource(&pdev->dev, res);
...
}
How shall I utilize this driver? Do I need to write some C code, or Linux provides some standard way through filesystem for controlling this?
In the filesystem there is a directory under /sys/bus/platform/drivers/infinity-wdt, but it contains only a few files:
# ls -l
total 0
lrwxrwxrwx 1 root root 0 Nov 16 20:23 1f006000.watchdog -> ../../../../devices/soc0/soc/1f006000.watchdog
--w------- 1 root root 4096 Nov 16 20:23 bind
--w------- 1 root root 4096 Nov 16 20:23 uevent
--w------- 1 root root 4096 Nov 16 20:23 unbind
Is it somehow possible to use the driver with this filesystem above?
Some extra info: 1f006000.watchdog is a symlink to a directory which contains these:
# ls -l
total 0
lrwxrwxrwx 1 root root 0 Nov 16 20:23 driver -> ../../../../bus/platform/drivers/infinity-wdt
-rw-r--r-- 1 root root 4096 Nov 16 20:23 driver_override
-r--r--r-- 1 root root 4096 Nov 16 20:23 modalias
lrwxrwxrwx 1 root root 0 Nov 16 20:23 of_node -> ../../../../firmware/devicetree/base/soc/watchdog
lrwxrwxrwx 1 root root 0 Nov 16 20:23 subsystem -> ../../../../bus/platform
-rw-r--r-- 1 root root 4096 Nov 16 20:23 uevent
This is the kernel log:
# cat /var/log/messages | grep -i watchdog
Jan 1 04:00:02 kernel: [WatchDog]infinity_wdt_probe
Jan 1 04:00:02 kernel: [WatchDog]infinity_wdt_set_heartbeat
Jan 1 04:00:04 kernel: [WatchDog]infinity_wdt_start
Jan 1 04:00:04 kernel: [WatchDog] infinity_wdt_ping tmr_margin=a ^M
Jan 1 04:00:04 kernel: watchdog: watchdog0: watchdog did not stop!
Jan 1 04:00:04 kernel: [WatchDog] infinity_wdt_ping tmr_margin=a ^M
Jan 1 04:00:04 kernel: [WatchDog]infinity_wdt_set_timeout=60
Jan 1 04:00:04 kernel: [WatchDog]infinity_wdt_set_timeout data=3c ^M
Jan 1 04:00:04 kernel: [WatchDog] infinity_wdt_ping tmr_margin=3c ^M
Jan 1 04:00:04 kernel: [WatchDog] infinity_wdt_ping tmr_margin=3c ^M
Nov 16 21:03:11 kernel: [WatchDog] infinity_wdt_ping tmr_margin=3c ^M
Nov 16 21:03:41 kernel: [WatchDog] infinity_wdt_ping tmr_margin=3c ^M
"Platform" driver means driver that doesn't fit into other standard subsystem (e.g. USB, I2C, etc). In this case it's a watchdog driver, which is apparently supposed to reboot embedded system if it's not responsive.
Entries in sysfs are standard bookkeeping entries, automatically created for any driver by kernel.
Since driver contains "of_match_table", it implies that it must be correctly specified in the device tree. Given it generally works and has no other explicit interfaces (e.g. procfs, sysfs), this should be enough to enable it.
You might also check if corresponding /dev/watchdog* is created by this driver. If so, standard userspace watchdog can be used by specified this /dev/watchdog* file in its config file.

SSH connect, execute a command and attach interactively or asynchronously standard streams (output, input, error) in Python

I would like to do a ssh connection to a remote host, execute a given command, and attach the standard streams (output, input, error) to the local process (sys.stdout, etc..) in an asynchronous/interactive way.
I can execute the first command and I can see the output, but if I try to run it again, there is no output because the streams are closed from the first execution.
See the example below:
import asyncio
import asyncssh
import sys
def run_command(command):
async def run():
async with asyncssh.connect(host='localhost') as ssh:
await ssh.run(
command=command,
stdout=sys.stdout,
stderr=sys.stderr,
stdin=sys.stdin
)
try:
asyncio.get_event_loop().run_until_complete(run())
except (OSError, asyncssh.Error) as exc:
sys.exit('SSH connection failed: ' + str(exc))
if __name__ == "__main__":
print(sys.stdout)
run_command("ls -la") # this one works fine
print(sys.stdout) # sys.stdout is already closed
run_command("ls -la") # this one won't print either
Output
$ python example.py
<_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>
total 296
drwxr-xr-x 40 ********** ********** 4096 may 4 12:43 .
drwxr-xr-x 3 root root 4096 ene 8 17:33 ..
drwx------ 3 ********** ********** 4096 ene 10 15:44 .foo
drwxrwxr-x 4 ********** ********** 4096 mar 31 15:48 .bar
drwxr-xr-x 2 ********** ********** 4096 ene 10 14:44 .bash_rc
-rw------- 1 ********** ********** 65912 may 4 12:43 .bash_history
-rw-r--r-- 1 ********** ********** 220 ene 8 17:33 .bash_logout
...
What would be the best approach to attach the standard streams in order to communicate to the process ?
Thanks in advance,

How to check if a locale is UTF-8?

I'm working with Yocto to create an embedded linux distribution for an ARM device (i.MX 6Quad Processors).
I've configured the list of desired locales with the variable:
IMAGE_LINGUAS = "de-de fr-fr en-gb en-gb.iso-8859-1 en-us en-us.iso-8859-1 zh-cn"
As result I've obtained a file systems that contains the following folders:
root#lam_icu:/usr/lib/locale# cd /usr/share/locale/
root#lam_icu:/usr/share/locale# ls -la
total 0
drwxr-xr-x 6 root root 416 Nov 17 2016 .
drwxr-xr-x 30 root root 2056 Nov 17 2016 ..
drwxr-xr-x 4 root root 296 Nov 17 2016 de
drwxr-xr-x 3 root root 232 Nov 17 2016 en_GB
drwxr-xr-x 4 root root 296 Nov 17 2016 fr
drwxr-xr-x 4 root root 296 Nov 17 2016 zh_CN
and:
root#lam_icu:/usr/share/locale# cd /usr/lib/locale/
root#lam_icu:/usr/lib/locale# ls -la
total 0
drwxr-xr-x 9 root root 640 Mar 13 2017 .
drwxr-xr-x 32 root root 40000 Mar 13 2017 ..
drwxr-xr-x 3 root root 1016 Mar 13 2017 de_DE
drwxr-xr-x 3 root root 1016 Mar 13 2017 en_GB
drwxr-xr-x 3 root root 1016 Mar 13 2017 en_GB.ISO-8859-1
drwxr-xr-x 3 root root 1016 Mar 13 2017 en_US
drwxr-xr-x 3 root root 1016 Mar 13 2017 en_US.ISO-8859-1
drwxr-xr-x 3 root root 1016 Mar 13 2017 fr_FR
drwxr-xr-x 3 root root 1016 Mar 13 2017 zh_CN
Which is the encoding of all non ISO-8859-1 locales? Can I assume that "en_GB" or "en_US" use the UTF-8 encoding?
I've tried to open the "LC_IDENTIFICATION" file, the result is:
Hc�������������cEnglish locale for the USAFree Software
Foundation,
Inc.http://www.gnu.org/software/libc/bug-glibc-locales#gnu.orgEnglishUSA1.02000-06-24en_US:2000en_US:2000en_US:2000en_US:2000en_US:2000en_US:2000en_US:2000en_US:2000en_US:2000en_US:2000en_US:2000en_US:2000UTF-8
At the end of the file there is something that recalls "UTF-8". Is this enough to assume that the encoding is UTF-8?
How to check if a locale is UTF-8?
LC_IDENTIFICATION doesn't tell you much:
LC_IDENTIFICATION - this is not a user-visible category, it contains information about the locale itself and is rarely useful for users or developers (but is listed here for completeness sake).
You'd have to look at the complete set of files.
There appears to be no standard command-line utility for doing this, but there is a runtime call (added a little later than the original locale functions). Here is a sample program which illustrates the function nl_langinfo:
#include <stdio.h>
#include <locale.h>
#include <langinfo.h>
int
main(int argc, char **argv)
{
int n;
for (n = 1; n < argc; ++n) {
if (setlocale(LC_ALL, argv[n]) != 0) {
char *code = nl_langinfo(CODESET);
if (code != 0)
printf("%s ->%s\n", argv[n], code);
else
printf("?%s (nl_langinfo)\n", argv[n]);
} else {
printf("? %s (setlocale)\n", argv[n]);
}
}
return 0;
}
and some output, e.g., by foo $(locale -a):
aa_DJ ->ISO-8859-1
aa_DJ.iso88591 ->ISO-8859-1
aa_DJ.utf8 ->UTF-8
aa_ER ->UTF-8
aa_ER#saaho ->UTF-8
aa_ER.utf8 ->UTF-8
aa_ER.utf8#saaho ->UTF-8
aa_ET ->UTF-8
aa_ET.utf8 ->UTF-8
af_ZA ->ISO-8859-1
af_ZA.iso88591 ->ISO-8859-1
af_ZA.utf8 ->UTF-8
am_ET ->UTF-8
am_ET.utf8 ->UTF-8
an_ES ->ISO-8859-15
an_ES.iso885915 ->ISO-8859-15
an_ES.utf8 ->UTF-8
ar_AE ->ISO-8859-6
ar_AE.iso88596 ->ISO-8859-6
ar_AE.utf8 ->UTF-8
ar_BH ->ISO-8859-6
ar_BH.iso88596 ->ISO-8859-6
The directory names you're referring to are often (but not required) to be the same as encoding names. That is the assumption made in the example program. There was a related question in How to get terminal's Character Encoding, but it has no useful answers. One is interesting though, since it asserts that
locale charmap
will give the locale encoding. According to the standard, that's not necessarily so:
The command locale charmap gives the name used in localedef -f
However, localedef attaches no special meaning to the name given in the -f option.
localedef has a different option -u which identifies the codeset, but locale (in the standard) mentions no method for displaying this information.
As usual, implementations may (or may not) treat unspecified features in different ways. The GNU C library's documentation differs in some respects from the standard (see locale and localedef), but offers no explicit options for showing the codeset name.

OCaml error: Unbound module Event

I try to build a short ocaml event example. But when I compile, the error in the title appears.
The question of: unbound module Event error when compiling Ocaml game was not helpful for me.
The system is Kubuntu 14.04 and I installed ocaml over aptitude, so installed packages are:
camlp4, ledit, libfindlib-ocaml, libfindlib-ocaml-dev, liboasis-ocaml, liboasis-ocaml-dev, libodn-ocaml, libodn-ocaml-dev, libtype-conv-camlp4-dev, oasis, ocaml, ocaml-base, ocaml-base-nox, ocaml-compiler-libs, ocaml-doc, ocaml-findlib, ocaml-interp, ocaml-native-compilers, ocaml-nox
The OCaml compiler is version 4.01.0
Here is my short test program.
open Thread;;
open Event;;
let chan = Event.new_channel();;
let a () =
Printf.printf "A waiting...\n";;
let sigRX = Event.receive chan in
Printf.printf "A received over channel\n";
let v = Event.sync sigRx in
Printf.printf "A running\n";
Printf.printf "A done!\n";;
let b () =
Thread.delay 0.8
Printf.printf "B sending...\n";;
let sigTX = Event.send "wake up" in
Event.sync sigTX;
Printf.printf "B done!\n";;
let t_a = Thread.create a ();;
let t_b = Thread.create b ();;
I tried to compile this single file (test.ml) with:
ocamlc -thread unix.cma threads.cma test.ml
The response is:
File "test.ml", line 2, characters 0-10:
Error: Unbound module Event
I googled, found some "thread-using-tips" like: http://caml.inria.fr/pub/docs/manual-ocaml/libthreads.html#c%3Athreads
In /usr/lib/ocaml is an threads folder and an thread.mli. Inside the threads folder there are this files:
-rw-r--r-- 1 root root 487 Jan 2 2014 condition.cmi
-rw-r--r-- 1 root root 487 Jan 2 2014 condition.cmx
-rw-r--r-- 1 root root 1203 Jan 2 2014 event.cmi
-rw-r--r-- 1 root root 1867 Jan 2 2014 event.cmx
-rw-r--r-- 1 root root 421 Jan 2 2014 mutex.cmi
-rw-r--r-- 1 root root 407 Jan 2 2014 mutex.cmx
-rw-r--r-- 1 root root 1859 Jan 2 2014 thread.cmi
-rw-r--r-- 1 root root 1308 Jan 2 2014 thread.cmx
-rw-r--r-- 1 root root 62778 Jan 2 2014 threads.a
-rw-r--r-- 1 root root 47047 Jan 2 2014 threads.cma
-rw-r--r-- 1 root root 1258 Jan 2 2014 threads.cmxa
-rw-r--r-- 1 root root 4145 Jan 2 2014 threadUnix.cmi
-rw-r--r-- 1 root root 1515 Jan 2 2014 threadUnix.cmx
What am I missing? I assume, that the Event is packed in Thread Module?
This command line works for me to get past the unbound module problem.
$ ocamlc -I +threads -c test.ml
There are errors in your code, but I imagine you'll know how to fix them.
This full command line will probably work, but I can't be sure because of the errors:
$ ocamlc -thread -I +threads unix.cma threads.cma test.ml
(There are some higher-level tools for building OCaml programs that you might want to learn about at some point.)

gammu-smsd RunOnReceive script results exit status 2

I want to forward an SMS using gammu-smsd RunOnReceive.
That is the script I want to run (/var/spool/gammu/forward.sh) and it goes perfectly if I run it from a sudoer or using sudo -u gammu -g gammu /var/spool/gammu/forward.sh
#!/bin/bash
SMS_MESSAGES=1
for i in `seq $SMS_MESSAGES`
do
number="SMS_${i}_NUMBER"
text="SMS_${i}_TEXT"
eval "gammu-smsd-inject TEXT my_number_goes_here -text \"${!number}: ${!text}\""
done
And here is the problem I am experiencing:
Thu 2015/01/29 23:08:57 gammu-smsd[2549]: Starting run on receive: /var/spool/gammu/forward.sh IN20150130_000850_00_+37368214400_00.txt
Thu 2015/01/29 23:08:57 gammu-smsd[2154]: Process failed with exit status 2
Output of ls -l /etc/gammu-smsdrc /var/spool/gammu/ /usr/bin/gammu-smsd*:
-rw-r--r-- 1 root root 457 Jan 29 22:44 /etc/gammu-smsdrc
-rwxrwxrwx 1 root root 14336 Jun 10 2012 /usr/bin/gammu-smsd
-rwxrwxrwx 1 root root 51164 Jun 10 2012 /usr/bin/gammu-smsd-inject
-rwxrwxrwx 1 root root 9972 Jun 10 2012 /usr/bin/gammu-smsd-monitor
/var/spool/gammu/:
total 24
drwxrwxrwx 2 gammu gammu 4096 Jan 28 16:02 error
-rwxrwxrwx 1 gammu gammu 189 Jan 29 22:13 forward.sh
drwxrwxrwx 2 gammu gammu 4096 Jan 29 23:08 inbox
-rw-rw-r-- 1 gammu gammu 3702 Jan 29 23:08 log
drwxrwxrwx 2 gammu gammu 4096 Jan 29 23:07 outbox
drwxrwxrwx 2 gammu gammu 4096 Jan 29 23:07 sent
What happens if I just do ./forward.sh (not root) - so all is OK:
gammu-smsd-inject[2606]: Created outbox message OUTC20150029_231213_00_my_number_here_sms0.txt
Written message with ID /var/spool/gammu/outbox/OUTC20150029_231213_00_my_number_here_sms0.txt
Here is my /etc/gammu-smsdrc
# Configuration file for Gammu SMS Daemon
[gammu]
port = /dev/ttyUSB0
connection = at
[smsd]
service = files
logfile = /var/spool/gammu/log
debuglevel = 2
commtimeout = 1
sendtimeout = 15
statusfrequency = 0
outboxformat = unicode
transmitformat = unicode
RunOnReceive = /var/spool/gammu/forward.sh
inboxpath = /var/spool/gammu/inbox/
outboxpath = /var/spool/gammu/outbox/
sentsmspath = /var/spool/gammu/sent/
errorsmspath = /var/spool/gammu/error/
ps -fe | grep gammu:
gammu 2154 1 0 23:05 ? 00:00:02 /usr/bin/gammu-smsd --daemon --user gammu --pid /var/run/gammu-smsd.pid
cubie 2644 2403 0 23:20 pts/0 00:00:00 grep gammu
Please, help
I had the same problem and I solved it this way:
First add gammu user to sudoers, with no password:
type: $ sudo visudo
and add: gammu ALL=(ALL) NOPASSWD: ALL
Then run gammu-smsd as root user:
in /etc/init.d/gammu-smsd
change USER=gammu to USER=root
save it and don't forget to restart daemon: service gammu-smsd restart
In RunOnReceive script add sudo in front of gammu-smsd-inject:
e.g.: sudo gammu-smsd-inject TEXT my_tel_num -text "Hello world!"
I hope this will work for you too!
P.S.: I use Gammu version 1.31.90.

Resources