-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtagging.cxx
198 lines (159 loc) · 4.21 KB
/
tagging.cxx
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* this deals with tagging modules
hlab gets its results from this functionality in the form of an interval manager */
/* c stdlib includes */
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
/* OS */
#include <sys/mman.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <signal.h>
/* c++ includes */
#include <map>
#include <string>
#include <vector>
using namespace std;
/* autils */
extern "C" {
#include <autils/bytes.h>
#include <autils/subprocess.h>
}
#include <autils/filesys.hpp>
/* local stuff */
#include "IntervalMgr.h"
int
tagging_tag(string target, string tagger, IntervalMgr &mgr)
{
//TODO: check for stderr chars
int rc = -1;
/* subprocess variables */
pid_t pid = -1;
int child_stdout = -1;
char arg0[PATH_MAX];
char arg1[PATH_MAX];
char *argv[3] = {arg0, arg1, NULL};
/* file/line stuff */
FILE *fp = NULL;
char *line = NULL;
/* execute */
strncpy(arg0, tagger.c_str(), PATH_MAX-1);
strncpy(arg1, target.c_str(), PATH_MAX-1);
if(0 != launch_ex(arg0, argv, &pid, NULL, &child_stdout, NULL)) {
printf("ERROR! launch_ex()\n");
goto cleanup;
}
/* wrap the tagger's stdout descriptor into a convenient file pointer */
fp = fdopen(child_stdout, "r");
if(!fp) {
printf("ERROR! fdopen()\n");
goto cleanup;
}
if(mgr.readFromFilePointer(fp)) {
printf("ERROR! readFromFilePointer()\n");
goto cleanup;
}
rc = 0;
cleanup:
/* terminate tagger */
if(pid != -1) {
int stat;
printf("kill() on pid=%d...\n", pid);
if(0 != kill(pid, SIGTERM))
printf("ERROR: kill()\n");
printf("waitpid() on pid=%d\n", pid);
if(waitpid(pid, &stat, 0) != pid)
printf("ERROR: waitpid()\n");
printf("waitpid() done\n");
pid = -1;
}
/* this free must occur even if getline() failed */
if(line) {
free(line);
line = NULL;
}
/* close wrapping file pointer */
if(fp) {
fclose(fp);
fp = NULL;
/* The file descriptor is not dup'ed, and will be closed
when the stream created by fdopen() is closed. */
child_stdout = -1;
}
/* close stdout file descriptor (if necessary) */
if(child_stdout != -1) {
close(child_stdout);
child_stdout = -1;
}
return rc;
}
/* "what taggers exist?" */
int tagging_findall(vector<string> &result)
{
string cwd;
filesys_cwd(cwd);
struct stat sb;
/* collect all files named hltag_* from hardcoded paths */
vector<string> temp;
filesys_ls(AUTILS_FILESYS_LS_STARTSWITH, "hltag_", cwd, temp, true);
filesys_ls(AUTILS_FILESYS_LS_STARTSWITH, "hltag_", cwd+"/taggers",
temp, true);
filesys_ls(AUTILS_FILESYS_LS_STARTSWITH, "hltag_", "/usr/local/bin",
temp, true);
/* filter executables */
result.clear();
for(int i=0; i<temp.size(); ++i) {
const char *fpath = temp[i].c_str();
if(stat(fpath, &sb) == 0 && (sb.st_mode & S_IXUSR)) {
result.push_back(fpath);
}
else {
printf("damn, %s is not executable\n", fpath);
}
}
return 0;
}
/* "what taggers exist that will agree to service <target>?" */
int tagging_pollall(string target, vector<string> &results)
{
int rc = -1;
printf("%s()\n", __func__);
/* collect all taggers */
vector<string> candidates;
if(0 != tagging_findall(candidates))
goto cleanup;
/* execute each one with target as an argument, see who responds */
for(auto i=candidates.begin(); i!=candidates.end(); ++i) {
#define CHILD_STDOUT_SZ 100
int child_ret;
char child_stdout[CHILD_STDOUT_SZ] = {'\0'};
char arg0[PATH_MAX];
char arg1[PATH_MAX];
strncpy(arg0, i->c_str(), PATH_MAX-1);
strncpy(arg1, target.c_str(), PATH_MAX-1);
char *argv[3] = {arg0, arg1, NULL};
string basename;
filesys_basename(*i, basename);
memset(child_stdout, 0, CHILD_STDOUT_SZ);
printf("launching %s %s %s\n", argv[0], argv[1], argv[2]);
if(0 != launch(arg0, argv, &child_ret, child_stdout, CHILD_STDOUT_SZ, NULL, 0, 1)) {
printf("ERROR: launch()\n");
continue;
}
printf("tagger(%s) returned %d\n", basename.c_str(), child_ret);
if(child_ret != 0) {
continue;
}
if(strncmp(child_stdout, "[0x", 3)) {
child_stdout[CHILD_STDOUT_SZ-1] = '\0';
printf("ERROR: tagger(%s) output didn't look right (\"%s...\")\n",
i->c_str(), child_stdout);
continue;
}
results.push_back(*i);
}
rc = 0;
cleanup:
return rc;
}