softwarecontainer  0.18.0-739e8d7 2017-05-04
softwarecontainer-common.h
1 /*
2  * Copyright (C) 2016-2017 Pelagicore AB
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
9  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
10  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR
11  * BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES
12  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
13  * WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
14  * ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  *
17  * For further information see LICENSE
18  */
19 
20 #pragma once
21 
22 #include "softwarecontainer-log.h"
23 
24 #include <map>
25 
26 #include <glibmm.h>
27 #include <sys/wait.h>
28 
29 namespace softwarecontainer {
30 
31 typedef int32_t ContainerID;
32 typedef std::map<std::string, std::string> EnvironmentVariables;
33 
34 static constexpr pid_t INVALID_PID = -1;
35 static constexpr int INVALID_FD = -1;
36 static constexpr uid_t ROOT_UID = 0;
37 
38 
45 inline int waitForProcessTermination(pid_t pid)
46 {
47  int status = 0;
48  waitpid(pid, &status, 0);
49  return WEXITSTATUS(status);
50 }
51 
57 bool isDirectory(const std::string &path);
58 
66 bool isDirectoryEmpty(const std::string &path);
67 
73 bool isFile(const std::string &path);
74 
80 bool isPipe(const std::string &path);
81 
87 bool isSocket(const std::string &path);
88 
94 bool existsInFileSystem(const std::string &path);
95 
96 std::string parentPath(const std::string &path);
97 std::string baseName(const std::string &path);
98 
99 bool touch(const std::string &path);
100 bool writeToFile(const std::string &path, const std::string &content);
101 bool readFromFile(const std::string &path, std::string &content);
102 bool parseInt(const char *args, int *result);
103 
104 /*
105  * This builds a full path from parts, including the appropriate separator.
106  *
107  * Example: buildPath("usr", "local", "bin) => usr/local/bin
108  *
109  * @returns a string representing the combined path
110  */
111 std::string buildPath(const std::string &arg1, const std::string &arg2);
112 std::string buildPath(const std::string &arg1, const std::string &arg2, const std::string &arg3);
113 
114 } // namespace softwarecontainer;
bool isDirectoryEmpty(const std::string &path)
isDirectoryEmpty Check if path is empty
bool isDirectory(const std::string &path)
isDirectory Check if path is a directory
bool isPipe(const std::string &path)
isPipe Check if path is a pipe
bool existsInFileSystem(const std::string &path)
existsInFileSystem Check if path exists
int waitForProcessTermination(pid_t pid)
waitForProcessTermination Waits for a process to terminate and then returns the status of the process...
bool isFile(const std::string &path)
isFile Check if path is a file
bool isSocket(const std::string &path)
isSocket Check if path is a socket
Developers guide to adding a config item: