--- randomsound-0.2.orig/randomsound.c +++ randomsound-0.2/randomsound.c @@ -4,6 +4,9 @@ #include #include #include +#include +#include +#include #include #include @@ -14,12 +17,14 @@ #include "asoundrunner.h" static char *version_number = "0.2"; +static const char *pidfile = NULL; int daemonise = 0; int verbose = 0; int minwatermark = 256; int maxwatermark = 4096-256; int depositsize = 64; int buffersize = 64 * 16; +int time_to_quit = 0; BitBuffer incoming_bits; BitBuffer buffered_bits; @@ -75,7 +80,7 @@ FD_ZERO(&writefds); - while (1) { + while (!time_to_quit) { if (restart_arecord == 1) { int pid; if (verbose > 1) @@ -96,7 +101,8 @@ FD_SET(arecord_read_fd, &errfds); ret = select(arecord_read_fd + 1, &readfds, &writefds, &errfds, NULL); if (ret == -1) { - perror("select"); + if (!time_to_quit) + perror("select"); return; } if (FD_ISSET(arecord_read_fd, &readfds)) @@ -134,8 +140,9 @@ fprintf(output, "%s: Usage:\n"\ "\n"\ "Argument: h - display this help message\n"\ - " V - display version information.\n", + " V - display version information.\n"\ " D - Daemonize\n"\ + " p - pidfile\n"\ " v - Increase verbosity. Can be used more than once.\n"\ " m - specify minimum number of bits of entropy in the pool.\n"\ " M - specify max number of bits in the pool.\n"\ @@ -152,12 +159,29 @@ prog, version_number); } +void +cleanup(void) +{ + if (pidfile) + if (unlink(pidfile) < 0) + perror(pidfile); + exit(0); +} + +static void +caught_signal(int sig) +{ + time_to_quit++; +} + int main(int argc, char **argv) { - int opt; + int opt, oldpid; + FILE *file; + struct sigaction sigact; - while ((opt = getopt(argc, argv, ":hDvVm:M:b:d:")) != -1) { + while ((opt = getopt(argc, argv, ":hDp:vVm:M:b:d:")) != -1) { switch (opt) { case 'h': usage(argv[0], stdout); @@ -168,6 +192,9 @@ case 'D': daemonise = 1; break; + case 'p': + pidfile = optarg; + break; case 'v': verbose += 1; break; @@ -207,6 +234,9 @@ return 2; } + if (daemonise && !pidfile) + pidfile = "/var/run/randomsound.pid"; + if (verbose > 0) { printf("Random sound daemon. Copyright 2007 Daniel Silverstone.\n\n"); printf("Will keep random pool between %d and %d bits of entropy.\n", minwatermark, maxwatermark); @@ -243,8 +273,33 @@ return 4; } if (fd != 0) return 0; + + memset(&sigact, 0, sizeof(struct sigaction)); + sigact.sa_handler = caught_signal; + sigaction(SIGTERM, &sigact, NULL); + sigaction(SIGHUP, &sigact, NULL); + sigaction(SIGINT, &sigact, NULL); + + if((file = fopen(pidfile, "r")) != NULL) { + if ((fscanf(file, "%d", &oldpid)) > 0) { + if ((kill (oldpid, 0)) == 0) { + fprintf(stderr, "another randomsound is already running with pid %d\n", oldpid); + exit(0); + } + } + fclose(file); + } + + if((file = fopen(pidfile, "w")) == NULL) { + perror("Can't open pidfile\n"); + exit(1); + } + + fprintf(file, "%d\n", (int)getpid()); + fclose(file); setpgrp(); setsid(); + atexit(cleanup); } main_loop(); --- randomsound-0.2.orig/asoundrunner.c +++ randomsound-0.2/asoundrunner.c @@ -4,6 +4,7 @@ #include #include #include +#include #include #include --- randomsound-0.2.orig/debian/install +++ randomsound-0.2/debian/install @@ -0,0 +1 @@ +randomsound usr/sbin/ --- randomsound-0.2.orig/debian/rules +++ randomsound-0.2/debian/rules @@ -0,0 +1,72 @@ +#!/usr/bin/make -f +# -*- makefile -*- +# Sample debian/rules that uses debhelper. +# This file was originally written by Joey Hess and Craig Small. +# As a special exception, when this file is copied by dh-make into a +# dh-make output file, you may use that output file without restriction. +# This special exception was added by Craig Small in version 0.37 of dh-make. + +# Uncomment this to turn on verbose mode. +#export DH_VERBOSE=1 + + + + +CFLAGS = -Wall -g + +ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS))) + CFLAGS += -O0 +else + CFLAGS += -O2 +endif + +build: build-stamp + +build-stamp: + dh_testdir + + $(MAKE) + $(MAKE) check + touch $@ + +clean: + dh_testdir + dh_testroot + rm -f build-stamp + + # Add here commands to clean up after the build process. + $(MAKE) clean + + dh_clean + +install: build + dh_testdir + dh_testroot + dh_clean -k + dh_installdirs + +# Build architecture-independent files here. +binary-indep: build install +# We have nothing to do by default. + +# Build architecture-dependent files here. +binary-arch: build install + dh_testdir + dh_testroot + dh_installchangelogs Changes + dh_installdocs + dh_install + dh_installinit + dh_installman + dh_link + dh_strip + dh_compress + dh_fixperms + dh_installdeb + dh_shlibdeps + dh_gencontrol + dh_md5sums + dh_builddeb + +binary: binary-indep binary-arch +.PHONY: build clean binary-indep binary-arch binary install configure --- randomsound-0.2.orig/debian/init.d +++ randomsound-0.2/debian/init.d @@ -0,0 +1,60 @@ +#!/bin/sh +### BEGIN INIT INFO +# Provides: randomsound +# Required-Start: $remote_fs $syslog alsa-utils +# Required-Stop: $remote_fs $syslog +# Default-Start: 2 3 4 5 +# Default-Stop: 0 1 6 +# Short-Description: Random Sound +# Description: ALSA sound card related entropy gathering daemon. +### END INIT INFO + +PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin +DAEMON=/usr/sbin/randomsound +NAME=randomsound +DESC="ALSA Entropy gatherer" + +test -x $DAEMON || exit 0 +if [ ! -e /proc/asound/cards ]; then + echo "No /proc/asound/cards: is alsa configured?" + exit 0 +fi + +# Include randomsound defaults if available +if [ -f /etc/default/randomsound ] ; then + . /etc/default/randomsound +fi + +case "$1" in + start) + echo -n "Starting $DESC: " + start-stop-daemon --start --quiet --exec $DAEMON -- -D $DAEMON_OPTS > /dev/null + echo "$NAME." + ;; + stop) + echo -n "Stopping $DESC: " + start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid + echo "$NAME." + ;; + force-reload) + # check wether $DAEMON is running. If so, restart + start-stop-daemon --stop --test --quiet --pidfile \ + /var/run/$NAME.pid --exec $DAEMON \ + && $0 restart \ + || exit 0 + ;; + restart) + echo -n "Restarting $DESC: " + start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid + sleep 1 + start-stop-daemon --start --quiet --exec $DAEMON -- -D $DAEMON_OPTS > /dev/null + echo "$NAME." + ;; + *) + N=/etc/init.d/$NAME + echo "Usage: $N {start|stop|restart|force-reload}" >&2 + exit 1 + ;; +esac + +exit 0 --- randomsound-0.2.orig/debian/randomsound.default +++ randomsound-0.2/debian/randomsound.default @@ -0,0 +1,10 @@ +# Defaults for randomsound initscript +# sourced by /etc/init.d/randomsound +# installed at /etc/default/randomsound by the maintainer scripts + +# +# This is a POSIX shell fragment +# + +# Additional options that are passed to the Daemon. -D is assumed +DAEMON_OPTS="" --- randomsound-0.2.orig/debian/compat +++ randomsound-0.2/debian/compat @@ -0,0 +1 @@ +5 --- randomsound-0.2.orig/debian/copyright +++ randomsound-0.2/debian/copyright @@ -0,0 +1,33 @@ +This package was debianized by Stephen Gran on +Wed, 08 Aug 2007 17:32:03 +0100. + +It was downloaded from http://www.digital-scurf.org/software/randomsound + +Upstream Author: + + Daniel Silverstone + +Copyright (C) 2007 Daniel Silverstone + +License: + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + +The Debian packaging is (C) 2007, Stephen Gran and +is licensed under the GPL, see `/usr/share/common-licenses/GPL'. --- randomsound-0.2.orig/debian/changelog +++ randomsound-0.2/debian/changelog @@ -0,0 +1,33 @@ +randomsound (0.2-5) unstable; urgency=low + + * Some init script header fixups (closes: #542271) + * Control information fixup + + -- Stephen Gran Sun, 13 Sep 2009 20:17:49 +0100 + +randomsound (0.2-4) unstable; urgency=low + + * Add pidfile handling to daemon (closes: #500504) + + -- Stephen Gran Mon, 29 Sep 2008 00:40:38 +0100 + +randomsound (0.2-3) unstable; urgency=low + + * Make init script check for presence of alsa sound card and exit + with a sensible warning if none is found (closes: #484742) + * Clean up some compiler warnings + * Update Standards-Version (no changes) + + -- Stephen Gran Sun, 28 Sep 2008 13:48:25 +0100 + +randomsound (0.2-2) unstable; urgency=low + + * Make debian/copyright be slightly more useful. + + -- Stephen Gran Fri, 14 Sep 2007 11:37:59 +0100 + +randomsound (0.2-1) unstable; urgency=low + + * Initial release + + -- Stephen Gran Sat, 18 Aug 2007 13:07:12 +0100 --- randomsound-0.2.orig/debian/control +++ randomsound-0.2/debian/control @@ -0,0 +1,15 @@ +Source: randomsound +Section: admin +Priority: extra +Maintainer: Stephen Gran +Build-Depends: debhelper (>= 5) +Homepage: http://www.digital-scurf.org/software/randomsound +Standards-Version: 3.8.3 + +Package: randomsound +Architecture: any +Depends: ${shlibs:Depends}, alsa-utils +Description: ALSA sound card related entropy gathering daemon + Using the low order bit of the ADC output of your sound card, + randomsound gathers entropy, debiases it and offers it up to your + kernel's random pool. --- randomsound-0.2.orig/debian/manpages +++ randomsound-0.2/debian/manpages @@ -0,0 +1 @@ +randomsound.8