~ubuntu-branches/debian/experimental/sysvinit/experimental

« back to all changes in this revision

Viewing changes to debian/initscripts/lib/init/bootclean.sh

  • Committer: Bazaar Package Importer
  • Author(s): Petter Reinholdtsen
  • Date: 2008-08-12 16:07:50 UTC
  • Revision ID: james.westby@ubuntu.com-20080812160750-65gpyv74vh4qr69w
Tags: 2.86.ds1-61
* Fix typo in rcS(5), proberly->properly (Closes: #484233).  Thanks to
  Julien Danjou for noticing.
* Fix typo in rcS(5), maually->manually (Closes: #493680).  Thanks to
  Xr for noticing.
* Modify runlevel detection code in invoke-rc.d to notice the
  difference between runlevels 0 and 6, and the boot runlevel, to
  make it possible to use invoke-rc.d during boot (Closes: 384509).
* Make sure to call restorecon after mounting tmpfs file systems, to
  set SELinux permissions (Closes: #493679).  Patch from Russell
  Coker.
* Move responsibility of stopping the splash screen process from
  individual init.d scripts to init.d/rc.  This make sure the
  progress calculation reflect reality, and that the splash screen
  is taken down in runlevel 1 (Closes: #431560) and that it stop
  before gdm and kdm (Closes: #422922, #489734).
* Skip error message from checkfs.sh when / is read-only.  Patch
  from Mirek Slugen (Closes: #492214).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/bin/sh
 
2
#
 
3
# bootclean
 
4
#
 
5
# Clean /tmp.  Clean /var/run and /var/lock if not mounted as tmpfs
 
6
#
 
7
# DO NOT RUN AFTER S:55bootmisc.sh and do not run this script directly
 
8
# in runlevel S. Instead write an initscript to call it.
 
9
#
 
10
 
 
11
. /lib/init/vars.sh
 
12
 
 
13
. /lib/lsb/init-functions
 
14
 
 
15
# Should be called outside verbose message block
 
16
mkflagfile()
 
17
{
 
18
        # Prevent symlink attack  (See #264234.)
 
19
        [ -L "$1" ] && log_warning_msg "bootclean: Deleting symbolic link '$1'."
 
20
        rm -f "$1" || { log_failure_msg "bootclean: Failure deleting '$1'." ; return 1 ; }
 
21
        # No user processes should be running, so no one should be able to introduce
 
22
        # a symlink here.  As an extra precaution, set noclobber.
 
23
        set -o noclobber
 
24
        :> "$1" || { log_failure_msg "bootclean: Failure creating '$1'." ; return 1 ; }
 
25
        return 0
 
26
}
 
27
 
 
28
clean_tmp() {
 
29
        cd /tmp || { log_failure_msg "bootclean: Could not cd to /tmp." ; return 1 ; }
 
30
 
 
31
        #
 
32
        # Only clean out /tmp if it is world-writable. This ensures
 
33
        # it really is a/the temp directory we're cleaning.
 
34
        #
 
35
        [ "$(find . -maxdepth 0 -perm -002)" = "." ] || return 0
 
36
 
 
37
        if [ ! "$TMPTIME" ]
 
38
        then
 
39
                log_warning_msg "Using default TMPTIME 0."
 
40
                TMPTIME=0
 
41
        fi
 
42
 
 
43
        [ "$VERBOSE" = no ] || log_action_begin_msg "Cleaning /tmp"
 
44
 
 
45
        #
 
46
        # Remove regardless of TMPTIME setting
 
47
        #
 
48
        rm -f .X*-lock
 
49
 
 
50
        #
 
51
        # Don't clean remaining files if TMPTIME is negative or 'infinite'
 
52
        #
 
53
        case "$TMPTIME" in
 
54
          -*|infinite|infinity)
 
55
                [ "$VERBOSE" = no ] || log_action_end_msg 0 "skipped"
 
56
                return 0
 
57
                ;;
 
58
        esac
 
59
 
 
60
        #
 
61
        # Wipe /tmp, excluding system files, but including lost+found
 
62
        #
 
63
        # If TMPTIME is set to 0, we do not use any ctime expression
 
64
        # at all, so we can also delete files with timestamps
 
65
        # in the future!
 
66
        #
 
67
        if [ "$TMPTIME" = 0 ] 
 
68
        then
 
69
                TEXPR=""
 
70
                DEXPR=""
 
71
        else
 
72
                TEXPR="-mtime +$TMPTIME -ctime +$TMPTIME -atime +$TMPTIME"
 
73
                DEXPR="-mtime +$TMPTIME -ctime +$TMPTIME"
 
74
        fi
 
75
 
 
76
        EXCEPT='! -name .
 
77
                ! ( -path ./lost+found -uid 0 )
 
78
                ! ( -path ./quota.user -uid 0 )
 
79
                ! ( -path ./aquota.user -uid 0 )
 
80
                ! ( -path ./quota.group -uid 0 )
 
81
                ! ( -path ./aquota.group -uid 0 )
 
82
                ! ( -path ./.journal -uid 0 )
 
83
                ! ( -path ./.clean -uid 0 )
 
84
                ! ( -path './...security*' -uid 0 )'
 
85
 
 
86
        mkflagfile /tmp/.clean || return 1
 
87
 
 
88
        report_err()
 
89
        {
 
90
                if [ "$VERBOSE" = no ]
 
91
                then
 
92
                        log_failure_msg "bootclean: Failure cleaning /tmp."
 
93
                else
 
94
                        log_action_end_msg 1 "bootclean: Failure cleaning /tmp"
 
95
                fi
 
96
        }
 
97
 
 
98
        #
 
99
        # First remove all old files...
 
100
        # (Use xargs here so that only one additional process gets created)
 
101
        #
 
102
        find . -depth -xdev $TEXPR $EXCEPT ! -type d \
 
103
                -print0 | xargs -0r rm -f -- \
 
104
                || { report_err ; return 1 ; }
 
105
 
 
106
        #
 
107
        # ...and then all empty directories
 
108
        # (Don't use xargs here because dirs must be removed one by one from
 
109
        # the bottom up)
 
110
        #
 
111
        find . -depth -xdev $DEXPR $EXCEPT -type d -empty \
 
112
                -exec rmdir \{\} \; \
 
113
                || { report_err ; return 1 ; }
 
114
 
 
115
        [ "$VERBOSE" = no ] || log_action_end_msg 0
 
116
        return 0
 
117
}
 
118
 
 
119
clean_lock() {
 
120
        if [ yes = "$RAMLOCK" ] ; then
 
121
            return 0
 
122
        fi
 
123
 
 
124
        cd /var/lock || { log_failure_msg "bootclean: Could not cd to /var/lock." ; return 1 ; }
 
125
 
 
126
        [ "$VERBOSE" = no ] || log_action_begin_msg "Cleaning /var/lock"
 
127
        report_err()
 
128
        {
 
129
                if [ "$VERBOSE" = no ]
 
130
                then
 
131
                        log_failure_msg "bootclean: Failure cleaning /var/lock."
 
132
                else
 
133
                        log_action_end_msg 1 "bootclean: Failure cleaning /var/lock"
 
134
                fi
 
135
        }
 
136
        find . ! -type d \
 
137
                -print0 | xargs -0r rm -f -- \
 
138
                || { report_err ; return 1 ; }
 
139
        [ "$VERBOSE" = no ] || log_action_end_msg 0
 
140
        mkflagfile /var/lock/.clean || return 1
 
141
        return 0
 
142
}
 
143
 
 
144
clean_run() {
 
145
        if [ yes = "$RAMRUN" ] ; then
 
146
            return 0
 
147
        fi
 
148
 
 
149
        cd /var/run || { log_action_end_msg 1 "bootclean: Could not cd to /var/run." ; return 1 ; }
 
150
 
 
151
        [ "$VERBOSE" = no ] || log_action_begin_msg "Cleaning /var/run"
 
152
        report_err()
 
153
        {
 
154
                if [ "$VERBOSE" = no ]
 
155
                then
 
156
                        log_failure_msg "bootclean: Failure cleaning /var/run."
 
157
                else
 
158
                        log_action_end_msg 1 "bootclean: Failure cleaning /var/run"
 
159
                fi
 
160
        }
 
161
        find . ! -xtype d ! -name utmp ! -name innd.pid \
 
162
                -print0 | xargs -0r rm -f -- \
 
163
                || { report_err ; return 1 ; }
 
164
        [ "$VERBOSE" = no ] || log_action_end_msg 0
 
165
        mkflagfile /var/run/.clean || return 1
 
166
        return 0
 
167
}
 
168
 
 
169
which find >/dev/null 2>&1 || exit 1
 
170
which xargs >/dev/null 2>&1 || exit 1
 
171
 
 
172
# If there are flag files that have not been created by root
 
173
# then remove them
 
174
for D in /tmp /var/run /var/lock
 
175
do
 
176
        if [ -f $D/.clean ]
 
177
        then
 
178
                which stat >/dev/null 2>&1 && cleanuid="$(stat -c %u $D/.clean)"
 
179
                # Poor's man stat %u, since stat (and /usr) might not be
 
180
                # available in some bootup stages
 
181
                [ "$cleanuid" ] || cleanuid="$(find $D/.clean -printf %U)"
 
182
                [ "$cleanuid" ] || { log_failure_msg "bootclean: Could not stat '$D/.clean'." ; exit 1 ; }
 
183
                if [ "$cleanuid" -ne 0 ]
 
184
                then
 
185
                        rm -f $D/.clean || { log_failure_msg "bootclean: Could not delete '$D/.clean'." ; exit 1 ; }
 
186
                fi
 
187
        fi
 
188
done
 
189
 
 
190
[ -f /tmp/.clean ] && [ -f /var/run/.clean ] && [ -f /var/lock/.clean ] && exit 0
 
191
 
 
192
ES=0
 
193
[ -d /tmp ] && ! [ -f /tmp/.clean ] && { clean_tmp || ES=1 ; }
 
194
[ -d /var/run ] && ! [ -f /var/run/.clean ] && { clean_run || ES=1 ; }
 
195
[ -d /var/lock ] && ! [ -f /var/lock/.clean ] && { clean_lock || ES=1 ; }
 
196
exit $ES
 
197