]> CyberLeo.Net >> Repos - CDN/portage-cdn.git/blob - net-p2p/freenet/files/run.sh-20090501
net-p2p/freenet: revbump
[CDN/portage-cdn.git] / net-p2p / freenet / files / run.sh-20090501
1 #! /bin/sh
2
3 #
4 # Copyright (c) 1999, 2006 Tanuki Software Inc.
5 #
6 # Java Service Wrapper sh script.  Suitable for starting and stopping
7 #  wrapped Java applications on UNIX platforms.
8 #
9
10 #-----------------------------------------------------------------------------
11 # These settings can be modified to fit the needs of your application
12
13 # Application
14 APP_NAME="Freenet"
15 APP_LONG_NAME="Freenet 0.7"
16
17 # Wrapper
18 WRAPPER_CMD="/usr/bin/wrapper"
19 WRAPPER_CONF="/etc/freenet-wrapper.conf"
20
21 # Priority at which to run the wrapper.  See "man nice" for valid priorities.
22 #  nice is only used if a priority is specified.
23
24 # Note that Freenet will scale its usage within the specifed niceness, some
25 # threads will have a lower priority (higher nice value) than this. Also please
26 # don't renice Freenet once it's started.
27 PRIORITY=10
28
29 # Location of the pid file.
30 PIDDIR="/var/freenet/"
31
32 # If uncommented, causes the Wrapper to be shutdown using an anchor file.
33 #  When launched with the 'start' command, it will also ignore all INT and
34 #  TERM signals.
35 IGNORE_SIGNALS=true
36
37 # If specified, the Wrapper will be run as the specified user.
38 # IMPORTANT - Make sure that the user has the required privileges to write
39 #  the PID file and wrapper.log files.  Failure to be able to write the log
40 #  file will cause the Wrapper to exit without any way to write out an error
41 #  message.
42 # NOTE - This will set the user which is used to run the Wrapper as well as
43 #  the JVM and is not useful in situations where a privileged resource or
44 #  port needs to be allocated prior to the user being changed.
45 RUN_AS_USER=freenet
46
47 # The following two lines are used by the chkconfig command. Change as is
48 #  appropriate for your application.  They should remain commented.
49 # chkconfig: 2345 20 80
50 # description: @app.long.name@
51
52 # Do not modify anything beyond this point
53 #-----------------------------------------------------------------------------
54
55 if [ "X`id -u`" = "X0" -a -z "$RUN_AS_USER" ]
56 then
57     echo "Do not run this script as root."
58     exit 1
59 fi
60
61 # and get java implementation too, Sun JDK or Kaffe
62 JAVA_IMPL=`java -version 2>&1 | head -n 1 | cut -f1 -d' '`
63
64 # sun specific options
65 LDPROP=""
66 #if [ "$JAVA_IMPL" = "java" ]
67 #then 
68 #       echo Sun java detected.
69 #       # Tell it not to use NPTL.
70 #       # BAD THINGS happen if it uses NPTL.
71 #       # Specifically, at least on 1.4.1. and 1.5.0b2, we get hangs
72 #       # where many threads are stuck waiting for a lock to be 
73 #       # unlocked but no thread owns it.
74 #
75 #       ## won't work on libc2.4 ... let's hope it's fixed
76 #       if [[ -z "$(/lib/libc.so.6 | head -n 1 | grep 'release version 2.4')" ]]
77 #       then
78 #               if [[ -d /lib/tls ]]
79 #               then
80 #                       LDPROP="set.LD_ASSUME_KERNEL=2.4.1" 
81 #               fi
82 #       fi
83 #fi 
84
85
86 # Get the fully qualified path to the script
87 case $0 in
88     /*)
89         SCRIPT="$0"
90         ;;
91     *)
92         PWD=`pwd`
93         SCRIPT="$PWD/$0"
94         ;;
95 esac
96
97 # Resolve the true real path without any sym links.
98 CHANGED=true
99 while [ "X$CHANGED" != "X" ]
100 do
101     # Change spaces to ":" so the tokens can be parsed.
102     SCRIPT=`echo $SCRIPT | sed -e 's; ;:;g'`
103     # Get the real path to this script, resolving any symbolic links
104     TOKENS=`echo $SCRIPT | sed -e 's;/; ;g'`
105     REALPATH=
106     for C in $TOKENS; do
107         REALPATH="$REALPATH/$C"
108         while [ -h "$REALPATH" ] ; do
109             LS="`ls -ld "$REALPATH"`"
110             LINK="`expr "$LS" : '.*-> \(.*\)$'`"
111             if expr "$LINK" : '/.*' > /dev/null; then
112                 REALPATH="$LINK"
113             else
114                 REALPATH="`dirname "$REALPATH"`""/$LINK"
115             fi
116         done
117     done
118
119     # Change ":" chars back to spaces.
120     REALPATH="`echo $REALPATH | sed -e 's;:; ;g'`"
121     SCRIPT="`echo $SCRIPT | sed -e 's;:; ;g'`"
122
123     if [ "$REALPATH" = "$SCRIPT" ]
124     then
125         CHANGED=""
126     else
127         SCRIPT="$REALPATH"
128     fi
129 done
130
131 # Change the current directory to the location of the script
132 cd "`dirname \"$REALPATH\"`"
133 REALDIR="`pwd`"
134 # If the PIDDIR is relative, set its value relative to the full REALPATH to avoid problems if
135 #  the working directory is later changed.
136 FIRST_CHAR="`echo $PIDDIR | cut -c1,1`"
137 if [ "$FIRST_CHAR" != "/" ]
138 then
139     PIDDIR="$REALDIR/$PIDDIR"
140 fi
141 # Same test for WRAPPER_CMD
142 FIRST_CHAR="`echo $WRAPPER_CMD | cut -c1,1`"
143 if [ "$FIRST_CHAR" != "/" ]
144 then
145     WRAPPER_CMD="$REALDIR/$WRAPPER_CMD"
146 fi
147 # Same test for WRAPPER_CONF
148 FIRST_CHAR="`echo $WRAPPER_CONF | cut -c1,1`"
149 if [ "$FIRST_CHAR" != "/" ]
150 then
151     WRAPPER_CONF="$REALDIR/$WRAPPER_CONF"
152 fi
153
154 # Process ID
155 ANCHORFILE="$PIDDIR/$APP_NAME.anchor"
156 PIDFILE="$PIDDIR/$APP_NAME.pid"
157 LOCKDIR="$REALDIR"
158 LOCKFILE="$LOCKDIR/$APP_NAME"
159 pid=""
160
161 # Resolve the os
162 DIST_OS=`uname -s | tr [:upper:] [:lower:] | tr -d [:blank:]`
163 case "$DIST_OS" in
164     'sunos')
165         DIST_OS="solaris"
166         ;;
167     'hp-ux' | 'hp-ux64')
168         DIST_OS="hpux"
169         ;;
170     'darwin' | 'oarwin')
171         DIST_OS="macosx"
172         
173         #We use the 1.5 jvm if it exists
174         if [ -d /System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/ ]
175         then
176                 export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/1.5.0/Home"
177         fi
178         ;;
179     'unix_sv')
180         DIST_OS="unixware"
181         ;;
182 esac
183
184 # Resolve the architecture
185 DIST_ARCH=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]`
186 case "$DIST_ARCH" in
187     'amd64' | 'ia32' | 'ia64' | 'i386' | 'i486' | 'i586' | 'i686' | 'x86_64')
188         DIST_ARCH="x86"
189         ;;
190     'ip27' | 'mips')
191         DIST_ARCH="mips"
192         ;;
193     'power' | 'powerpc' | 'power_pc' | 'ppc64')
194         DIST_ARCH="ppc"
195         ;;
196     'pa_risc' | 'pa-risc')
197         DIST_ARCH="parisc"
198         ;;
199     'sun4u' | 'sparcv9')
200         DIST_ARCH="sparc"
201         ;;
202     '9000/800')
203         DIST_ARCH="parisc"
204         ;;
205 esac
206
207 # Check if we are running on 64bit platform, seems like a workaround for now...
208 DIST_BIT=`uname -m | tr [:upper:] [:lower:] | tr -d [:blank:]`
209 case "$DIST_BIT" in
210     'amd64' | 'ia64' | 'x86_64' | 'ppc64')
211         DIST_BIT="64"
212         ;;
213 #    'pa_risc' | 'pa-risc') # Are some of these 64bit? Least not all...
214 #       BIT="64"
215 #        ;;
216     'sun4u' | 'sparcv9') # Are all sparcs 64?
217         DIST_BIT="64"
218         ;;
219 #    '9000/800')
220 #       DIST_BIT="64"
221 #        ;;
222     *) # In any other case default to 32
223         DIST_BIT="32"
224         ;;
225 esac
226
227 # Decide on the wrapper binary to use.
228 # 64bit wrapper by default on 64bit platforms, because
229 # they might not have 32bit emulation libs installed. 
230 # For macosx, we also want to look for universal binaries.
231
232 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BIT"
233
234 if [ -x "$WRAPPER_TEST_CMD" ]
235 then
236     WRAPPER_CMD="$WRAPPER_TEST_CMD"
237 else
238     if [ "$DIST_OS" = "macosx" ] # Some osx weirdness, someone please check that this still works
239     then
240         WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-$DIST_BIT"
241         if [ -x "$WRAPPER_TEST_CMD" ]
242         then
243             WRAPPER_CMD="$WRAPPER_TEST_CMD"
244         else
245             WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BIT"
246             if [ -x "$WRAPPER_TEST_CMD" ]
247             then
248                 WRAPPER_CMD="$WRAPPER_TEST_CMD"
249             else
250                 WRAPPER_TEST_CMD="$WRAPPER_CMD-$DIST_OS-universal-$DIST_BIT"
251                 if [ -x "$WRAPPER_TEST_CMD" ]
252                 then
253                     WRAPPER_CMD="$WRAPPER_TEST_CMD"
254                 else
255                     if [ ! -x "$WRAPPER_CMD" ]
256                     then
257                         echo "Unable to locate any of the following binaries:"
258                         echo "  $WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BIT"
259                         echo "  $WRAPPER_CMD-$DIST_OS-universal-$DIST_BIT"
260                         echo "  $WRAPPER_CMD"
261                         #
262                         # We need -Djava.net.preferIPv4Stack=true on FreeBSD, otherwise recent jvms thow an IllegalArgumentException when we create the socket
263                         #
264                         NO_WRAPPER="java -Djava.net.preferIPv4Stack=true -cp freenet-ext.jar:freenet.jar freenet.node.NodeStarter"
265                     fi
266                 fi
267             fi
268         fi
269     else
270         if [ ! -x "$WRAPPER_CMD" ]
271         then
272             echo "Unable to locate any of the following binaries:"
273             echo "  $WRAPPER_CMD-$DIST_OS-$DIST_ARCH-$DIST_BIT"
274             echo "  $WRAPPER_CMD"
275             NO_WRAPPER="java -cp freenet-ext.jar:freenet.jar freenet.node.NodeStarter"
276         fi
277     fi
278 fi
279
280 # Build the nice clause
281 if [ "X$PRIORITY" = "X" ]
282 then
283     CMDNICE=""
284 else
285     CMDNICE="nice -$PRIORITY"
286 fi
287
288 # Build the anchor file clause.
289 if [ "X$IGNORE_SIGNALS" = "X" ]
290 then
291    ANCHORPROP=
292    IGNOREPROP=
293 else
294    ANCHORPROP=wrapper.anchorfile=\"$ANCHORFILE\"
295    IGNOREPROP=wrapper.ignore_signals=TRUE
296 fi
297
298 # Build the lock file clause.  Only create a lock file if the lock directory exists on this platform.
299 if [ -d "$LOCKDIR" ]
300 then
301     LOCKPROP=wrapper.lockfile=\"$LOCKFILE\"
302 else
303     LOCKPROP=
304 fi
305
306 checkUser() {
307     # Check the configured user.  If necessary rerun this script as the desired user.
308     if [ "X$RUN_AS_USER" != "X" ]
309     then
310         # Resolve the location of the 'id' command
311         IDEXE="/usr/xpg4/bin/id"
312         if [ ! -x $IDEXE ]
313         then
314             IDEXE="/usr/bin/id"
315             if [ ! -x $IDEXE ]
316             then
317                 echo "Unable to locate 'id'."
318                 echo "Please report this message along with the location of the command on your system."
319                 exit 1
320             fi
321         fi
322     
323         if [ "`$IDEXE -u -n`" = "$RUN_AS_USER" ]
324         then
325             # Already running as the configured user.  Avoid password prompts by not calling su.
326             RUN_AS_USER=""
327         fi
328     fi
329     if [ "X$RUN_AS_USER" != "X" ]
330     then
331         # If LOCKPROP and $RUN_AS_USER are defined then the new user will most likely not be
332         # able to create the lock file.  The Wrapper will be able to update this file once it
333         # is created but will not be able to delete it on shutdown.  If $2 is defined then
334         # the lock file should be created for the current command
335         if [ "X$LOCKPROP" != "X" ]
336         then
337             if [ "X$2" != "X" ]
338             then
339                 # Resolve the primary group 
340                 RUN_AS_GROUP=`groups $RUN_AS_USER | awk '{print $3}' | tail -1`
341                 if [ "X$RUN_AS_GROUP" = "X" ]
342                 then
343                     RUN_AS_GROUP=$RUN_AS_USER
344                 fi
345                 touch "$LOCKFILE"
346                 chown $RUN_AS_USER:$RUN_AS_GROUP "$LOCKFILE"
347             fi
348         fi
349
350         # Still want to change users, recurse.  This means that the user will only be
351         #  prompted for a password once.
352         su -m $RUN_AS_USER -c "$REALPATH $1"
353
354         # Now that we are the original user again, we may need to clean up the lock file.
355         if [ "X$LOCKPROP" != "X" ]
356         then
357             getpid
358             if [ "X$pid" = "X" ]
359             then
360                 # Wrapper is not running so make sure the lock file is deleted.
361                 if [ -f "$LOCKFILE" ]
362                 then
363                     rm "$LOCKFILE"
364                 fi
365             fi
366         fi
367
368         exit 0
369     fi
370 }
371
372 getpid() {
373     if [ -f "$PIDFILE" ]
374     then
375         if [ -r "$PIDFILE" ]
376         then
377             pid="`cat \"$PIDFILE\"`"
378             if [ "X$pid" != "X" ]
379             then
380                 # It is possible that 'a' process with the pid exists but that it is not the
381                 #  correct process.  This can happen in a number of cases, but the most
382                 #  common is during system startup after an unclean shutdown.
383                 # So make sure the process is one of "ours" -- that we can send
384                 # a signal to it.  (We don't use ps(1) because that's neither
385                 # safe nor portable.
386                 if ! kill -0 $pid 2>/dev/null
387                 then
388                     # This is a stale pid file.
389                     rm -f "$PIDFILE"
390                     echo "Removed stale pid file: $PIDFILE"
391                     pid=""
392                 fi
393                 # Sometimes the pid exists and it's ours!
394                 if ! test -f /proc/$pid/cwd/Freenet.pid
395                 then
396                     # This is a stale pid file.
397                     rm -f "$PIDFILE"
398                     echo "Removed stale pid file2: $PIDFILE"
399                     pid=""
400                     
401                 fi
402             fi
403         else
404             echo "Cannot read $PIDFILE."
405             exit 1
406         fi
407     fi
408 }
409
410 testpid() {
411     if ! kill -0 $pid 2>/dev/null
412     then
413         # Process is gone so remove the pid file.
414         rm -f "$PIDFILE"
415         pid=""
416     fi
417 }
418
419 console() {
420     echo "Running $APP_LONG_NAME..."
421     getpid
422     if [ "X$pid" = "X" ]
423     then
424         COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" $LDPROP $ANCHORPROP $LOCKPROP"
425         eval $COMMAND_LINE
426     else
427         echo "$APP_LONG_NAME is already running."
428         exit 1
429     fi
430 }
431  
432 start() {
433     echo "Starting $APP_LONG_NAME..."
434     getpid
435     if [ "X$pid" = "X" ]
436     then
437         if [ "$NO_WRAPPER" ] # Check if we don't have usable wrapper, and run without it
438         then
439             echo ""
440             echo "Let's start the node without the wrapper, you'll have to daemonize it yourself."
441             eval $NO_WRAPPER
442         else                 # Otherwise use the wrapper
443             COMMAND_LINE="$CMDNICE \"$WRAPPER_CMD\" \"$WRAPPER_CONF\" wrapper.syslog.ident=\"$APP_NAME\" wrapper.pidfile=\"$PIDFILE\" $LDPROP wrapper.daemonize=TRUE $ANCHORPROP $IGNOREPROP $LOCKPROP"
444             eval $COMMAND_LINE
445         fi
446     else
447         echo "$APP_LONG_NAME is already running."
448         exit 1
449     fi
450 }
451  
452 stopit() {
453     echo "Stopping $APP_LONG_NAME..."
454     getpid
455     if [ "X$pid" = "X" ]
456     then
457         echo "$APP_LONG_NAME was not running."
458     else
459         if [ "X$IGNORE_SIGNALS" = "X" ]
460         then
461             # Running so try to stop it.
462             kill $pid
463             if [ $? -ne 0 ]
464             then
465                 # An explanation for the failure should have been given
466                 echo "Unable to stop $APP_LONG_NAME."
467                 exit 1
468             fi
469         else
470             rm -f "$ANCHORFILE"
471             if [ -f "$ANCHORFILE" ]
472             then
473                 # An explanation for the failure should have been given
474                 echo "Unable to stop $APP_LONG_NAME."
475                 exit 1
476             fi
477         fi
478
479         # We can not predict how long it will take for the wrapper to
480         #  actually stop as it depends on settings in wrapper.conf.
481         #  Loop until it does.
482         savepid=$pid
483         CNT=0
484         TOTCNT=0
485         while [ "X$pid" != "X" ]
486         do
487             # Show a waiting message every 5 seconds.
488             if [ "$CNT" -lt "5" ]
489             then
490                 CNT=`expr $CNT + 1`
491             else
492                 echo "Waiting for $APP_LONG_NAME to exit..."
493                 CNT=0
494             fi
495             TOTCNT=`expr $TOTCNT + 1`
496
497             sleep 1
498
499             testpid
500         done
501
502         pid=$savepid
503         testpid
504         if [ "X$pid" != "X" ]
505         then
506             echo "Failed to stop $APP_LONG_NAME."
507             exit 1
508         else
509             echo "Stopped $APP_LONG_NAME."
510         fi
511     fi
512 }
513
514 status() {
515     getpid
516     if [ "X$pid" = "X" ]
517     then
518         echo "$APP_LONG_NAME is not running."
519         exit 1
520     else
521         echo "$APP_LONG_NAME is running ($pid)."
522         exit 0
523     fi
524 }
525
526 dump() {
527     echo "Dumping $APP_LONG_NAME..."
528     getpid
529     if [ "X$pid" = "X" ]
530     then
531         echo "$APP_LONG_NAME was not running."
532
533     else
534         kill -QUIT $pid
535
536         if [ $? -ne 0 ]
537         then
538             echo "Failed to dump $APP_LONG_NAME."
539             exit 1
540         else
541             echo "Dumped $APP_LONG_NAME."
542         fi
543     fi
544 }
545
546 case "$1" in
547
548     'console')
549         checkUser $1 touchlock
550         console
551         ;;
552
553     'start')
554         checkUser $1 touchlock
555         start
556         ;;
557
558     'stop')
559         checkUser $1
560         stopit
561         ;;
562
563     'restart')
564         checkUser $1 touchlock
565         stopit
566         start
567         ;;
568
569     'status')
570         checkUser $1
571         status
572         ;;
573
574     'dump')
575         checkUser $1
576         dump
577         ;;
578
579     *)
580         echo "Usage: $0 { console | start | stop | restart | status | dump }"
581         exit 1
582         ;;
583 esac
584
585 exit 0