-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmustang_bridge_start
executable file
·84 lines (65 loc) · 2.15 KB
/
mustang_bridge_start
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
77
78
79
80
81
82
83
84
#!/usr/bin/python
import os
import sys
from os.path import expanduser
from pwd import getpwnam
from grp import getgrnam
import glob
import syslog
from string import split
import usb.core
rundir = "/var/run/mustang/"
# Mustang USB parms
mustang_vid = 0x1ed8
mustang_pids = ( 0x0004, 0x0005, 0x000a, 0x0010, 0x0012, 0x0014, 0x0016 )
####### Start User Edits #########
# Controller USB parms
control_vid = 0x0763
control_pid = 0x0160
# Controller MIDI device
midi_device = 1
# MIDI listen channel
midi_channel = 1
######## End User Edits ##########
# Look for devices
controller = usb.core.find( idVendor = control_vid, idProduct = control_pid )
if ( not controller ):
sys.exit( 0 )
mustang = False
for pid in mustang_pids:
device = usb.core.find( idVendor = mustang_vid, idProduct = pid )
if ( device ):
mustang = True
break
if ( not mustang ):
sys.exit( 0 )
pid = os.getpid()
# syslog.syslog( "%d: Starting" % pid )
# Look for atomic pid file
os.chdir( rundir )
filelist = glob.glob( 'mustang_*' )
if len( filelist ) != 1:
syslog.syslog( "%d: /var/run/mustang is not properly setup" % pid )
sys.exit( 0 )
# Found file. Parse out the PID of the process that created it.
lockfile = filelist[0]
oldpid = split( lockfile, '_' )[1]
# syslog.syslog( "%d: Check for path /proc/%s" % (pid, oldpid) )
if not os.path.exists( "/proc/%s" % oldpid ):
# No such process, ok to start ours
# syslog.syslog( "%d: Renaming %s to mustang_%d" % (pid, lockfile, pid) )
try:
os.rename( lockfile, "mustang_%d" % os.getpid() )
except Exception, e:
pass
# syslog.syslog( "%d: Unable to rename file" % pid )
else:
# syslog.syslog( "%d: About to exec" % pid )
# Drop privileges and start the program
pwObj = getpwnam('mustang-user')
os.setgid( pwObj.pw_gid )
# Need secondary groups to access MIDI and USB devices
sec_gids = ( getgrnam('plugdev').gr_gid, getgrnam('audio').gr_gid )
os.setgroups( sec_gids )
os.setuid( pwObj.pw_uid )
os.execl( "/usr/local/bin/mustang_midi", "mustang_midi", "%s" % midi_device, "%s" % midi_channel )