Repository navigation
Expand file tree
/
Copy pathstatus.c
More file actions
116 lines (98 loc) · 2.24 KB
/
Copy pathstatus.c
File metadata and controls
116 lines (98 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
* status.c
* run external status scripts
*/
#include <err.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <pwd.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "status.h"
/* globals */
int status_stdin_pipe[2];
void
start_log_filter(int safe) {
char *argv[8];
char *awk_script, *awk_script_path;
struct passwd *pw;
awk_script = getenv("ENTR_STATUS_SCRIPT");
if ((!awk_script) || (strlen(awk_script) == 0)) {
pw = getpwuid(getuid());
if (pw == NULL)
errx(1, "getpwuid");
asprintf(&awk_script, "%s/.entr/status.awk", pw->pw_dir);
}
awk_script_path = strdup(awk_script);
create_dir(dirname(awk_script_path));
install_file(awk_script,
"# http://eradman.com/entrproject/status-filters.html\n"
"/^signal/ { print $3, \"terminated by signal\", $2; }\n"
"/^exit/ { print $3, \"returned exit code\", $2; }\n");
argv[0] = "/usr/bin/awk";
argv[1] = "-F";
argv[2] = "|";
argv[3] = "-f";
argv[4] = awk_script;
#if defined(_LINUX_PORT)
argv[5] = "-S";
#else
argv[5] = "-safe";
#endif
argv[6] = NULL;
if (safe == 2)
argv[5] = NULL;
int rc = pipe(status_stdin_pipe);
if (rc == -1)
err(1, "pipe");
status_pid = fork();
if (status_pid == -1)
err(1, "fork");
if (status_pid == 0) {
close(status_stdin_pipe[1]);
dup2(status_stdin_pipe[0], STDIN_FILENO);
execvp("awk", argv);
err(1, "could not exec %s", argv[0]);
}
close(status_stdin_pipe[0]);
free(awk_script_path);
}
void
write_log_filter(char *input, size_t len) {
if (write(status_stdin_pipe[1], input, len) == -1)
err(1, "write to child");
}
void
end_log_filter() {
close(status_stdin_pipe[1]);
kill(status_pid, SIGKILL);
}
/*
* create_dir - ensure a directory exists
* install_file - create file is it does not exist
*/
void
create_dir(const char *dir) {
struct stat dst_sb;
if (stat(dir, &dst_sb) == -1)
mkdir(dir, 0750);
}
void
install_file(const char *dst, const char *content) {
int fd;
struct stat dst_sb;
if (stat(dst, &dst_sb) == -1) {
printf("entr: created '%s'\n", dst);
fd = open(dst, O_WRONLY | O_CREAT, 0640);
if (fd == -1)
err(1, "open");
if (write(fd, content, strlen(content)) == -1)
err(1, "write");
close(fd);
}
}