rc-status-page.c (2739B)
- // rc-status-page: Basic HTML status page based on OpenRC
 - // Copyright © 2022 Haelwenn (lanodan) Monnier <contact+rc-status-page@hacktivis.me>
 - // SPDX-License-Identifier: BSD-2-Clause
 - #include <sys/queue.h> /* TAILQ_FOREACH() */
 - #include <rc.h> /* rc_ / RC_ */
 - #include <stdlib.h> /* printf */
 - #include <errno.h> /* errno */
 - #include <time.h> /* time(), gmtime(), strftime() */
 - static RC_DEPTREE *deptree;
 - static RC_STRINGLIST *types;
 - RC_STRINGLIST *levels, *services;
 - static void
 - print_service(const char *service)
 - {
 - RC_SERVICE state = rc_service_state(service);
 - char *status;
 - if(state & RC_SERVICE_STOPPING) {
 - status = "stopping";
 - } else if(state & RC_SERVICE_STARTING) {
 - status = "starting";
 - } else if(state & RC_SERVICE_INACTIVE) {
 - status = "inactive";
 - } else if(state & RC_SERVICE_SCHEDULED) {
 - status = "scheduled";
 - } else if(state & RC_SERVICE_STARTED) {
 - errno = 0;
 - if (rc_service_daemons_crashed(service) && errno != EACCES) {
 - status = "crashed";
 - } else {
 - status = "started";
 - }
 - } else {
 - status = "stopped";
 - }
 - printf("\t<dt class=\"service-name\">%s</dt><dd class=\"service-%s\">%s</dd>\n", service, status, status);
 - }
 - static void
 - print_services(const char *runlevel, RC_STRINGLIST *svcs)
 - {
 - RC_STRINGLIST *l = NULL;
 - RC_STRING *s;
 - char *r = NULL;
 - if (!svcs) { return; }
 - if (!deptree) { deptree = rc_deptree_load(); }
 - if (!types) {
 - types = rc_stringlist_new();
 - rc_stringlist_add(types, "ineed");
 - rc_stringlist_add(types, "iuse");
 - rc_stringlist_add(types, "iafter");
 - }
 - if (!runlevel) { r = rc_runlevel_get(); }
 - l = rc_deptree_depends(deptree, types, svcs, r ? r : runlevel, RC_DEP_STRICT | RC_DEP_TRACE | RC_DEP_START);
 - free(r);
 - if (!l) return;
 - printf("<dl class=\"services\">\n");
 - TAILQ_FOREACH(s, l, entries) {
 - if (!rc_stringlist_find(svcs, s->value)) continue;
 - if (!runlevel || rc_service_in_runlevel(s->value, runlevel)) print_service(s->value);
 - }
 - rc_stringlist_free(l);
 - printf("</dl>\n");
 - }
 - int
 - main() {
 - RC_STRING *l;
 - char timestamp[BUFSIZ];
 - time_t now = time(NULL);
 - struct tm *tm = gmtime(&now);
 - static char *title = "Status page of hacktivis.me";
 - strftime(timestamp, sizeof(timestamp), "%FT%TZ", tm);
 - levels = rc_stringlist_new();
 - rc_stringlist_add(levels, "hacktivism");
 - printf("<html><head><link rel=stylesheet href=\"rc-status-page.css\"/><title>%s</title></head>\n", title);
 - printf("<h1>%s</h1>\n", title);
 - TAILQ_FOREACH(l, levels, entries) {
 - services = rc_services_in_runlevel(l->value);
 - print_services(l->value, services);
 - }
 - printf("<footer>Generated by <a href=\"https://hacktivis.me/git/rc-status-page\">rc-status-page</a> at %s</footer>", timestamp);
 - printf("</body></html>\n");
 - rc_stringlist_free(levels);
 - return 0;
 - }