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

simplifies its.

Directories are handled by sanitize_write
parent f48ca627
No related branches found
No related tags found
No related merge requests found
......@@ -18,12 +18,12 @@
#include <stdint.h>
#include <66/service.h>
extern int write_services(resolve_service_t *res, char const *workdir, uint8_t force) ;
extern void write_classic(resolve_service_t *res, char const *dst, uint8_t force) ;
extern void write_services(resolve_service_t *res, char const *workdir) ;
extern void write_classic(resolve_service_t *res, char const *dst) ;
extern void write_common(resolve_service_t *res, char const *dst) ;
extern void write_environ(char const *name, char const *contents, char const *dst) ;
extern void write_execute_scripts(char const *file, char const *contents, char const *dst) ;
extern void write_logger(resolve_service_t *res, char const *destination, uint8_t force) ;
extern void write_logger(resolve_service_t *res, char const *destination) ;
extern void write_oneshot(resolve_service_t *res, char const *dst) ;
extern void write_uint(char const *dst, char const *name, uint32_t ui) ;
......
......@@ -22,7 +22,7 @@
/* dst e.g. /var/lib/66/system/<tree>/servicedirs/svc/<name> */
void write_classic(resolve_service_t *res, char const *dst, uint8_t force)
void write_classic(resolve_service_t *res, char const *dst)
{
log_flow() ;
......@@ -42,16 +42,4 @@ void write_classic(resolve_service_t *res, char const *dst, uint8_t force)
/** finish.user file */
if (res->execute.finish.run_user)
write_execute_scripts("finish.user", res->sa.s + res->execute.finish.run_user, dst) ;
/** logger */
if (res->logger.name) {
char destination[strlen(dst) + 1] ;
if (!ob_dirname(destination, dst))
log_dieu(LOG_EXIT_SYS, "get dirname of: ", dst) ;
write_logger(res, destination, force) ;
}
}
......@@ -40,49 +40,31 @@
/** @destination -> /var/lib/66/system/<tree>/servicedirs/svc/ */
void write_logger(resolve_service_t *res, char const *destination, uint8_t force)
void write_logger(resolve_service_t *res, char const *destination)
{
log_flow() ;
int r ;
uid_t log_uid ;
gid_t log_gid ;
uint8_t owner = res->owner ;
char *logrunner = res->logger.execute.run.runas ? res->sa.s + res->logger.execute.run.runas : SS_LOGGER_RUNNER ;
char *logrunner = res->execute.run.runas ? res->sa.s + res->execute.run.runas : SS_LOGGER_RUNNER ;
char dst[strlen(destination) + strlen(res->sa.s + res->logger.name) + 1] ;
auto_strings(dst, destination, res->sa.s + res->logger.name) ;
if (res->execute.timeout.kill)
write_uint(destination, "timeout-kill", res->execute.timeout.kill) ;
r = scan_mode(dst, S_IFDIR) ;
if (r && force) {
if (!dir_rm_rf(dst))
log_dieusys(LOG_EXIT_SYS, "delete: ", dst) ;
} else if (r) {
log_warn("ignoring ", dst, " -- already exist") ;
return ;
}
if (!dir_create_parent(dst, 0755))
log_dieusys(LOG_EXIT_SYS, "create directory: ", dst) ;
if (res->logger.execute.timeout.kill)
write_uint(dst, "timeout-kill", res->logger.execute.timeout.kill) ;
if (res->logger.execute.timeout.finish)
write_uint(dst, "timeout-finish", res->logger.execute.timeout.finish) ;
if (res->execute.timeout.finish)
write_uint(destination, "timeout-finish", res->execute.timeout.finish) ;
/** notification */
write_uint(dst, "notification-fd", 3) ;
write_uint(destination, "notification-fd", 3) ;
/** log destination */
log_trace("create directory: ", res->sa.s + res->logger.destination) ;
if (!dir_create_parent(res->sa.s + res->logger.destination, 0755))
log_dieusys(LOG_EXIT_SYS, "create directory: ", res->sa.s + res->logger.destination) ;
if (!owner && ((res->logger.execute.run.build == BUILD_AUTO) || (!res->logger.execute.run.build))) {
if (!owner && ((res->execute.run.build == BUILD_AUTO) || (!res->execute.run.build))) {
if (!youruid(&log_uid, logrunner) || !yourgid(&log_gid, log_uid))
log_dieusys(LOG_EXIT_SYS, "get uid and gid of: ", logrunner) ;
......@@ -91,22 +73,22 @@ void write_logger(resolve_service_t *res, char const *destination, uint8_t force
log_dieusys(LOG_EXIT_SYS, "chown: ", res->sa.s + res->logger.destination) ;
}
char write[strlen(dst) + 10] ;
char write[strlen(destination) + 10] ;
/** run script */
if (!file_write_unsafe(dst, "run", res->sa.s + res->logger.execute.run.run, strlen(res->sa.s + res->logger.execute.run.run)))
log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ;
if (!file_write_unsafe(destination, "run", res->sa.s + res->execute.run.run, strlen(res->sa.s + res->execute.run.run)))
log_dieusys(LOG_EXIT_SYS, "write: ", destination, "/run.user") ;
auto_strings(write, dst, "/run") ;
auto_strings(write, destination, "/run") ;
if (chmod(write, 0755) < 0)
log_dieusys(LOG_EXIT_SYS, "chmod", write) ;
/** run.user script */
if (!file_write_unsafe(dst, "run.user", res->sa.s + res->logger.execute.run.run_user, strlen(res->sa.s + res->logger.execute.run.run_user)))
log_dieusys(LOG_EXIT_SYS, "write: ", dst, "/run.user") ;
if (!file_write_unsafe(destination, "run.user", res->sa.s + res->execute.run.run_user, strlen(res->sa.s + res->execute.run.run_user)))
log_dieusys(LOG_EXIT_SYS, "write: ", destination, "/run.user") ;
auto_strings(write, dst, "/run.user") ;
auto_strings(write, destination, "/run.user") ;
if (chmod(write, 0755) < 0)
log_dieusys(LOG_EXIT_SYS, "chmod", write) ;
......
......@@ -27,6 +27,7 @@
#include <66/enum.h>
#include <66/write.h>
#include <66/constants.h>
#include <66/sanitize.h>
/** @Return 0 on fail
* @Return 1 on success
......@@ -34,35 +35,16 @@
*
* @workdir -> /var/lib/66/system/<tree>/servicedirs/
* */
int write_services(resolve_service_t *res, char const *workdir, uint8_t force)
void write_services(resolve_service_t *res, char const *workdir)
{
log_flow() ;
int r ;
size_t workdirlen = strlen(workdir) ;
char *name = res->sa.s + res->name ;
size_t namelen = strlen(name) ;
int type = res->type ;
{
resolve_service_t fres = RESOLVE_SERVICE_ZERO ;
resolve_wrapper_t_ref wres = resolve_set_struct(DATA_SERVICE, &fres) ;
ss_state_t ste = STATE_ZERO ;
if (resolve_check(workdir, name)) {
if (!resolve_read(wres, workdir, name))
log_dieu(LOG_EXIT_SYS, "read resolve file of: ", name) ;
if (state_check(fres.sa.s + fres.path.home, name)) {
if (!state_read(&ste, fres.sa.s + fres.path.home, name))
log_dieu(LOG_EXIT_SYS, "read state file of: ", name) ;
if (fres.type != type && FLAGS_ISSET(ste.isenabled, STATE_FLAGS_TRUE))
log_die(LOG_EXIT_SYS, "Detection of incompatible type format for: ", name, " -- current: ", get_key_by_enum(ENUM_TYPE, type), " previous: ", get_key_by_enum(ENUM_TYPE, fres.type)) ;
}
}
resolve_free(wres) ;
}
uint32_t type = res->type ;
char logname = get_rstrlen_until(name, SS_LOG_SUFFIX) ;
if (logname > 0)
type = 4 ;
/**
*
*
......@@ -74,28 +56,8 @@ int write_services(resolve_service_t *res, char const *workdir, uint8_t force)
*
*
* */
char wname[workdirlen + SS_SVC_LEN + 1 + namelen + 1] ;
auto_strings(wname, workdir, SS_SVC, "/", name) ;
r = scan_mode(wname, S_IFDIR) ;
if (r < 0)
log_die(LOG_EXIT_SYS, "unvalide source: ", wname) ;
if ((r && force) || !r) {
if (!dir_rm_rf(wname))
log_dieusys(LOG_EXIT_SYS, "remove: ", wname) ;
if (!dir_create_parent(wname, 0755))
log_dieusys(LOG_EXIT_SYS, "create ", wname) ;
} else if (r && !force) {
log_info("Ignoring: ", name, " service: already written") ;
return 2 ;
}
log_trace("Write service ", name, " ...") ;
log_trace("write service ", name) ;
switch(type) {
......@@ -105,19 +67,23 @@ int write_services(resolve_service_t *res, char const *workdir, uint8_t force)
case TYPE_CLASSIC:
write_classic(res, wname, force) ;
write_classic(res, workdir) ;
break ;
case TYPE_ONESHOT:
write_oneshot(res, wname) ;
write_oneshot(res, workdir) ;
break ;
case 4:
write_logger(res, workdir) ;
break ;
default:
log_die(LOG_EXIT_SYS, "unkown type: ", get_key_by_enum(ENUM_TYPE, type)) ;
}
return 1 ;
}
......
......@@ -15,6 +15,7 @@
#include <stdint.h>
#include <oblibs/log.h>
#include <oblibs/string.h>
#include <oblibs/files.h>
#include <skalibs/types.h>
......
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