CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cmd_util.cc
Go to the documentation of this file.
1 
5 #include "cvmfs_config.h"
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 {
22  if (!FileExists(path_hooks))
23  return 0;
24 
25  int pipe_stdin[2];
26  MakePipe(pipe_stdin);
27  std::set<int> preserve_fildes;
28  preserve_fildes.insert(0);
29  preserve_fildes.insert(1);
30  preserve_fildes.insert(2);
31  std::map<int, int> map_fildes;
32  map_fildes[pipe_stdin[0]] = 0; // Reading end of pipe_stdin
33  std::vector<std::string> cmd_line;
34  cmd_line.push_back("/bin/sh");
35  pid_t child_pid;
36  bool rvb = ManagedExec(cmd_line,
37  preserve_fildes,
38  map_fildes,
39  false /* drop_credentials */,
40  false /* clear_env */,
41  false /* double_fork */,
42  &child_pid);
43  if (!rvb) {
44  ClosePipe(pipe_stdin);
45  return -127;
46  }
47  close(pipe_stdin[0]);
48 
49  const std::string callout =
50  func + "() { :; }\n" +
51  ". " + path_hooks + "\n" +
52  func + " " + fqrn + "\n";
53  WritePipe(pipe_stdin[1], callout.data(), callout.length());
54  close(pipe_stdin[1]);
55 
56  return WaitForChild(child_pid);
57 }
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:1846
int WaitForChild(pid_t pid, const std::vector< int > &sig_ok)
Definition: posix.cc:1602
void MakePipe(int pipe_fd[2])
Definition: posix.cc:492
bool FileExists(const std::string &path)
Definition: posix.cc:791
void WritePipe(int fd, const void *buf, size_t nbyte)
Definition: posix.cc:501
void ClosePipe(int pipe_fd[2])
Definition: posix.cc:551