Skip to content
Snippets Groups Projects
Commit 0cb5751b authored by Eric Vidal's avatar Eric Vidal :speech_balloon:
Browse files

reduce HEAP memory usage

parent 122070c3
No related branches found
No related tags found
No related merge requests found
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdarg.h>
#include <oblibs/obgetopt.h> #include <oblibs/obgetopt.h>
#include <oblibs/log.h> #include <oblibs/log.h>
...@@ -32,6 +33,7 @@ ...@@ -32,6 +33,7 @@
#include <skalibs/env.h> #include <skalibs/env.h>
#include <skalibs/bytestr.h>//byte_count #include <skalibs/bytestr.h>//byte_count
#include <skalibs/exec.h> #include <skalibs/exec.h>
#include <skalibs/buffer.h>
#include <s6/config.h> #include <s6/config.h>
#include <execline/config.h> #include <execline/config.h>
...@@ -40,7 +42,6 @@ ...@@ -40,7 +42,6 @@
#include <66/utils.h> #include <66/utils.h>
#include <66/constants.h> #include <66/constants.h>
#define CRASH 0 #define CRASH 0
#define FINISH 1 #define FINISH 1
#define INT 2 #define INT 2
...@@ -65,6 +66,9 @@ static char const *log_user = SS_LOGGER_RUNNER ; ...@@ -65,6 +66,9 @@ static char const *log_user = SS_LOGGER_RUNNER ;
static unsigned int BOOT = 0 ; static unsigned int BOOT = 0 ;
static unsigned int CONTAINER = SS_BOOT_CONTAINER ; static unsigned int CONTAINER = SS_BOOT_CONTAINER ;
static unsigned int CATCH_LOG = SS_BOOT_CATCH_LOG ; static unsigned int CATCH_LOG = SS_BOOT_CATCH_LOG ;
static size_t compute_buf_size(char const *str,...) ;
static size_t CONFIG_STR_LEN = 0 ;
static int BUF_FD ; // general buffer fd
#define USAGE "66-scandir [ -h ] [ -z ] [ -v verbosity ] [ -l live ] [ -b|B ] [ -c ] [ -L log_user ] [ -s skel ] [ -o owner ] create|remove" #define USAGE "66-scandir [ -h ] [ -z ] [ -v verbosity ] [ -l live ] [ -b|B ] [ -c ] [ -L log_user ] [ -s skel ] [ -o owner ] create|remove"
...@@ -200,32 +204,83 @@ static void inline log_perm(char const *str,uid_t *uid,gid_t *gid) ...@@ -200,32 +204,83 @@ static void inline log_perm(char const *str,uid_t *uid,gid_t *gid)
if (!yourgid(gid,*uid)) log_dieusys(LOG_EXIT_SYS,"set gid of: ",str) ; if (!yourgid(gid,*uid)) log_dieusys(LOG_EXIT_SYS,"set gid of: ",str) ;
} }
void shebang(stralloc *sa,char const *opts) void inline shebang(buffer *b, char const *opts)
{ {
if (!auto_stra(sa, "#!" SS_EXECLINE_SHEBANGPREFIX "execlineb ",opts,"\n")) log_flow() ;
log_die_nomem("stralloc") ;
if (!auto_buf(b, "#!" SS_EXECLINE_SHEBANGPREFIX "execlineb ",opts,"\n"))
log_die_nomem("buffer") ;
} }
void append_shutdown(stralloc *sa,char const *live, char const *opts) void append_shutdown(buffer *b, char const *live, char const *opts)
{ {
if (!auto_stra(sa,SS_BINPREFIX "66-shutdown ",opts)) log_flow() ;
log_die_nomem("stralloc") ;
if (!auto_buf(b,SS_BINPREFIX "66-shutdown ",opts))
log_die_nomem("buffer") ;
if (!CONTAINER) if (!CONTAINER)
if (!auto_stra(sa," -a")) if (!auto_buf(b," -a"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
if (!auto_buf(b," -l ",live," -- now\n"))
log_die_nomem("buffer") ;
}
if (!auto_stra(sa," -l ",live," -- now\n")) static size_t compute_buf_size(char const *str,...)
log_die_nomem("stralloc") ; {
va_list alist ;
va_start(alist,str) ;
size_t len = 0 ;
while (str != 0) {
len += strlen(str) ;
str = va_arg(alist, char const *) ;
}
va_end(alist) ;
return len ;
}
static buffer init_buffer(char const *dst, char const *file, size_t len)
{
log_flow() ;
int fd ;
buffer b ;
size_t dstlen = strlen(dst), filen = strlen(file) ;
char w[dstlen + 1 + filen + 1] ;
char buf[len + 1] ;
auto_strings(w, dst, "/", file) ;
fd = open_trunc(w) ;
if (fd < 0 || ndelay_off(fd) < 0)
log_die(LOG_EXIT_SYS,"open trunc") ;
buffer_init(&b,&fd_writev, fd, buf, len) ;
return b ;
}
void write_to_bufnclose(buffer *b, char const *dst, char const *file)
{
if (!buffer_flush(b))
log_dieusys(LOG_EXIT_SYS, "write to: ", dst, "/", file) ;
fd_close(BUF_FD) ;
} }
void write_shutdownd(char const *live, char const *scandir) void write_shutdownd(char const *live, char const *scandir)
{ {
log_flow() ; log_flow() ;
stralloc run = STRALLOC_ZERO ; buffer b ;
size_t blen = compute_buf_size(live, skel, 0) ;
blen += 500 + CONFIG_STR_LEN ;
size_t scandirlen = strlen(scandir) ; size_t scandirlen = strlen(scandir) ;
char shut[scandirlen + 1 + SS_BOOT_SHUTDOWND_LEN + 5 + 1] ; char shut[scandirlen + 1 + SS_BOOT_SHUTDOWND_LEN + 5 + 1] ;
...@@ -237,32 +292,32 @@ void write_shutdownd(char const *live, char const *scandir) ...@@ -237,32 +292,32 @@ void write_shutdownd(char const *live, char const *scandir)
auto_fifo(shut) ; auto_fifo(shut) ;
shebang(&run,"-P") ; shut[scandirlen + 1 + SS_BOOT_SHUTDOWND_LEN] = 0 ;
if (!auto_stra(&run,
b = init_buffer(shut, "run", blen) ;
shebang(&b, "-P") ;
if (!auto_buf(&b,
SS_BINPREFIX "66-shutdownd -l ", SS_BINPREFIX "66-shutdownd -l ",
live," -s ",skel," -g 3000")) live," -s ",skel," -g 3000"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
if (CONTAINER) if (CONTAINER)
if (!auto_stra(&run," -B")) if (!auto_buf(&b," -B"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
if (!CATCH_LOG) if (!CATCH_LOG)
if (!auto_stra(&run," -c")) if (!auto_buf(&b," -c"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
if (!auto_stra(&run,"\n")) if (!auto_buf(&b,"\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
shut[scandirlen + 1 + SS_BOOT_SHUTDOWND_LEN] = 0 ; write_to_bufnclose(&b, shut, "run") ;
auto_file(shut,"run",run.s,run.len) ;
auto_strings(shut + scandirlen + 1 + SS_BOOT_SHUTDOWND_LEN,"/run") ; auto_strings(shut + scandirlen + 1 + SS_BOOT_SHUTDOWND_LEN,"/run") ;
auto_chmod(shut,0755) ; auto_chmod(shut,0755) ;
stralloc_free(&run) ;
} }
void write_bootlog(char const *live, char const *scandir) void write_bootlog(char const *live, char const *scandir)
...@@ -272,14 +327,11 @@ void write_bootlog(char const *live, char const *scandir) ...@@ -272,14 +327,11 @@ void write_bootlog(char const *live, char const *scandir)
int r ; int r ;
uid_t uid ; uid_t uid ;
gid_t gid ; gid_t gid ;
size_t livelen = strlen(live) ; size_t livelen = strlen(live), scandirlen = strlen(scandir), ownerlen = uid_fmt(OWNERSTR,OWNER), loglen = 0, blen = 0 ;
size_t scandirlen = strlen(scandir) ; buffer b ;
size_t ownerlen = uid_fmt(OWNERSTR,OWNER) ;
size_t loglen ;
char path[livelen + 4 + ownerlen + 1] ; char path[livelen + 4 + ownerlen + 1] ;
char logdir[scandirlen + SS_SCANDIR_LEN + SS_LOG_SUFFIX_LEN + 1 + 5 + 1] ; char logdir[scandirlen + SS_SCANDIR_LEN + SS_LOG_SUFFIX_LEN + 1 + 5 + 1] ;
stralloc run = STRALLOC_ZERO ;
/** run/66/scandir/uid_name/scandir-log */ /** run/66/scandir/uid_name/scandir-log */
auto_strings(logdir,scandir,"/" SS_SCANDIR SS_LOG_SUFFIX) ; auto_strings(logdir,scandir,"/" SS_SCANDIR SS_LOG_SUFFIX) ;
...@@ -308,62 +360,71 @@ void write_bootlog(char const *live, char const *scandir) ...@@ -308,62 +360,71 @@ void write_bootlog(char const *live, char const *scandir)
auto_chmod(path,02755) ; auto_chmod(path,02755) ;
logdir[loglen] = 0 ;
blen = compute_buf_size(live, logdir, log_user, path, 0) ;
blen += 500 + CONFIG_STR_LEN;
b = init_buffer(logdir, "run", blen) ;
/** make run file */ /** make run file */
shebang(&run,"-P") ; shebang(&b,"-P") ;
if (CONTAINER) { if (CONTAINER) {
if (!auto_stra(&run,EXECLINE_BINPREFIX "fdmove -c 1 2\n")) if (!auto_buf(&b,EXECLINE_BINPREFIX "fdmove -c 1 2\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
} else { } else {
if (!auto_stra(&run, if (!auto_buf(&b,
EXECLINE_BINPREFIX "redirfd -w 1 /dev/null\n")) EXECLINE_BINPREFIX "redirfd -w 1 /dev/null\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
} }
if (!auto_stra(&run, if (!auto_buf(&b,
EXECLINE_BINPREFIX "redirfd -rnb 0 fifo\n" \ EXECLINE_BINPREFIX "redirfd -rnb 0 fifo\n" \
S6_BINPREFIX "s6-setuidgid ", S6_BINPREFIX "s6-setuidgid ",
log_user, log_user,
"\n" S6_BINPREFIX "s6-log -bpd3 -- 1")) "\n" S6_BINPREFIX "s6-log -bpd3 -- 1"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
if (SS_LOGGER_TIMESTAMP < TIME_NONE) if (SS_LOGGER_TIMESTAMP < TIME_NONE)
if (!auto_stra(&run,SS_LOGGER_TIMESTAMP == TIME_ISO ? " T " : " t ")) if (!auto_buf(&b, SS_LOGGER_TIMESTAMP == TIME_ISO ? " T " : " t "))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
if (!auto_stra(&run,path,"\n")) if (!auto_buf(&b,path,"\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
logdir[loglen] = 0 ; write_to_bufnclose(&b, logdir, "run") ;
auto_file(logdir,"run",run.s,run.len) ;
auto_file(logdir,"notification-fd","3\n",2) ; auto_file(logdir,"notification-fd","3\n",2) ;
auto_strings(logdir + loglen,"/run") ; auto_strings(logdir + loglen,"/run") ;
auto_chmod(logdir,0755) ; auto_chmod(logdir,0755) ;
stralloc_free(&run) ;
} }
void write_control(char const *scandir,char const *live, char const *filename, int file) void write_control(char const *scandir,char const *live, char const *filename, int file)
{ {
log_flow() ; log_flow() ;
size_t scandirlen = strlen(scandir) ; buffer b ;
size_t filen = strlen(filename) ; size_t scandirlen = strlen(scandir), filen = strlen(filename), blen = 0 ;
char mode[scandirlen + SS_SVSCAN_LOG_LEN + filen + 1] ; char mode[scandirlen + SS_SVSCAN_LOG_LEN + filen + 1] ;
stralloc sa = STRALLOC_ZERO ;
shebang(&sa,"-P") ; auto_strings(mode,scandir,SS_SVSCAN_LOG) ;
blen = compute_buf_size(live, scandir, 0) ;
blen += 500 + CONFIG_STR_LEN ;
b = init_buffer(mode, filename + 1, blen) ;
shebang(&b,"-P") ;
if (file == FINISH) if (file == FINISH)
{ {
if (CONTAINER) { if (CONTAINER) {
if (!auto_stra(&sa, if (!auto_buf(&b,
SS_BINPREFIX "execl-envfile ",live, SS_BOOT_CONTAINER_DIR "/",OWNERSTR,"\n" \ SS_BINPREFIX "execl-envfile ",live, SS_BOOT_CONTAINER_DIR "/",OWNERSTR,"\n" \
EXECLINE_BINPREFIX "fdclose 1\n" \ EXECLINE_BINPREFIX "fdclose 1\n" \
EXECLINE_BINPREFIX "fdclose 2\n" \ EXECLINE_BINPREFIX "fdclose 2\n" \
...@@ -371,25 +432,25 @@ void write_control(char const *scandir,char const *live, char const *filename, i ...@@ -371,25 +432,25 @@ void write_control(char const *scandir,char const *live, char const *filename, i
EXECLINE_BINPREFIX "foreground {\n" \ EXECLINE_BINPREFIX "foreground {\n" \
SS_BINPREFIX "66-hpr -f -n -${HALTCODE} -l ",live," \n}\n" \ SS_BINPREFIX "66-hpr -f -n -${HALTCODE} -l ",live," \n}\n" \
EXECLINE_BINPREFIX "exit ${EXITCODE}\n")) EXECLINE_BINPREFIX "exit ${EXITCODE}\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
} else if (BOOT) { } else if (BOOT) {
if (!auto_stra(&sa, if (!auto_buf(&b,
EXECLINE_BINPREFIX "redirfd -w 2 /dev/console\n" \ EXECLINE_BINPREFIX "redirfd -w 2 /dev/console\n" \
EXECLINE_BINPREFIX "fdmove -c 1 2\n" \ EXECLINE_BINPREFIX "fdmove -c 1 2\n" \
EXECLINE_BINPREFIX "foreground { " SS_BINPREFIX "66-echo -- \"scandir ", EXECLINE_BINPREFIX "foreground { " SS_BINPREFIX "66-echo -- \"scandir ",
scandir," exited. Rebooting.\" }\n" \ scandir," exited. Rebooting.\" }\n" \
SS_BINPREFIX "66-hpr -r -f -l ", SS_BINPREFIX "66-hpr -r -f -l ",
live,"\n")) live,"\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
} else { } else {
if (!auto_stra(&sa, if (!auto_buf(&b,
SS_BINPREFIX "66-echo -- \"scandir ", SS_BINPREFIX "66-echo -- \"scandir ",
scandir," shutted down...\"\n")) scandir," shutted down...\"\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
} }
goto write ; goto write ;
} }
...@@ -399,7 +460,7 @@ void write_control(char const *scandir,char const *live, char const *filename, i ...@@ -399,7 +460,7 @@ void write_control(char const *scandir,char const *live, char const *filename, i
if (CONTAINER) { if (CONTAINER) {
if (!auto_stra(&sa, if (!auto_buf(&b,
EXECLINE_BINPREFIX "foreground {\n" \ EXECLINE_BINPREFIX "foreground {\n" \
EXECLINE_BINPREFIX "fdmove -c 1 2\n" \ EXECLINE_BINPREFIX "fdmove -c 1 2\n" \
SS_BINPREFIX "66-echo \"scandir crashed. Killing everythings and exiting.\"\n}\n" \ SS_BINPREFIX "66-echo \"scandir crashed. Killing everythings and exiting.\"\n}\n" \
...@@ -407,37 +468,37 @@ void write_control(char const *scandir,char const *live, char const *filename, i ...@@ -407,37 +468,37 @@ void write_control(char const *scandir,char const *live, char const *filename, i
EXECLINE_BINPREFIX "66-nuke\n}\n" \ EXECLINE_BINPREFIX "66-nuke\n}\n" \
EXECLINE_BINPREFIX "wait { }\n" \ EXECLINE_BINPREFIX "wait { }\n" \
SS_BINPREFIX "66-hpr -f -n -p -l ",live,"\n")) SS_BINPREFIX "66-hpr -f -n -p -l ",live,"\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
} }
else { else {
if (!auto_stra(&sa, if (!auto_buf(&b,
EXECLINE_BINPREFIX "redirfd -w 2 /dev/console\n" \ EXECLINE_BINPREFIX "redirfd -w 2 /dev/console\n" \
EXECLINE_BINPREFIX "fdmove -c 1 2\n" \ EXECLINE_BINPREFIX "fdmove -c 1 2\n" \
EXECLINE_BINPREFIX "foreground { " SS_BINPREFIX "66-echo -- \"scandir ", EXECLINE_BINPREFIX "foreground { " SS_BINPREFIX "66-echo -- \"scandir ",
scandir, " crashed.")) scandir, " crashed."))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
if (BOOT) { if (BOOT) {
if (!auto_stra(&sa, if (!auto_buf(&b,
" Rebooting.\" }\n" \ " Rebooting.\" }\n" \
SS_BINPREFIX "66-hpr -r -f -l ", SS_BINPREFIX "66-hpr -r -f -l ",
live,"\n")) live,"\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
} else if (!auto_stra(&sa,"\" }\n")) } else if (!auto_buf(&b,"\" }\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
} }
goto write ; goto write ;
} }
if (!BOOT) { if (!BOOT) {
if (!auto_stra(&sa, if (!auto_buf(&b,
EXECLINE_BINPREFIX "foreground { " SS_BINPREFIX "66-all -v3 -l ", EXECLINE_BINPREFIX "foreground { " SS_BINPREFIX "66-all -v3 -l ",
live," down }\n")) live," down }\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
} }
...@@ -447,33 +508,33 @@ void write_control(char const *scandir,char const *live, char const *filename, i ...@@ -447,33 +508,33 @@ void write_control(char const *scandir,char const *live, char const *filename, i
case USR1: case USR1:
if (BOOT) if (BOOT)
append_shutdown(&sa,live,"-p") ; append_shutdown(&b,live,"-p") ;
break ; break ;
case USR2: case USR2:
if (BOOT) if (BOOT)
append_shutdown(&sa,live,"-h") ; append_shutdown(&b,live,"-h") ;
break ; break ;
case TERM: case TERM:
if (!BOOT) if (!BOOT)
if (!auto_stra(&sa, SS_BINPREFIX "66-scanctl -l ",live," stop\n")) if (!auto_buf(&b, SS_BINPREFIX "66-scanctl -l ",live," stop\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
break ; break ;
case QUIT: case QUIT:
if (!BOOT) if (!BOOT)
if (!auto_stra(&sa, SS_BINPREFIX "66-scanctl -l ",live," quit\n")) if (!auto_buf(&b, SS_BINPREFIX "66-scanctl -l ",live," quit\n"))
log_die_nomem("stralloc") ; log_die_nomem("buffer") ;
break ; break ;
case INT: case INT:
if (BOOT) if (BOOT)
append_shutdown(&sa,live,"-r") ; append_shutdown(&b,live,"-r") ;
break ; break ;
...@@ -485,15 +546,12 @@ void write_control(char const *scandir,char const *live, char const *filename, i ...@@ -485,15 +546,12 @@ void write_control(char const *scandir,char const *live, char const *filename, i
} }
write: write:
auto_strings(mode,scandir,SS_SVSCAN_LOG) ;
auto_file(mode,filename+1,sa.s,sa.len) ; write_to_bufnclose(&b, mode, filename + 1) ;
auto_strings(mode + scandirlen + SS_SVSCAN_LOG_LEN, filename) ; auto_strings(mode + scandirlen + SS_SVSCAN_LOG_LEN, filename) ;
auto_chmod(mode,0755) ; auto_chmod(mode,0755) ;
stralloc_free(&sa) ;
} }
void create_scandir(char const *live, char const *scandir) void create_scandir(char const *live, char const *scandir)
...@@ -576,6 +634,8 @@ int main(int argc, char const *const *argv, char const *const *envp) ...@@ -576,6 +634,8 @@ int main(int argc, char const *const *argv, char const *const *envp)
log_color = &log_color_disable ; log_color = &log_color_disable ;
CONFIG_STR_LEN = compute_buf_size(SS_BINPREFIX, SS_EXTBINPREFIX, SS_EXTLIBEXECPREFIX, SS_LIBEXECPREFIX, SS_EXECLINE_SHEBANGPREFIX, 0) ;
cmd = create = remove = 0 ; cmd = create = remove = 0 ;
OWNER = MYUID ; OWNER = MYUID ;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment