Wait for existence of wayland socket before changing its permissions.
[profile/ivi/weston.git] / packaging / rc.weston
1 #! /bin/sh
2 ### BEGIN INIT INFO
3 # Provides:          weston
4 # Required-Start:    $remote_fs $syslog
5 # Required-Stop:     $remote_fs $syslog
6 # Default-Start:     2 3 4 5
7 # Default-Stop:      0 1 6
8 # Short-Description: Weston compositor boot script
9 # Description:       This script starts the Weston compositor at boot.
10 ### END INIT INFO
11
12 # Author: Ossama Othman
13 #
14 # Please remove the "Author" lines above and replace them
15 # with your own name if you copy and modify this script.
16
17 # Do NOT "set -e"
18
19
20 backend=
21
22 # Use the Weston DRM backend if $DISPLAY is set and X isn't running
23 #since Weston won't be able use the X11 backend.
24 #
25 # This allows this script to start Weston with either the X11 or DRM
26 #backends.
27 if [ -z `pidof Xorg` ] && [ -n "$DISPLAY" ]; then
28     backend="--backend=drm-backend.so"
29 fi
30
31 # PATH should only include /usr/* if it runs after the mountnfs.sh script
32 PATH=/sbin:/usr/sbin:/bin:/usr/bin
33 DESC="Weston compositor"
34 NAME=weston
35 DAEMON=/usr/bin/weston
36 DAEMON_ARGS="$backend --log=/var/log/weston.log"
37 PIDFILE=/var/run/$NAME.pid
38 SCRIPTNAME=/etc/init.d/$NAME
39
40 # Exit if the package is not installed
41 [ -x "$DAEMON" ] || exit 0
42
43 # Read configuration variable file if it is present
44 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
45
46 # Load the VERBOSE setting and other rcS variables
47 . /lib/init/vars.sh
48
49 # Define LSB log_* functions.
50 # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
51 # and status_of_proc is working.
52 . /lib/lsb/init-functions
53
54 #
55 # Function that starts the daemon/service
56 #
57 do_start()
58 {
59         # Make sure we have a sane XDG_RUNTIME_DIR environment
60         # variable set.
61         if [ -z "${XDG_RUNTIME_DIR}" ]; then
62             . /etc/profile.d/weston.sh
63         fi
64
65         # Return
66         #   0 if daemon has been started
67         #   1 if daemon was already running
68         #   2 if daemon could not be started
69         start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
70                 || return 1
71         # start-stop-daemon --start --background --quiet --pidfile $PIDFILE --exec $DAEMON -- \
72         #       $DAEMON_ARGS \
73         #       || return 2
74         $DAEMON $DAEMON_ARGS &
75         weston_pid=$!
76
77         if [ $weston_pid = 0 ]; then
78             return 2
79         else
80             echo $weston_pid > $PIDFILE
81         fi
82
83         # Add code here, if necessary, that waits for the process to be ready
84         # to handle requests from services started subsequently which depend
85         # on this one.  As a last resort, sleep for some time.
86
87         # ************************************************************
88         # HACK TO WORK AROUND INSUFFICIENT ACCESS PERMISSIONS FOR
89         # UNPRIVILEGED USERS.
90         #
91         # Ideally we should launch weston with weston-launch with the
92         # appropriate --user flag.  Unfortunately, weston-launch isn't
93         # available due to missing package dependencies.
94         # ************************************************************
95         wayland_socket="$XDG_RUNTIME_DIR/wayland-0"
96         MAX_ATTEMPTS=8
97         attempt=0
98         while [ ! -f $wayland_socket ] && [ $attempt -lt $MAX_ATTEMPTS ]
99         do
100                 sleep 0.5
101                 attempt=$[$attempt + 1]
102         done
103         chmod 777 $wayland_socket
104
105         # ***********************************************************
106         # HACK TO WORK AROUND BROKEN INFINITE LOOPS IN BOOT ANIMATION
107         # VIRTUAL KEYBOARD BOOT SCRIPTS.
108         # ***********************************************************
109         touch /tmp/.X0-lock
110         touch /tmp/.wm_ready
111         # ***********************************************************
112 }
113
114 #
115 # Function that stops the daemon/service
116 #
117 do_stop()
118 {
119         # Return
120         #   0 if daemon has been stopped
121         #   1 if daemon was already stopped
122         #   2 if daemon could not be stopped
123         #   other if a failure occurred
124         #start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME
125         start-stop-daemon --stop --quiet --pidfile $PIDFILE --name $NAME
126         RETVAL="$?"
127         [ "$RETVAL" = 2 ] && return 2
128         # Wait for children to finish too if this is a daemon that forks
129         # and if the daemon is only ever run from this initscript.
130         # If the above conditions are not satisfied then add some other code
131         # that waits for the process to drop all resources that could be
132         # needed by services started subsequently.  A last resort is to
133         # sleep for some time.
134         #start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
135         start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
136         [ "$?" = 2 ] && return 2
137         # Many daemons don't delete their pidfiles when they exit.
138         rm -f $PIDFILE
139         return "$RETVAL"
140 }
141
142 #
143 # Function that sends a SIGHUP to the daemon/service
144 #
145 do_reload() {
146         #
147         # If the daemon can reload its configuration without
148         # restarting (for example, when it is sent a SIGHUP),
149         # then implement that here.
150         #
151         start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
152         return 0
153 }
154
155 case "$1" in
156   start)
157         [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
158         do_start
159         case "$?" in
160                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
161                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
162         esac
163         ;;
164   stop)
165         [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
166         do_stop
167         case "$?" in
168                 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
169                 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
170         esac
171         ;;
172   status)
173        status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
174        ;;
175   #reload|force-reload)
176         #
177         # If do_reload() is not implemented then leave this commented out
178         # and leave 'force-reload' as an alias for 'restart'.
179         #
180         #log_daemon_msg "Reloading $DESC" "$NAME"
181         #do_reload
182         #log_end_msg $?
183         #;;
184   restart|force-reload)
185         #
186         # If the "reload" option is implemented then remove the
187         # 'force-reload' alias
188         #
189         log_daemon_msg "Restarting $DESC" "$NAME"
190         do_stop
191         case "$?" in
192           0|1)
193                 do_start
194                 case "$?" in
195                         0) log_end_msg 0 ;;
196                         1) log_end_msg 1 ;; # Old process is still running
197                         *) log_end_msg 1 ;; # Failed to start
198                 esac
199                 ;;
200           *)
201                 # Failed to stop
202                 log_end_msg 1
203                 ;;
204         esac
205         ;;
206   *)
207         #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
208         echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
209         exit 3
210         ;;
211 esac
212
213 :