[Bluez-devel] [PATCH] pand/main.c, restore signals for dev-up script

James Cameron
2004-06-27T13:33:25Z

[-- Attachment #1.1: Type: text/plain, Size: 2363 bytes --]

Problem: pand dev-up script on Debian fails to "ifup bnep0", the error
is "Failed to bring up bnep0".

Diagnosis: dev-up script inherits the SIG_IGN of SIGCHLD and SIGPIPE
done by pand/main.c, in addition to some open sockets. The ifup program
requires signals in default state.

Solution: restore signals to SIG_DFL, close some of the sockets. A
patch for this is attached, less comments. The patch applies cleanly to
both the Debian revision of pand/main.c 1.3, and to the current revision
1.4 in CVS.

Discussion:

Restoring the signals takes a call to sigaction() after fork() but
before exec(). This call undoes the initialisation call in main().

Closing the sockets can be done automatically on fork(), or manually.
My patch does it manually.

Closing the sockets automatically would require setting FD_CLOEXEC on
them after they are created. fcntl(sk, F_SETFD, FD_CLOEXEC);

Closing the sockets manually requires the socket file descriptors to be
sent to run_devup(); this seems ugly to me, I'm not altogether happy
with the way I've done it and I hope the maintainer will rethink this
for me. Perhaps making the socket numbers global would make more sense.

Even then, tests show that there are *three* file descriptors inherited
by the dev-up script, and I've only been able to find two. Hopefully
the maintainer knows where the other one is; bnep_init()?

Here's how I found the open sockets ... (a) place a "sleep 30m" in
dev-up, (b) cause pand to run dev-up, (c) find pid of sleep, (d) run
lsof -p ...

sleep 16393 root 0u CHR 1,3 163834 /dev/null
sleep 16393 root 1w REG 3,4 258 48965 /tmp/dev-up.16391.log
sleep 16393 root 2w REG 3,4 258 48965 /tmp/dev-up.16391.log
sleep 16393 root 3u sock 0,4 4477 can't identify protocol
sleep 16393 root 5u sock 0,4 4480 can't identify protocol
sleep 16393 root 6u sock 0,4 27277 can't identify protocol

The signals were found using "cat /proc/${pid}/status", and confirmed
with strace.

Please let me know if my patch is applied. Thanks!

--
James Cameron http://quozl.netrek.org/
HP Open Source, Volunteer http://opensource.hp.com/
PPTP Client Project, Release Engineer http://pptpclient.sourceforge.net/

[-- Attachment #1.2: pand-256436.patch --]
[-- Type: text/plain, Size: 1132 bytes --]

--- bluez-utils-2.7/pand/main.c 2004-05-09 20:39:43.000000000 +1000
+++ bluez-utils-2.7-4-jc1/pand/main.c 2004-06-27 23:02:00.000000000 +1000
@@ -84,9 +84,10 @@
KILL
} modes;

-static void run_devup(char *dev, char *dst)
+static void run_devup(char *dev, char *dst, int sk, int nsk)
{
char *argv[4], prog[40];
+ struct sigaction sa;

sprintf(prog, "%s/%s", PAND_CONFIG_DIR, PAND_DEVUP_CMD);

@@ -96,6 +97,15 @@
if (fork())
return;

+ close(sk);
+ if (nsk != -1)
+ close(nsk);
+
+ memset(&sa, 0, sizeof(sa));
+ sa.sa_handler = SIG_DFL;
+ sigaction(SIGCHLD, &sa, NULL);
+ sigaction(SIGPIPE, &sa, NULL);
+
argv[0] = prog;
argv[1] = dev;
argv[2] = dst;
@@ -184,7 +194,7 @@

syslog(LOG_INFO, "New connection from %s %s", str, netdev);

- run_devup(netdev, str);
+ run_devup(netdev, str, sk, nsk);
} else {
syslog(LOG_ERR, "Connection failed. %s(%d)",
strerror(errno), errno);
@@ -274,7 +284,7 @@

syslog(LOG_INFO, "%s connected", netdev);

- run_devup(netdev, dst);
+ run_devup(netdev, dst, sk, -1);

if (persist)
w4_hup(sk);

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 189 bytes --]