Gentoo Archives: gentoo-commits

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