More refactor. Added trivial config parser into c++ bindings for client.
[elliptics.git] / srw / src / worker.cpp
blob16bf4f2c63122159389261cb577ddb74559e4fe3
1 #include <elliptics/srw/srw.hpp>
3 static void kill_all_fds(const char *log)
5 int fd, null_fd;
7 for (int i = 3; i < 1024; ++i) {
8 close(i);
11 fd = open("/dev/null", O_RDWR);
12 if (fd < 0) {
13 fd = -errno;
14 fprintf(stderr, "Can not open /dev/null: %d\n", fd);
15 exit(fd);
18 null_fd = fd;
20 dup2(fd, STDIN_FILENO);
22 fd = open(log, O_RDWR | O_APPEND);
23 if (fd < 0) {
24 fd = null_fd;
27 dup2(fd, STDERR_FILENO);
28 dup2(fd, STDOUT_FILENO);
31 static void worker_usage(char *arg)
33 std::cerr << "Usage: " << arg << " <options>\n" <<
34 " -i init-file - shared library path to load worker code from\n" <<
35 " -c config-file - config file for worker\n" <<
36 " -l log-file - log file for worker\n" <<
37 " -p pipe-base - pipe base for worker: it will write to @pipe-base.w2c and read from @pipe-base.c2w\n" <<
38 " -h - this help\n";
39 exit(-1);
42 int main(int argc, char *argv[])
44 int ch;
45 std::string log("/dev/stdout"), pipe("/tmp/test-pipe"), init, conf;
47 while ((ch = getopt(argc, argv, "c:i:l:p:h")) != -1) {
48 switch (ch) {
49 case 'c':
50 conf.assign(optarg);
51 break;
52 case 'i':
53 init.assign(optarg);
54 break;
55 case 'l':
56 log.assign(optarg);
57 break;
58 case 'p':
59 pipe.assign(optarg);
60 break;
61 case 'h':
62 default:
63 worker_usage(argv[0]);
67 kill_all_fds(log.c_str());
69 try {
70 ioremap::srw::worker<ioremap::srw::shared> w(log, pipe, init, conf);
71 w.process();
72 } catch (const std::exception &e) {
73 std::ofstream l(log.c_str(), std::ios::app);
74 l << getpid() << ": worker exception: " << e.what() << std::endl;
77 return 0;