CernVM-FS  2.13.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_util.cc
Go to the documentation of this file.
1 
6 #include "cmd_util.h"
7 
8 #include <unistd.h>
9 
10 #include <map>
11 #include <set>
12 #include <string>
13 #include <vector>
14 
15 #include "publish/except.h"
16 #include "util/posix.h"
17 
18 int publish::CallServerHook(const std::string &func,
19  const std::string &fqrn,
20  const std::string &path_hooks) {
21  if (!FileExists(path_hooks))
22  return 0;
23 
24  int pipe_stdin[2];
25  MakePipe(pipe_stdin);
26  std::set<int> preserve_fildes;
27  preserve_fildes.insert(0);
28  preserve_fildes.insert(1);
29  preserve_fildes.insert(2);
30  std::map<int, int> map_fildes;
31  map_fildes[pipe_stdin[0]] = 0; // Reading end of pipe_stdin
32  std::vector<std::string> cmd_line;
33  cmd_line.push_back("/bin/sh");
34  pid_t child_pid;
35  bool rvb = ManagedExec(cmd_line,
36  preserve_fildes,
37  map_fildes,
38  false /* drop_credentials */,
39  false /* clear_env */,
40  false /* double_fork */,
41  &child_pid);
42  if (!rvb) {
43  ClosePipe(pipe_stdin);
44  return -127;
45  }
46  close(pipe_stdin[0]);
47 
48  const std::string callout = func + "() { :; }\n" + ". " + path_hooks + "\n"
49  + func + " " + fqrn + "\n";
50  WritePipe(pipe_stdin[1], callout.data(), callout.length());
51  close(pipe_stdin[1]);
52 
53  return WaitForChild(child_pid);
54 }
int CallServerHook(const std::string &func, const std::string &fqrn, const std::string &path_hooks="/etc/cvmfs/cvmfs_server_hooks.sh")
Definition: cmd_util.cc:18
bool ManagedExec(const std::vector< std::string > &command_line, const std::set< int > &preserve_fildes, const std::map< int, int > &map_fildes, const bool drop_credentials, const bool clear_env, const bool double_fork, pid_t *child_pid)
Definition: posix.cc:1895
int WaitForChild(pid_t pid, const std::vector< int > &sig_ok)
Definition: posix.cc:1600
void MakePipe(int pipe_fd[2])
Definition: posix.cc:487
bool FileExists(const std::string &path)
Definition: posix.cc:803
void WritePipe(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:496
void ClosePipe(int pipe_fd[2])
Definition: posix.cc:559