From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by finch.gentoo.org (Postfix) with ESMTPS id 9CDE8139694 for ; Fri, 12 May 2017 02:42:51 +0000 (UTC) Received: from pigeon.gentoo.org (localhost [127.0.0.1]) by pigeon.gentoo.org (Postfix) with SMTP id 6E493E0CC7; Fri, 12 May 2017 02:42:49 +0000 (UTC) Received: from smtp.gentoo.org (smtp.gentoo.org [140.211.166.183]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by pigeon.gentoo.org (Postfix) with ESMTPS id 46EACE0CC7 for ; Fri, 12 May 2017 02:42:49 +0000 (UTC) Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.gentoo.org (Postfix) with ESMTPS id 3DCB73416BF for ; Fri, 12 May 2017 02:42:48 +0000 (UTC) Received: from localhost.localdomain (localhost [IPv6:::1]) by oystercatcher.gentoo.org (Postfix) with ESMTP id 991CF7458 for ; Fri, 12 May 2017 02:42:46 +0000 (UTC) From: "William Hubbs" To: gentoo-commits@lists.gentoo.org Content-Transfer-Encoding: 8bit Content-type: text/plain; charset=UTF-8 Reply-To: gentoo-dev@lists.gentoo.org, "William Hubbs" Message-ID: <1494556743.82e12e309247bc84abf29aca04b3a2dd845fa11b.williamh@OpenRC> Subject: [gentoo-commits] proj/openrc:master commit in: src/rc/ X-VCS-Repository: proj/openrc X-VCS-Files: src/rc/rc-status.c X-VCS-Directories: src/rc/ X-VCS-Committer: williamh X-VCS-Committer-Name: William Hubbs X-VCS-Revision: 82e12e309247bc84abf29aca04b3a2dd845fa11b X-VCS-Branch: master Date: Fri, 12 May 2017 02:42:46 +0000 (UTC) Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: List-Id: Gentoo Linux mail X-BeenThere: gentoo-commits@lists.gentoo.org X-Archives-Salt: 2b85de7c-12a9-43f3-af4b-a9f39a724740 X-Archives-Hash: c320efa0c0a0ea215030ce701f508734 commit: 82e12e309247bc84abf29aca04b3a2dd845fa11b Author: William Hubbs gmail com> AuthorDate: Fri May 12 02:38:55 2017 +0000 Commit: William Hubbs gentoo org> CommitDate: Fri May 12 02:39:03 2017 +0000 URL: https://gitweb.gentoo.org/proj/openrc.git/commit/?id=82e12e30 rc-status: show uptimes and respawn counts for supervised daemons src/rc/rc-status.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) diff --git a/src/rc/rc-status.c b/src/rc/rc-status.c index 8a591db1..a314bb1d 100644 --- a/src/rc/rc-status.c +++ b/src/rc/rc-status.c @@ -76,10 +76,55 @@ print_level(const char *prefix, const char *level) printf("%s\n", level); } +static void get_uptime(const char *service, char *uptime, int uptime_size) +{ + RC_SERVICE state = rc_service_state(service); + char *start_count; + time_t now; + char *start_time_string; + time_t start_time; + double time_diff; + double diff_tmp; + double diff_days; + double diff_hours; + double diff_mins; + + uptime[0] = '\0'; + if (state & RC_SERVICE_STARTED) { + start_count = rc_service_value_get(service, "start_count"); + start_time_string = rc_service_value_get(service, "start_time"); + if (start_count && start_time_string) { + start_time = to_time_t(start_time_string); + now = time(NULL); + time_diff = difftime(now, start_time); + diff_tmp = time_diff; + if (diff_tmp > 86400.0) { + diff_days = diff_tmp / 86400.0; + diff_tmp -= diff_days * 86400.0; + } + if (diff_tmp > 3600.0) { + diff_hours = diff_tmp / 3600.0; + diff_tmp -= diff_hours * 3600.0; + } + if (diff_tmp > 60.0) { + diff_mins = diff_tmp / 60.0; + diff_tmp -= diff_mins * 60.0; + } + if ((int) diff_days > 0) + snprintf(uptime, uptime_size, "%.0f days %02.0f:%02.0f (%s)", + diff_days, diff_hours, diff_mins, start_count); + else + snprintf(uptime, uptime_size, "%02.0f:%02.0f (%s)", + diff_hours, diff_mins, start_count); + } + } +} + static void print_service(const char *service) { - char status[10]; + char status[60]; + char uptime [40]; int cols = printf(" %s", service); const char *c = ecolor(ECOLOR_GOOD); RC_SERVICE state = rc_service_state(service); @@ -101,7 +146,8 @@ print_service(const char *service) { snprintf(status, sizeof(status), " crashed "); } else { - snprintf(status, sizeof(status), " started "); + get_uptime(service, uptime, 40); + snprintf(status, sizeof(status), " started %s", uptime); color = ECOLOR_GOOD; } } else if (state & RC_SERVICE_SCHEDULED) {