Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 36 additions & 14 deletions src/librc/librc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1041,30 +1041,52 @@ rc_service_value_get(const char *service, const char *option)
return buffer;
}

bool
rc_service_value_set(const char *service, const char *option, const char *value)
static inline int
open_optiondir(const char *service)
{
bool ret = true;
int optdirfd;
if (mkdirat(rc_dirfd(RC_DIR_OPTIONS), service, 0755) != 0 && errno != EEXIST)
return -1;

return openat(rc_dirfd(RC_DIR_OPTIONS), service, O_RDONLY | O_DIRECTORY);
}

RC_PRINTF(3, 4) bool
rc_service_value_fmt(const char *service, const char *option, const char *fmt, ...)
{
int optdirfd = open_optiondir(service);
va_list ap;
FILE *fp;

if (mkdirat(rc_dirfd(RC_DIR_OPTIONS), service, 0755) != 0 && errno != EEXIST)
if (optdirfd == -1)
return false;

if ((optdirfd = openat(rc_dirfd(RC_DIR_OPTIONS), service, O_RDONLY | O_DIRECTORY)) == -1)
if (!(fp = do_fopenat(optdirfd, option, O_WRONLY | O_CREAT | O_TRUNC))) {
close(optdirfd);
return false;
}

if (!value) {
unlinkat(optdirfd, option, 0);
} else if ((fp = do_fopenat(optdirfd, option, O_WRONLY | O_CREAT | O_TRUNC))) {
fprintf(fp, "%s", value);
fclose(fp);
} else {
ret = false;
va_start(ap, fmt);
vfprintf(fp, fmt, ap);
va_end(ap);

fclose(fp);
return true;
}

bool
rc_service_value_set(const char *service, const char *option, const char *value)
{
int optdirfd;

if (value) {
return rc_service_value_fmt(service, option, "%s", value);
}

if ((optdirfd = open_optiondir(service)) == -1)
return false;
unlinkat(optdirfd, option, 0);
close(optdirfd);
return ret;
return true;
}

bool
Expand Down
8 changes: 8 additions & 0 deletions src/librc/rc.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@ bool rc_service_started_daemon(const char *, const char *,
* @return saved value */
char *rc_service_value_get(const char *, const char *);

/*! Format and save a persistent value for a service
* @param service to save for
* @param option to save
* @param format string
* @return true if saved, otherwise false */
__attribute__((__format__(__printf__, 3, 4)))
bool rc_service_value_fmt(const char *, const char *, const char *, ...);

/*! Save a persistent value for a service
* @param service to save for
* @param option to save
Expand Down
16 changes: 16 additions & 0 deletions src/shared/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ RC_UNUSED static void *xrealloc(void *ptr, size_t size)
/* NOTREACHED */
}

RC_UNUSED static char *xstrndup(const char *str, size_t n)
{
char *value;

if (!str)
return (NULL);

value = strndup(str, n);

if (value)
return (value);

ERRX;
/* NOTREACHED */
}

RC_UNUSED static char *xstrdup(const char *str)
{
char *value;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/misc.c
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ exec_service(const char *service, const char *arg)
}

int
parse_mode(mode_t *mode, char *text)
parse_mode(mode_t *mode, const char *text)
{
char *p;
unsigned long l;
Expand Down
2 changes: 1 addition & 1 deletion src/shared/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ int is_writable(const char *);
#define service_start(service) exec_service(service, "start")
#define service_stop(service) exec_service(service, "stop")

int parse_mode(mode_t *, char *);
int parse_mode(mode_t *, const char *);

/* Handy function so we can wrap einfo around our deptree */
RC_DEPTREE *_rc_deptree_load (int, int *);
Expand Down
4 changes: 2 additions & 2 deletions src/shared/rc_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,13 @@ int rc_waitpid(pid_t pid)
return status;
}

int rc_pipe_command(const char *cmd, int devnullfd)
int rc_pipe_command(const char *cmd)
{
const char *argv[] = { "/bin/sh", "-c", cmd, NULL };
struct exec_result res;
struct exec_args args = exec_init(argv);
args.redirect_stdin = EXEC_MKPIPE;
args.redirect_stdout = args.redirect_stderr = devnullfd;
args.redirect_stdout = args.redirect_stderr = EXEC_DEVNULL;
res = do_exec(&args);
return (res.pid > 0) ? res.proc_stdin : -1;
}
2 changes: 1 addition & 1 deletion src/shared/rc_exec.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,6 @@ struct exec_result do_exec(struct exec_args *args);

/* some exec related helplers */
int rc_waitpid(pid_t pid);
int rc_pipe_command(const char *cmd, int devnullfd);
int rc_pipe_command(const char *cmd);

#endif
3 changes: 3 additions & 0 deletions src/shared/timeutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ int64_t parse_duration(const char *duration)
char *end;
long val;

if (!duration)
return 0;

errno = 0;
val = strtol(duration, &end, 10);
if (errno == ERANGE || *duration == '\0' || val < 0)
Expand Down
4 changes: 2 additions & 2 deletions src/start-stop-daemon/start-stop-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -1085,7 +1085,7 @@ int main(int argc, char **argv)
" for stdout `%s': %s",
applet, redirect_stdout, strerror(errno));
}else if (stdout_process) {
stdout_fd = rc_pipe_command(stdout_process, devnull_fd);
stdout_fd = rc_pipe_command(stdout_process);
if (stdout_fd == -1)
eerrorx("%s: unable to open the logging process"
" for stdout `%s': %s",
Expand All @@ -1099,7 +1099,7 @@ int main(int argc, char **argv)
" for stderr `%s': %s",
applet, redirect_stderr, strerror(errno));
}else if (stderr_process) {
stderr_fd = rc_pipe_command(stderr_process, devnull_fd);
stderr_fd = rc_pipe_command(stderr_process);
if (stderr_fd == -1)
eerrorx("%s: unable to open the logging process"
" for stderr `%s': %s",
Expand Down
Loading