fiss

Friedel's Initialization and Service Supervision
Log | Files | Refs | LICENSE

encode.c (2747B)


      1 #include "defs.h"
      2 
      3 #include <stdlib.h>
      4 #include <string.h>
      5 
      6 const char* state_name(state_t state) {
      7 	switch (state) {
      8 		case STATE_INACTIVE:
      9 			return "inactive";
     10 		case STATE_DEPENDENCY:
     11 			return "waiting for dependenies";
     12 		case STATE_SETUP:
     13 			return "setup";
     14 		case STATE_STARTING:
     15 			return "starting";
     16 		case STATE_ACTIVE_DUMMY:
     17 			return "active (dummy)";
     18 		case STATE_ACTIVE_FOREGROUND:
     19 			return "active (foreground)";
     20 		case STATE_ACTIVE_BACKGROUND:
     21 			return "active (background)";
     22 		case STATE_STOPPING:
     23 			return "stopping";
     24 		case STATE_FINISHING:
     25 			return "finishing";
     26 		default:
     27 			return "unknown";
     28 	}
     29 }
     30 
     31 void store_status(serial_t* buffer, char* stat_buffer, char* pid_buffer) {
     32 	u64 tai = (u64) service.state_change + 4611686018427387914llu;
     33 
     34 	stat_buffer[0] = '\0';
     35 	pid_buffer[0]  = '\0';
     36 
     37 	// lower-endian
     38 	buffer->status_change[0] = (tai >> 56) & 0xff;
     39 	buffer->status_change[1] = (tai >> 48) & 0xff;
     40 	buffer->status_change[2] = (tai >> 40) & 0xff;
     41 	buffer->status_change[3] = (tai >> 32) & 0xff;
     42 	buffer->status_change[4] = (tai >> 24) & 0xff;
     43 	buffer->status_change[5] = (tai >> 16) & 0xff;
     44 	buffer->status_change[6] = (tai >> 8) & 0xff;
     45 	buffer->status_change[7] = (tai >> 0) & 0xff;
     46 
     47 	// big-endian (network)
     48 	buffer->pid[0] = (service.pid >> 0) & 0xff;
     49 	buffer->pid[1] = (service.pid >> 8) & 0xff;
     50 	buffer->pid[2] = (service.pid >> 16) & 0xff;
     51 	buffer->pid[3] = (service.pid >> 24) & 0xff;
     52 
     53 	buffer->state      = service.state;
     54 	buffer->fail_count = service.death_count;
     55 
     56 	buffer->flags = ((service.stop_timeout != 0) << 4) |
     57 	                ((service.should_restart) << 3) |
     58 	                ((service.should_restart) << 2);
     59 
     60 	buffer->paused     = service.paused;
     61 	buffer->restart    = need_restart() ? 'u' : 'd';
     62 	buffer->force_down = 0;
     63 
     64 	if (service.death_last == 0) {
     65 		buffer->return_code = 0;
     66 		buffer->flags |= 0;
     67 	} else if (WIFEXITED(service.death_last)) {
     68 		buffer->return_code = WEXITSTATUS(service.death_last);
     69 		buffer->flags |= 1;
     70 	} else {
     71 		buffer->return_code = WTERMSIG(service.death_last);
     72 		buffer->flags |= 2;
     73 	}
     74 
     75 	switch (service.state) {
     76 		case STATE_INACTIVE:
     77 			buffer->state_runit = 0;    // inactive
     78 			break;
     79 		case STATE_DEPENDENCY:
     80 		case STATE_SETUP:
     81 		case STATE_STARTING:
     82 		case STATE_ACTIVE_DUMMY:
     83 		case STATE_ACTIVE_FOREGROUND:
     84 		case STATE_ACTIVE_BACKGROUND:
     85 		case STATE_ACTIVE_PID:
     86 			buffer->state_runit = 1;    // running
     87 			break;
     88 		case STATE_STOPPING:
     89 		case STATE_FINISHING:
     90 			buffer->state_runit = 2;    // finishing
     91 			break;
     92 	}
     93 
     94 
     95 	strcpy(stat_buffer, state_name(service.state));
     96 	if (service.desired_state) {
     97 		strcat(stat_buffer, ", wants ");
     98 		strcat(stat_buffer, state_name(service.desired_state));
     99 	}
    100 	if (service.paused)
    101 		strcat(stat_buffer, ", is paused");
    102 }