From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-598006-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	by finch.gentoo.org (Postfix) with ESMTP id 3E192138200
	for <garchives@archives.gentoo.org>; Sun,  9 Jun 2013 09:44:48 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id BD9C7E07FB;
	Sun,  9 Jun 2013 09:44:47 +0000 (UTC)
Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183])
	(using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 53A07E07FB
	for <gentoo-commits@lists.gentoo.org>; Sun,  9 Jun 2013 09:44:47 +0000 (UTC)
Received: from hornbill.gentoo.org (hornbill.gentoo.org [94.100.119.163])
	(using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits))
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 6A4C233DFE1
	for <gentoo-commits@lists.gentoo.org>; Sun,  9 Jun 2013 09:44:46 +0000 (UTC)
Received: from localhost.localdomain (localhost [127.0.0.1])
	by hornbill.gentoo.org (Postfix) with ESMTP id 0E211E468F
	for <gentoo-commits@lists.gentoo.org>; Sun,  9 Jun 2013 09:44:44 +0000 (UTC)
From: "Richard Yao" <ryao@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Richard Yao" <ryao@gentoo.org>
Message-ID: <1370771036.add80bbae16dff97c52be8d8da92fe3ccdf4bdd9.ryao@gentoo>
Subject: [gentoo-commits] proj/genkernel:ryao commit in: defaults/
X-VCS-Repository: proj/genkernel
X-VCS-Files: defaults/initrd.scripts
X-VCS-Directories: defaults/
X-VCS-Committer: ryao
X-VCS-Committer-Name: Richard Yao
X-VCS-Revision: add80bbae16dff97c52be8d8da92fe3ccdf4bdd9
X-VCS-Branch: ryao
Date: Sun,  9 Jun 2013 09:44:44 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Archives-Salt: 7269bfce-21ec-4a00-933d-165e73fb0609
X-Archives-Hash: 052548b6f45237a2d5f699a411591a69

commit:     add80bbae16dff97c52be8d8da92fe3ccdf4bdd9
Author:     Richard Yao <ryao <AT> gentoo <DOT> org>
AuthorDate: Sun Jun  9 09:40:07 2013 +0000
Commit:     Richard Yao <ryao <AT> gentoo <DOT> org>
CommitDate: Sun Jun  9 09:43:56 2013 +0000
URL:        http://git.overlays.gentoo.org/gitweb/?p=proj/genkernel.git;a=commit;h=add80bba

Add call_func_timeout helper function

call_func_timeout permits us to call a helper function and kill it
should it exceed some timeout. This is derived from example code posted
on Stack Overflow:

http://stackoverflow.com/a/11056286

Signed-off-by: Richard Yao <ryao <AT> gentoo.org>

---
 defaults/initrd.scripts | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/defaults/initrd.scripts b/defaults/initrd.scripts
index 9b0e899..c8e7abb 100644
--- a/defaults/initrd.scripts
+++ b/defaults/initrd.scripts
@@ -2,6 +2,22 @@
 
 . /etc/initrd.defaults
 
+call_func_timeout()
+{
+	local func=$1 timeout=$2 pid watcher
+	[ $# -ne 2 ] && gen_die "call_func_timeout() called with $# arguments"
+
+	( ${func} ) & pid=$!
+	( sleep ${timeout} && kill -HUP ${pid} ) 2>/dev/null & watcher=$!
+	if wait ${pid} 2>/dev/null; then
+	    kill -HUP $watcher 2> /dev/null
+	    wait $watcher
+	    return 1
+	fi
+
+	return 0
+}
+
 modules_load() {
 	for module in $*
 	do