Debian Codegraph @

scripts/sysvinit/bumblebeed.in

raw
1 #!/bin/sh
2 ### BEGIN INIT INFO
3 # Provides: bumblebeed
4 # Required-Start: $localfs $syslog nvidia-kernel
5 # Required-Stop: $localfs $syslog nvidia-kernel
6 # Should-Start: kdm gdm
7 # Should-Stop: kdm gdm
8 # Default-Start: 2 3 4 5
9 # Default-Stop: 0 1 6
10 # Short-Description: Bumblebee supporting nVidia Optimus cards
11 # Description: Daemon responsible for handling power management for
12 # nVidia Optimus cards and handling optirun requests which
13 # allows the discrete card to be used
14 ### END INIT INFO
15
16 # Bumblebee daemon handler script. Distro-independent script to start/stop
17 # daemon. Should be runnable in any distro but won't give any feedback.
18
19 # Avoid bashism since this script runs using /bin/sh, not /bin/bash!
20
21 NAME=bumblebeed
22 BIN='@SBINDIR@/bumblebeed'
23 PIDFILE=/var/run/$NAME.pid
24
25 # returns 0 if running, non-zero otherwise
26 status() {
27 local pid
28 # program is not running
29 [ -s "$PIDFILE" ] || return 3
30
31 pid="$(cat "$PIDFILE" 2>/dev/null)"
32
33 # process is not running, pid file not properly cleared
34 kill -0 "$pid" || return 1
35
36 # process is running
37 return 0
38 }
39
40 start() {
41 # program is not installed
42 [ -x "$BIN" ] || return 5
43
44 # return if already started
45 status && return 0 || true
46 "$BIN" --daemon
47 status && return 0 || return 1
48 }
49
50 stop() {
51 local retries=10
52 local pid="$(cat "$PIDFILE" 2>/dev/null)"
53
54 # not running, we're done
55 status || return 0
56
57 # first ask the daemon nicely to quit
58 kill "$pid" || true
59 # and check whether it listened
60 while [ $retries -gt 0 ]; do
61 retries=$((retries - 1))
62
63 # process has gone, return success
64 status || return 0
65 # wait for half a minut before polling again
66 sleep .5
67 done
68 # failed to stop or timeout
69 return 1
70 }
71
72 restart() {
73 stop && start
74 }
75
76 case "$1" in
77 start)
78 start
79 ;;
80 stop)
81 stop
82 ;;
83 restart)
84 restart
85 ;;
86 status)
87 status
88 ;;
89 *)
90 echo "Usage: $0 {start|stop|restart|status}"
91 exit 1
92 ;;
93 esac