71 lines
1.6 KiB
C
71 lines
1.6 KiB
C
#ifndef __STDLIB_EXIT_H__
|
|
#define __STDLIB_EXIT_H__
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <sys/ksys.h>
|
|
|
|
#define __PATH_TO_STATUS_FILE "/tmp0/1/"
|
|
#define __STATUS_FILE_EXTENSION ".status"
|
|
#define __STATUS_FILE_FORMAT "%d"
|
|
#define __FULL_STATUS_FILE_NAME __PATH_TO_STATUS_FILE "%d" __STATUS_FILE_EXTENSION
|
|
#define __FULL_STATUS_FILE_NAME_SIZE (sizeof(__PATH_TO_STATUS_FILE) + 32 + sizeof(__STATUS_FILE_EXTENSION))
|
|
|
|
void __libc_exit(int status, void (*before_exit)(int status));
|
|
|
|
// Save exit code
|
|
inline void WRITE_EXIT_CODE(int status)
|
|
{
|
|
char buff[__FULL_STATUS_FILE_NAME_SIZE];
|
|
ksys_thread_t t;
|
|
|
|
_ksys_thread_info(&t, -1);
|
|
snprintf(buff, sizeof(buff), __FULL_STATUS_FILE_NAME, t.pid);
|
|
|
|
FILE* f = fopen(buff, "w");
|
|
|
|
if (f) {
|
|
snprintf(buff, sizeof(buff), __STATUS_FILE_FORMAT, status);
|
|
fputs(buff, f);
|
|
fflush(f);
|
|
fclose(f);
|
|
} else {
|
|
_ksys_debug_puts("error while write status\n");
|
|
}
|
|
}
|
|
|
|
// Read exit code
|
|
inline int READ_EXIT_CODE(int pid, ksys_thread_t *t)
|
|
{
|
|
char buff[__FULL_STATUS_FILE_NAME_SIZE];
|
|
int status = 0;
|
|
bool free_t = false;
|
|
|
|
if(!t)
|
|
{
|
|
t = malloc(sizeof(ksys_thread_t));
|
|
_ksys_thread_info(t, -1);
|
|
free_t = true;
|
|
}
|
|
snprintf(buff, sizeof(buff), __FULL_STATUS_FILE_NAME, t->pid);
|
|
|
|
FILE* f = fopen(buff, "r");
|
|
if (f) {
|
|
fscanf(f, __STATUS_FILE_FORMAT, &status);
|
|
fclose(f);
|
|
}
|
|
else if(t && t->slot_state == 4) // it was stopped before it created status file
|
|
{
|
|
status = -1;
|
|
}
|
|
|
|
if(free_t)
|
|
{
|
|
free(t);
|
|
}
|
|
|
|
return status;
|
|
}
|
|
|
|
#endif // __STDLIB_EXIT_H__
|