Skip to content

Commit febc578

Browse files
committed
add option to change working directory
1 parent dbaaed4 commit febc578

File tree

4 files changed

+19
-3
lines changed

4 files changed

+19
-3
lines changed

src/conf_gram.y

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ typedef void* yyscan_t;
5050
%parse-param { conf_t *conf }
5151
%parse-param { yyscan_t scanner }
5252

53-
%token OPENBRACE ENDBRACE TOKENTASK TOKENNAME TOKENPATH TOKENARGS TOKENENV SEMICOLON QUOTE
53+
%token OPENBRACE ENDBRACE TOKENTASK TOKENNAME TOKENPATH TOKENARGS TOKENENV TOKENDIR SEMICOLON QUOTE
5454

5555
%union
5656
{
@@ -113,8 +113,8 @@ taskopt:
113113
args
114114
|
115115
env
116-
{
117-
}
116+
|
117+
dir
118118
;
119119

120120
name:
@@ -195,4 +195,11 @@ env:
195195
}
196196
;
197197

198+
dir:
199+
TOKENDIR PATH SEMICOLON
200+
{
201+
check_task();
202+
task->dir = $2;
203+
};
204+
198205
%%

src/conf_scan.l

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ name return TOKENNAME;
2020
path return TOKENPATH;
2121
args return TOKENARGS;
2222
env return TOKENENV;
23+
dir return TOKENDIR;
2324
\" return QUOTE;
2425
\; return SEMICOLON;
2526
{number}+ yylval->string=strdup(yytext); return NUMBER;

src/task.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ int task_init(task_t *task, context_t *ctx)
4848
task->ctx = ctx;
4949
task->path = NULL;
5050
task->name = NULL;
51+
task->dir = NULL;
5152
task->args = NULL;
5253
task->args_len = 0;
5354
task->args_total = 0;
@@ -130,6 +131,12 @@ int task_run(task_t *task)
130131
switch(pid) {
131132
case 0:
132133
task_redirect_io(task);
134+
if (task->dir && chdir(task->dir) < 0) {
135+
bz_log_error(task->ctx->log,
136+
"change working directory failed, task: %s, error: %s",
137+
task->name, strerror(errno));
138+
_exit(ERROR);
139+
}
133140
ret = execv(task->path, task->args);
134141
if (ret < 0) {
135142
bz_log_error(task->ctx->log,

src/task.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ typedef struct task_s {
3838
char *log_path;
3939
const char *path;
4040
const char *name;
41+
const char *dir;
4142
char **args;
4243
size_t args_len;
4344
size_t args_total;

0 commit comments

Comments
 (0)