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  const bool rvb = ManagedExec(
36  cmd_line, preserve_fildes, map_fildes, false /* drop_credentials */,
37  false /* clear_env */, false /* double_fork */, &child_pid);
38  if (!rvb) {
39  ClosePipe(pipe_stdin);
40  return -127;
41  }
42  close(pipe_stdin[0]);
43 
44  const std::string callout = func + "() { :; }\n" + ". " + path_hooks + "\n"
45  + func + " " + fqrn + "\n";
46  WritePipe(pipe_stdin[1], callout.data(), callout.length());
47  close(pipe_stdin[1]);
48 
49  return WaitForChild(child_pid);
50 }
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:1896
int WaitForChild(pid_t pid, const std::vector< int > &sig_ok)
Definition: posix.cc:1601
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