This file is indexed.

/usr/sbin/cereal-admin is in cereal 0.24-1.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  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
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/bin/sh

# cereal-admin: manage cereal sessions (runit service directory structure
# and runit run directory).
#
# The cereal scripts were written by
# Jameson Graef Rollins <jrollins@finestructure.net>
# and
# Daniel Kahn Gillmor <dkg@fifthhorseman.net>.
#
# They are Copyright 2007, and are all released under the GPL, version 3
# or later.

##################################################
SHAREDIR=${SHAREDIR:-"/usr/share/cereal"}
export SHAREDIR
. "$SHAREDIR/common"
[ -r "$ETC/cereal-admin.conf" ] && . "$ETC/cereal-admin.conf"
##################################################

usage() {
cat <<EOF
Usage: cereal-admin <subcommand> [options] [args]
Cereal session management program.

subcommands:
  create (c) SESSION TTY BAUD USER LOGGROUP    create cereal session
  start (s) [options] SESSION [SESSION]...     start cereal session(s)
    -a (--all)                                   start all sessions
  restart (r) [options] SESSION [SESSION]...   restart cereal session(s)
    -a (--all)                                   restart all sessions
    -r (--running)                               restart only running sessions
  stop (k) [options] SESSION [SESSION]...      stop cereal session(s)
    -a (--all)                                   stop all sessions
  destroy (d) [options] SESSION [SESSION]...   destroy cereal session(s)
    -a (--all)                                   destroy all sessions
  list (l) [options] [SESSION]...              list session(s)
    -n (--names)                                 list just session names
  help (h,?)                                   this help

EOF
}

# create session
create() {
    if [ $# -lt 5 ] ; then
	failure "Not enough input arguments.
Type 'cereal-admin help' for more info."
    fi

    SESSION="$1"
    TTY="$2"
    BAUD="$3"
    SUSER="$4"
    SGROUP=$(stat --dereference --printf="%G" "$TTY")
    LOGUSER='cereal'
    LOGGROUP="$5"

    is_session "$SESSION" && failure "A session named '$SESSION' already exists."
    check_tty "$TTY"
    check_session_tty "$TTY"
    check_user "$SUSER"
    check_group "$SGROUP"
    check_user "$LOGUSER"
    check_group "$LOGGROUP"
    check_tty_rw "$SUSER" "$SGROUP" "$TTY"

    # create service directory
    mkdir -p "$SESSIONDIR/$SESSION"
    ln -s "$SHAREDIR/mainrun" "$SESSIONDIR/$SESSION/run"
    ln -s "$SHAREDIR/finish" "$SESSIONDIR/$SESSION/finish"
    touch "$SESSIONDIR/$SESSION/down"

    # store environment variables
    mkdir -p "$SESSIONDIR/$SESSION/env"
    echo "$SESSION" > "$SESSIONDIR/$SESSION/env/SESSION"
    echo "$TTY" > "$SESSIONDIR/$SESSION/env/TTY"
    echo "$BAUD" > "$SESSIONDIR/$SESSION/env/BAUD" 
    echo "$SUSER" > "$SESSIONDIR/$SESSION/env/USER"
    echo "$SGROUP" > "$SESSIONDIR/$SESSION/env/GROUP"
    echo "$LOGUSER" > "$SESSIONDIR/$SESSION/env/LOGUSER"
    echo "$LOGGROUP" > "$SESSIONDIR/$SESSION/env/LOGGROUP"

    # create logging infrastructure
    mkdir -p -m 0750 "$SESSIONDIR/$SESSION/log/main"
    ln -s "$SHAREDIR/logrun" "$SESSIONDIR/$SESSION/log/run"
    touch "$SESSIONDIR/$SESSION/log/main/current"
    chmod 0640 "$SESSIONDIR/$SESSION/log/main/current"
    chown -R "$LOGUSER" "$SESSIONDIR/$SESSION/log/main"
    chgrp -R "$LOGGROUP" "$SESSIONDIR/$SESSION/log"

    # make supervise directory world accessible if requested
    if [ "$SUPERVISE_WORLD_ACCESSIBLE" = 'yes' ] ; then
	mkdir -p -m 0755 "$SESSIONDIR/$SESSION/supervise"
	mkdir -p -m 0755 "$SESSIONDIR/$SESSION/log/supervise"
    fi

    # create socket for screen, since it can't log to stdout
    mkfifo "$SESSIONDIR/$SESSION/socket"
    chown "$SUSER:$LOGGROUP" "$SESSIONDIR/$SESSION/socket"
    chmod 0640 "$SESSIONDIR/$SESSION/socket"

    echo "Created session '$SESSION':"
    display_session "$SESSION"

    update-service --add "$SESSIONDIR/$SESSION" "cereal.$SESSION"
}

# start_session SESSION
start_session() {
    local SESSION
    SESSION="$1"

    chpst -e "$SESSIONDIR/$SESSION/env/" sh -c ". $SHAREDIR/common && start_check" || return 1
    sv start "$SESSIONDIR/$SESSION" || return 1
    rm -r "$SESSIONDIR/$SESSION/down"
    log_write "$SESSION" "session '$SESSION' started."
}

# start session
start() {
    if [ -z "$1" ] ; then
	failure "Not enough input arguments.
Type 'cereal-admin help' for more info."
    elif [ "$1" = '--all' -o "$1" = '-a' ] ; then
	SESSIONS=$(list -n) || failure "There are no sessions." 1
    else
	SESSIONS="$@"
    fi

    for SESSION in $SESSIONS ; do
	if ! is_session "$SESSION" ; then
	    error "Session '$SESSION' not found." 1
	elif is_running "$SESSION" ; then
	    echo "Session '$SESSION' is already running."
	elif is_locked "$SESSION" ; then
	    error "Session tty device appears to be locked."
	elif start_session "$SESSION" ; then
	    echo "Session '$SESSION' started."
	else
	    error "Session '$SESSION' could not be started." 2
	fi
    done
}

# restart_session SESSION
restart_session(){
    local SESSION
    SESSION="$1"
    sv restart "$SESSIONDIR/$SESSION" || return 1
    log_write "$SESSION" "session '$SESSION' restarted."
}

# restart session
restart() {
    if [ -z "$1" ] ; then
	failure "Not enough input arguments.
Type 'cereal-admin help' for more info."
    elif [ "$1" = '--all' -o "$1" = '-a' ] ; then
	SESSIONS=$(list -n) || failure "There are no sessions." 1
    elif [ "$1" = '--running' -o "$1" = '-r' ] ; then
	SESSIONS=$(list | grep '^+' | cut -d ' ' -f 2)
	[ "$SESSIONS" ] || failure "There are no running sessions." 0
    else
	SESSIONS="$@"
    fi
    
    for SESSION in $SESSIONS ; do
	if ! is_session "$SESSION" ; then
	    error "Session '$SESSION' not found." 1
	elif ! is_running "$SESSION" ; then
	    if start_session "$SESSION" ; then
		echo "Session '$SESSION' started."
	    else
		error "Session '$SESSION' could not be started." 2
	    fi
	elif restart_session "$SESSION" ; then
	    echo "Session '$SESSION' restarted."
	else
	    error "Session could not be restarted." 2
	fi
    done
}

# stop_session SESSION
stop_session() {
    local SESSION
    SESSION="$1"
    sv stop "$SESSIONDIR/$SESSION" || return 1
    touch "$SESSIONDIR/$SESSION/down"
    log_write "$SESSION" "session '$SESSION' stopped."
}

# stop session
stop() {
    if [ -z "$1" ] ; then
	failure "Not enough input arguments.
Type 'cereal-admin help' for more info."
    elif [ "$1" = '--all' -o "$1" = '-a' ] ; then
	SESSIONS=$(list -n) || failure "There are no sessions." 1
    else
	SESSIONS="$@"
    fi
    
    for SESSION in $SESSIONS ; do
	if ! is_session "$SESSION" ; then
	    error "Session '$SESSION' not found." 1
	elif stop_session "$SESSION" ; then
	    echo "Session '$SESSION' stopped."
	else
	    error "Session '$SESSION' could not be stopped." 2
	fi
    done
}

# destroy_session SESSION
destroy_session() {
    local SESSION
    SESSION="$1"
    update-service --remove "$SESSIONDIR/$SESSION" "cereal.$SESSION"
    rm -rf "$SESSIONDIR/$SESSION"
}

# destroy session
destroy() {
    if [ -z "$1" ] ; then
	failure "Not enough input arguments.
Type 'cereal-admin help' for more info."
    elif [ "$1" = '--all' -o "$1" = '-a' ] ; then
	SESSIONS=$(list -n) || failure "There are no sessions." 1
    else
	SESSIONS="$@"
    fi

    for SESSION in $SESSIONS ; do
	if ! is_session "$SESSION" ; then
	    error "Session '$SESSION' not found." 1
        elif [ ! -w "$SESSIONDIR/$SESSION" ] ; then
            error "You do not have permission to destroy session '$SESSION'." 2
	elif is_running "$SESSION" ; then
	    echo "Session '$SESSION' is currently running."
	    printf "Really stop and destroy session? [Y|n]: "
	    read OK; OK=${OK:=Y}
	    if [ "$OK" = 'y' -o "$OK" = 'Y' ] ; then
		if stop_session "$SESSION" ; then
		    if destroy_session "$SESSION" ; then
			echo "Session '$SESSION' stopped and destroyed."
		    else
			error "Session '$SESSION' could not be destroyed." 2
		    fi
		else
		    error "Session '$SESSION' could not be stopped." 2
		fi
	    else
		error "Session '$SESSION' not stopped." 1
	    fi
	else
	    printf "Really destroy session '%s'? [Y|n]: " "$SESSION"
	    read OK; OK=${OK:=Y}
	    if [ "$OK" = 'y' -o "$OK" = 'Y' ] ; then
		if destroy_session "$SESSION" ; then
		    echo "Session '$SESSION' destroyed."
		else
		    error "Session '$SESSION' could not be destroyed." 2
		fi
	    else
		error "Session '$SESSION' not destroyed." 1
	    fi
	fi
    done
}

###############################################################
### MAIN

COMMAND="$1"
[ "$COMMAND" ] || failure "Type 'cereal-admin help' for usage."
shift

case $COMMAND in
    'create'|'c')
	create "$@"
	;;
    'start'|'s')
	start "$@"
	;;
    'restart'|'r')
	restart "$@"
	;;
    'stop'|'k')
	stop "$@"
	;;
    'destroy'|'d')
	destroy "$@"
	;;
    'list'|'l')
	list "$@" || failure "There are no sessions." 1
	;;
    'help'|'h'|'?')
	usage
	;;
    *)
	failure "Unknown command: '$COMMAND'
Type 'cereal-admin help' for usage."
	;;
esac

exit "$ERR"