File: 23_hotplug_racecond.dpatch

package info (click to toggle)
gpsd 2.33-4etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 2,904 kB
  • ctags: 1,861
  • sloc: ansic: 14,508; sh: 9,172; xml: 2,657; python: 1,446; makefile: 334; cpp: 120; perl: 22
file content (76 lines) | stat: -rw-r--r-- 3,079 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#! /bin/sh /usr/share/dpatch/dpatch-run
## 23_hotplug_racecond.dpatch by Tilman Koschnick <til@subnetz.org>
##
## All lines beginning with `## DP:' are a description of the patch.
## DP: Fix race condition in hotplug script.
## DP: Patch by Robin Johnson, taken from upstream svn revision 3378.

@DPATCH@
diff -urNad gpsd~/gpsd.c gpsd/gpsd.c
--- gpsd~/gpsd.c	2006-06-09 17:59:42.000000000 +0200
+++ gpsd/gpsd.c	2006-10-07 20:27:18.079634794 +0200
@@ -1048,9 +1048,10 @@
 	    (void)write(sfd, "ERROR\n", 6);
     } else if (buf[0] == '+') {
 	p = snarfline(buf+1, &stash);
-	if (find_device(stash))
+	if (find_device(stash)) {
 	    gpsd_report(1,"<= control(%d): %s already active \n", sfd, stash);
-	else {
+		(void)write(sfd, "ERROR\n", 6);
+	} else {
 	    gpsd_report(1,"<= control(%d): adding %s \n", sfd, stash);
 	    if (open_device(stash))
 		(void)write(sfd, "OK\n", 3);
diff -urNad gpsd~/gpsd.hotplug gpsd/gpsd.hotplug
--- gpsd~/gpsd.hotplug	2006-10-07 20:27:13.635718194 +0200
+++ gpsd/gpsd.hotplug	2006-10-07 20:27:18.079634794 +0200
@@ -56,7 +56,7 @@
     return action
 
 def hotplug(action, devpath):
-    #syslog.syslog("ACTION=%s" % action)
+    #syslog.syslog("ACTION=%s DEVPATH=%s" % (action,devpath))
     if not devpath:
         syslog.syslog("No device")
     else:
@@ -88,16 +88,27 @@
             return
 
 if __name__ == '__main__':
-    syslog.openlog('gpsd.hotplug', 0, syslog.LOG_DAEMON)
-    try: 
-        if len(sys.argv) == 1:	# Called as hotplug script
-            hotplug(os.getenv("ACTION"), os.getenv("DEVPATH"))
-        else:			# Called by hand for testing
-            gpsd_control(sys.argv[1], sys.argv[2])
-    except:
-        (exc_type, exc_value, exc_traceback) = sys.exc_info()
-        syslog.syslog("gpsd.hotplug: exception %s yields %s" % (exc_type, exc_value))
-        raise exc_type, exc_value, exc_traceback
-    #syslog.syslog("gpsd.hotplug ends")
-    syslog.closelog()
+    # In recent versions of udev, the gpsd script runs in series with
+    # the task that creates the real /dev/ttyUSB0 device
+    # node. Unfortunately, the gpsd script runs BEFORE the creation of
+    # the node, and the node is not created until after you kill the
+    # gpsd script, because the gpsd script waits forever for the node
+    # to appear.
+    # 
+    # This is a race condition, and is best fixed by running the
+    # actual wait/hotplug portion in the background.
+    pid = os.fork()
+    if not pid:
+        syslog.openlog('gpsd.hotplug', 0, syslog.LOG_DAEMON)
+        try: 
+            if len(sys.argv) == 1:	# Called as hotplug script
+                hotplug(os.getenv("ACTION"), os.getenv("DEVPATH"))
+            else:			# Called by hand for testing
+                gpsd_control(sys.argv[1], sys.argv[2])
+        except:
+            (exc_type, exc_value, exc_traceback) = sys.exc_info()
+            syslog.syslog("gpsd.hotplug: exception %s yields %s" % (exc_type, exc_value))
+            raise exc_type, exc_value, exc_traceback
+        #syslog.syslog("gpsd.hotplug ends")
+        syslog.closelog()