[prev in list] [next in list] [prev in thread] [next in thread] 

List:       openbsd-tech
Subject:    convert usbhidaction(1) from system(3) to fork+exec
From:       Vadim Zhukov <persgray () gmail ! com>
Date:       2010-03-27 8:08:41
Message-ID: 201003271108.41852.persgray () gmail ! com
[Download RAW message or body]

Hello all.

Today I found that when I start some program asynchronously ("prog &") 
from usbhidaction(1) and then usbhidaction(1) terminates, then "prog" 
terminates too. This is caused by the fact usbhidaction(1) uses 
system(3). I changed the logic to do fork+setpgid+exec dance, and now 
things work as expected. The patch is at the end of letter.

-- 
  Best wishes,
    Vadim Zhukov

A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?
A: Top-posting.
Q: What is the most annoying thing in e-mail?


Index: usbhidaction.c
===================================================================
RCS file: /cvs/src/usr.bin/usbhidaction/usbhidaction.c,v
retrieving revision 1.12
diff -u -p -r1.12 usbhidaction.c
--- usbhidaction.c	21 Oct 2009 15:48:27 -0000	1.12
+++ usbhidaction.c	27 Mar 2010 08:04:37 -0000
@@ -46,6 +46,7 @@
 #include <util.h>
 #include <syslog.h>
 #include <signal.h>
+#include <paths.h>
 
 int verbose = 0;
 int isdemon = 0;
@@ -399,6 +400,7 @@ docmd(struct command *cmd, int value, co
 	char cmdbuf[SIZE], *p, *q;
 	size_t len;
 	int n, r;
+	pid_t pid;
 
 	for (p = cmd->action, q = cmdbuf; *p && q < &cmdbuf[SIZE-1]; ) {
 		if (*p == '$') {
@@ -431,11 +433,16 @@ docmd(struct command *cmd, int value, co
 	}
 	*q = 0;
 
-	if (verbose)
-		printf("system '%s'\n", cmdbuf);
-	r = system(cmdbuf);
-	if (verbose > 1 && r)
-		printf("return code = 0x%x\n", r);
+	pid = fork();
+	if (pid == -1)
+		warn("fork failed");
+	else if (pid == 0) {
+		setpgid(0, 0);
+		if (verbose)
+			printf("executing '%s'\n", cmdbuf);
+		r = execl(_PATH_BSHELL, "sh", "-c", cmdbuf, NULL);
+		err(1, "execl");
+	}
 }
 
 void

[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic