Project

General

Profile

[OT] Bluez btmgmt output is discarded by CGI

Added by taydin 16 days ago

I have a quite simple test setup with the following CGI script:

#!/bin/bash

printf "Content-type: text/plain\r\n\r\n" 

btmgmt info

But when I open this script from my browser, I am not seeing the output of the program. When I remove btmgmt and use "echo", I am seeing the output. I know for a fact that btmgmt is generating output, because when I run this program on a terminal, I am seeing the expected output, which is this:

root@rs1-F8DC7A0F32D4:~# btmgmt info
Index list with 1 item
hci0:   Primary controller
        addr F8:DC:7A:0F:32:D5 version 7 manufacturer 15 class 0x000414
        supported settings: powered connectable fast-connectable discoverable bondable link-security ssp br/edr hs le advertising secure-conn debug-keys privacy static-addr
        current settings: powered connectable discoverable bondable ssp br/edr le secure-conn
        name DS-Z31W
        short name

root@rs1-F8DC7A0F32D4:~#

I have also attached the lighttpd configuration file. The version of lighttpd is 1.4.51 and this is on an embedded system. In general, any program that uses printf to generate output, works fine inside the CGI. But btmgmt is using the readline library and also glib mainloop. I don't understand all of the code in btmgmt, but there are callbacks and gio objects and any output that is generated in that callback isn't working in the CGI.

But as I said, when I run btmgmt from a terminal, I can see the output. AND, when I run btmgmt using strace, I can confirm that the output is written to standard output (fileno = 1). So I'm at a loss about why btmgmt output is discarded in the CGI. I'm hoping somebody can point me in the right direction.

lighttpd.conf (4.24 KB) lighttpd.conf lighttpd configuration

Replies (9)

RE: Bluez btmgmt output is discarded by CGI - Added by gstrauss 16 days ago

I think you might want to start with a "How to" on writing CGI. Your script should include something like printf "Status: 200\r\n" before printf "Content-type: text/plain\r\n\r\n"

The Common Gateway Interface (CGI) Version 1.1
https://datatracker.ietf.org/doc/html/rfc3875

RE: Bluez btmgmt output is discarded by CGI - Added by gstrauss 16 days ago

You might want to include btmgmt --timeout 10 info in the command.
You can test btmgmt --timeout 10 info </dev/null 2>/dev/null to redirect stdin and stderr to /dev/null
If your system has /usr/bin/timeout, you might test with /usr/bin/timeout 10 btmgmt --timeout 10 info </dev/null 2>/dev/null

stdin and stdout of the CGI are pipes. stderr is a file IFF you specify server.breakagelog in your lighttpd.conf, or else it is the same as lighttpd stderr.

If you're expecting a streaming response, see server.stream-response-body

RE: Bluez btmgmt output is discarded by CGI - Added by taydin 16 days ago

gstrauss wrote in RE: Bluez btmgmt output is discarded by CGI:

You might want to include btmgmt --timeout 10 info in the command.
You can test btmgmt --timeout 10 info </dev/null 2>/dev/null to redirect stdin and stderr to /dev/null
If your system has /usr/bin/timeout, you might test with /usr/bin/timeout 10 btmgmt --timeout 10 info </dev/null 2>/dev/null

stdin and stdout of the CGI are pipes. stderr is a file IFF you specify server.breakagelog in your lighttpd.conf, or else it is the same as lighttpd stderr.

If you're expecting a streaming response, see server.stream-response-body

I have added the --timeout 10 option to btmgmt and this didn't make any difference. Just to be sure that the text content isn't causing anything funny, I have written the output of the btmgmt info command into a text file and then used "cat" to output the contents, and this works fine, so this isn't related to the text content itself.

I didn't understand why I should be testing with stdout routed to /dev/null though. In case of the web server, stdout will be going to the web server and I should be seeing it in the browser, no?

RE: Bluez btmgmt output is discarded by CGI - Added by taydin 16 days ago

This has gotta be related to how btmgmt writes things to stdout. When I use "btmgmt help", I can see the output. In this case, btmgmt is using printf equivalent to write to the terminal. If I use "btmgmt info", then btmgmt initiated a transaction with the bluez daemon and a callback is called when the response has been received. Anything written to the terminal in this callback isn't displayed in the CGI. But it is displayed when run from a terminal. So weird ...

RE: Bluez btmgmt output is discarded by CGI - Added by gstrauss 16 days ago

Is btmgmt writing to stderr? btmgmt --timeout 10 info 2>&1

RE: Bluez btmgmt output is discarded by CGI - Added by gstrauss 16 days ago

From the command line, /usr/bin/btmgmt info </dev/null produces no output, whereas /usr/bin/btmgmt info produces output (without </dev/null)

This is not an issue with lighttpd. Perhaps you should be using a different tools, such as btmon? I do not know. However, I do know that this isn't the place to get help with using btmgmt or btmon. Sorry.

RE: Bluez btmgmt output is discarded by CGI - Added by taydin 16 days ago

gstrauss wrote in RE: Bluez btmgmt output is discarded by CGI:

Is btmgmt writing to stderr? btmgmt --timeout 10 info 2>&1

Based on strace output, btmgmt is writing to stdout:

write(1, "Index list with 0 items\n", 24Index list with 0 items
) = 24
epoll_ctl(3, EPOLL_CTL_MOD, 4, {EPOLLIN|EPOLLOUT, {u32=347506464, u64=93948462139168}}) = 0
epoll_ctl(3, EPOLL_CTL_DEL, 0, NULL)    = 0
epoll_ctl(3, EPOLL_CTL_DEL, 4, NULL)    = 0
epoll_ctl(3, EPOLL_CTL_DEL, 5, NULL)    = 0
close(5)                                = 0
close(3)                                = 0
close(4)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++
[ta@bonsai ~]$

RE: Bluez btmgmt output is discarded by CGI - Added by taydin 16 days ago

taydin wrote in RE: Bluez btmgmt output is discarded by CGI:

gstrauss wrote in RE: Bluez btmgmt output is discarded by CGI:

Is btmgmt writing to stderr? btmgmt --timeout 10 info 2>&1

Based on strace output, btmgmt is writing to stdout:

[...]

OK, I see. So the web server is connecting btmgmt's stdin to /dev/null and that seems to prevent it from outputting anything when NOT using printf or equivalent.

RE: [OT] Bluez btmgmt output is discarded by CGI - Added by gstrauss 16 days ago

No, the lighttpd mod_cgi runs CGI programs with stdin and stdout connected to pipes. man pipe
However, my example was that redirecting stdin to /dev/null changes btmgmt behavior, which is independent of lighttpd.

    (1-9/9)