commit 8b31de3b4b151b861b083d6445dd04818c949b0f
parent 0546033a2dd426a3c9d638f2a5a26aee12846575
Author: Friedel Schon <[email protected]>
Date: Wed, 12 Apr 2023 00:44:56 +0200
clear-up serialization and prettify output
Diffstat:
4 files changed, 88 insertions(+), 24 deletions(-)
diff --git a/src/command.c b/src/command.c
@@ -42,7 +42,7 @@ int service_command(char command, char extra, string service, service_t* respons
int res;
read(sockfd, &res, 1);
- uint8_t service_buffer[19];
+ uint8_t service_buffer[SV_SERIAL_LEN];
if (res == 0) {
if (response) {
diff --git a/src/exec/fsvc.c b/src/exec/fsvc.c
@@ -30,35 +30,35 @@ static const char HELP_MESSAGE[] =
static const char VERSION_MESSAGE[] =
"FISS v" SV_VERSION "\n";
-void print_status(service_t* s) {
- const char* state;
+
+void print_status(service_t* s, char* state, size_t size) {
switch (s->state) {
case STATE_INACTIVE:
- state = "inactive";
+ strcpy(state, "inactive");
break;
case STATE_STARTING:
- state = "starting";
+ strcpy(state, "starting");
break;
case STATE_ACTIVE_PID:
- state = "active (pid)";
+ snprintf(state, size, "active (pid) as %d", s->pid);
break;
case STATE_ACTIVE_BACKGROUND:
- state = "active (background)";
+ strcpy(state, "active (background)");
break;
case STATE_ACTIVE_DUMMY:
- state = "active (dummy)";
+ strcpy(state, "active (dummy)");
break;
case STATE_ACTIVE_FOREGROUND:
- state = "active";
+ snprintf(state, size, "active as %d", s->pid);
break;
case STATE_FINISHING:
- state = "finishing";
+ strcpy(state, "finishing");
break;
case STATE_STOPPING:
- state = "stopping";
+ strcpy(state, "stopping");
break;
case STATE_DEAD:
- state = "dead";
+ strcpy(state, "dead");
break;
}
time_t diff = time(NULL) - s->status_change;
@@ -75,23 +75,63 @@ void print_status(service_t* s) {
diff /= 24;
diff_unit = "days";
}
- printf("%s since %lu%s", state, diff, diff_unit);
+ int len = strlen(state);
+ snprintf(state + len, size - len, " since %lu%s", diff, diff_unit);
}
void print_service(service_t* s, service_t* log) {
- printf("- %s (", s->name);
- print_status(s);
- printf(")\n");
+ if (s->is_log_service)
+ return;
+ char state[100];
+ print_status(s, state, sizeof(state));
+
+ printf("- %s (%s)\n", s->name, state);
printf(" [ %c ] restart on exit\n", s->restart_file || s->restart_manual ? 'x' : ' ');
printf(" [%3d] last return code (%s)\n", s->return_code, s->last_exit == EXIT_SIGNALED ? "signaled" : "exited");
if (s->log_service) {
- printf(" logging: ");
- print_status(log);
- printf("\n");
+ print_status(log, state, sizeof(state));
+ printf(" logging: %s\n", state);
}
printf("\n");
}
+void print_service_short(service_t* s, service_t* log) {
+ bool active = s->state == STATE_ACTIVE_BACKGROUND ||
+ s->state == STATE_ACTIVE_DUMMY ||
+ s->state == STATE_ACTIVE_FOREGROUND ||
+ s->state == STATE_ACTIVE_PID;
+
+ bool restart = s->restart_file == S_ONCE ||
+ s->restart_file == S_RESTART ||
+ s->restart_manual == S_ONCE ||
+ s->restart_manual == S_RESTART;
+
+ bool wants_other = active != restart;
+
+ if (s->state == STATE_STARTING)
+ printf("[ {+}]");
+ else if (s->state == STATE_FINISHING || s->state == STATE_STOPPING)
+ printf("[{-} ]");
+ else if (active)
+ printf("[ %s + ]", wants_other ? "<-" : " ");
+ else
+ printf("[ - %s ]", wants_other ? "->" : " ");
+
+ printf(" %s", s->name);
+ if (s->state == STATE_ACTIVE_PID || s->state == STATE_ACTIVE_FOREGROUND)
+ printf(" (pid: %d)", s->pid);
+ else if (s->last_exit != EXIT_SIGNALED)
+ printf(" (last exit: SIG%s)", strsignal(s->return_code));
+ else if (s->last_exit != EXIT_NORMAL)
+ printf(" (last exit: %d)", s->return_code);
+ printf("\n");
+
+ if (log) {
+ printf("log: ");
+ print_service_short(log, NULL);
+ }
+}
+
static const struct option long_options[] = {
{ "help", no_argument, 0, 'h' },
{ "verbose", no_argument, 0, 'v' },
@@ -102,6 +142,7 @@ static const struct option long_options[] = {
{ "once", no_argument, 0, 'o' },
{ "check", no_argument, 0, 'c' },
{ "reset", no_argument, 0, 'f' },
+ { "short", no_argument, 0, 'q' },
{ 0 }
};
@@ -112,9 +153,12 @@ int main(int argc, char** argv) {
bool check = false,
pin = false,
once = false,
- reset = false;
+ reset = false,
+
+ short_ = false;
+
int c;
- while ((c = getopt_long(argc, argv, ":hVvs:r:pocf", long_options, NULL)) > 0) {
+ while ((c = getopt_long(argc, argv, ":hVvqs:r:pocf", long_options, NULL)) > 0) {
switch (c) {
case 'r':
strcpy(runlevel, optarg);
@@ -122,6 +166,9 @@ int main(int argc, char** argv) {
case 's':
service_dir = optarg;
break;
+ case 'q':
+ short_ = true;
+ break;
case 'v':
verbose = true;
break;
@@ -258,6 +305,16 @@ int main(int argc, char** argv) {
return 1;
}
+ if (check)
+ printf("warn: --check specified but not used\n");
+ if (pin)
+ printf("warn: --pin specified but not used\n");
+ if (once)
+ printf("warn: --once specified but not used\n");
+ if (reset)
+ printf("warn: --reset specified but not used\n");
+
+
service_t response[50];
int rc;
@@ -286,7 +343,10 @@ int main(int argc, char** argv) {
}
}
}
- print_service(&response[i], log);
+ if (short_)
+ print_service_short(&response[i], log);
+ else
+ print_service(&response[i], log);
}
}
}
\ No newline at end of file
diff --git a/src/serialize.c b/src/serialize.c
@@ -25,7 +25,7 @@ void service_store(service_t* s, uint8_t* buffer) {
buffer[12] = (s->status_change >> 56) & 0xff;
buffer[13] = (s->fail_count);
buffer[14] = (s->return_code);
- buffer[17] = (s->restart_file << 0) |
+ buffer[15] = (s->restart_file << 0) |
(s->restart_manual << 2);
buffer[16] = (s->last_exit << 0) |
(s->paused << 2) |
diff --git a/src/service.c b/src/service.c
@@ -106,5 +106,9 @@ static bool is_dependency(service_t* d) {
bool service_need_restart(service_t* s) {
if (s->restart_manual == S_FORCE_DOWN)
return is_dependency(s);
- return s->restart_file || s->restart_manual || is_dependency(s);
+ return s->restart_file == S_ONCE ||
+ s->restart_file == S_RESTART ||
+ s->restart_manual == S_ONCE ||
+ s->restart_manual == S_RESTART ||
+ is_dependency(s);
}
\ No newline at end of file