├── .gitignore
├── CMakeLists.txt
├── LICENSE
├── common
├── CMakeLists.txt
├── config.hpp
└── utils
│ ├── c-args.hpp
│ ├── callback-guard.cpp
│ ├── callback-guard.hpp
│ ├── callback-wrapper.hpp
│ ├── ccolor.cpp
│ ├── ccolor.hpp
│ ├── channel.cpp
│ ├── channel.hpp
│ ├── counting-map.hpp
│ ├── credentials.cpp
│ ├── credentials.hpp
│ ├── daemon.cpp
│ ├── daemon.hpp
│ ├── environment.cpp
│ ├── environment.hpp
│ ├── eventfd.cpp
│ ├── eventfd.hpp
│ ├── exception.cpp
│ ├── exception.hpp
│ ├── execute.cpp
│ ├── execute.hpp
│ ├── fd-utils.cpp
│ ├── fd-utils.hpp
│ ├── file-wait.cpp
│ ├── file-wait.hpp
│ ├── fs.cpp
│ ├── fs.hpp
│ ├── glib-loop.cpp
│ ├── glib-loop.hpp
│ ├── glib-utils.cpp
│ ├── glib-utils.hpp
│ ├── img.cpp
│ ├── img.hpp
│ ├── initctl.cpp
│ ├── initctl.hpp
│ ├── inotify.cpp
│ ├── inotify.hpp
│ ├── latch.cpp
│ ├── latch.hpp
│ ├── make-clean.hpp
│ ├── paths.hpp
│ ├── same-thread-guard.cpp
│ ├── same-thread-guard.hpp
│ ├── scoped-daemon.cpp
│ ├── scoped-daemon.hpp
│ ├── scoped-dir.cpp
│ ├── scoped-dir.hpp
│ ├── scoped-gerror.cpp
│ ├── scoped-gerror.hpp
│ ├── signal.cpp
│ ├── signal.hpp
│ ├── signalfd.cpp
│ ├── signalfd.hpp
│ ├── smack.cpp
│ ├── smack.hpp
│ ├── spin-wait-for.hpp
│ ├── text.cpp
│ ├── text.hpp
│ ├── typeinfo.cpp
│ ├── typeinfo.hpp
│ ├── value-latch.hpp
│ ├── vt.cpp
│ ├── vt.hpp
│ ├── worker.cpp
│ └── worker.hpp
├── doc
├── cargo_usage.md
├── custom.css
├── doxygen.cfg
├── footer.html
├── generate_documentation.sh
└── header.html
├── libs
├── cargo-fd
│ ├── CMakeLists.txt
│ ├── cargo-fd.hpp
│ ├── internals
│ │ ├── fdstore.cpp
│ │ ├── fdstore.hpp
│ │ ├── from-fdstore-internet-visitor.hpp
│ │ ├── from-fdstore-visitor-base.hpp
│ │ ├── from-fdstore-visitor.hpp
│ │ ├── to-fdstore-internet-visitor.hpp
│ │ ├── to-fdstore-visitor-base.hpp
│ │ └── to-fdstore-visitor.hpp
│ └── libcargo-fd.pc.in
├── cargo-gvariant
│ ├── CMakeLists.txt
│ ├── cargo-gvariant.hpp
│ ├── internals
│ │ ├── from-gvariant-visitor.hpp
│ │ └── to-gvariant-visitor.hpp
│ └── libcargo-gvariant.pc.in
├── cargo-ipc
│ ├── CMakeLists.txt
│ ├── client.cpp
│ ├── client.hpp
│ ├── epoll
│ │ ├── event-poll.cpp
│ │ ├── event-poll.hpp
│ │ ├── events.cpp
│ │ ├── events.hpp
│ │ ├── glib-dispatcher.cpp
│ │ ├── glib-dispatcher.hpp
│ │ ├── thread-dispatcher.cpp
│ │ └── thread-dispatcher.hpp
│ ├── exception.hpp
│ ├── internals
│ │ ├── acceptor.cpp
│ │ ├── acceptor.hpp
│ │ ├── add-peer-request.hpp
│ │ ├── event-queue.hpp
│ │ ├── finish-request.hpp
│ │ ├── method-request.hpp
│ │ ├── processor.cpp
│ │ ├── processor.hpp
│ │ ├── remove-method-request.hpp
│ │ ├── remove-peer-request.hpp
│ │ ├── request-queue.hpp
│ │ ├── result-builder.hpp
│ │ ├── send-result-request.hpp
│ │ ├── signal-request.hpp
│ │ ├── socket.cpp
│ │ └── socket.hpp
│ ├── libcargo-ipc.pc.in
│ ├── method-result.cpp
│ ├── method-result.hpp
│ ├── result.hpp
│ ├── service.cpp
│ ├── service.hpp
│ ├── types.cpp
│ ├── types.hpp
│ ├── unique-id.cpp
│ └── unique-id.hpp
├── cargo-json
│ ├── CMakeLists.txt
│ ├── cargo-json.hpp
│ ├── internals
│ │ ├── from-json-visitor.hpp
│ │ └── to-json-visitor.hpp
│ └── libcargo-json.pc.in
├── cargo-sqlite-json
│ ├── CMakeLists.txt
│ ├── cargo-sqlite-json.hpp
│ └── libcargo-sqlite-json.pc.in
├── cargo-sqlite
│ ├── CMakeLists.txt
│ ├── cargo-sqlite.hpp
│ ├── internals
│ │ ├── from-kvstore-ignoring-visitor.hpp
│ │ ├── from-kvstore-visitor-base.hpp
│ │ ├── from-kvstore-visitor.hpp
│ │ ├── kvstore-visitor-utils.hpp
│ │ ├── kvstore.cpp
│ │ ├── kvstore.hpp
│ │ └── to-kvstore-visitor.hpp
│ ├── libcargo-sqlite.pc.in
│ └── sqlite3
│ │ ├── connection.cpp
│ │ ├── connection.hpp
│ │ ├── statement.cpp
│ │ └── statement.hpp
├── cargo-validator
│ ├── CMakeLists.txt
│ ├── exception.hpp
│ ├── internals
│ │ ├── is-validable.hpp
│ │ └── validator-visitor.hpp
│ ├── libcargo-validator.pc.in
│ ├── validator.cpp
│ └── validator.hpp
├── cargo
│ ├── CMakeLists.txt
│ ├── exception.hpp
│ ├── fields-union.hpp
│ ├── fields.hpp
│ ├── internals
│ │ ├── is-like-tuple.hpp
│ │ ├── is-streamable.hpp
│ │ ├── is-union.hpp
│ │ ├── is-visitable.hpp
│ │ └── visit-fields.hpp
│ ├── libcargo.pc.in
│ └── types.hpp
└── logger
│ ├── CMakeLists.txt
│ ├── backend-file.cpp
│ ├── backend-file.hpp
│ ├── backend-journal.cpp
│ ├── backend-journal.hpp
│ ├── backend-null.hpp
│ ├── backend-persistent-file.cpp
│ ├── backend-persistent-file.hpp
│ ├── backend-stderr.cpp
│ ├── backend-stderr.hpp
│ ├── backend-syslog.cpp
│ ├── backend-syslog.hpp
│ ├── backend.hpp
│ ├── formatter.cpp
│ ├── formatter.hpp
│ ├── level.cpp
│ ├── level.hpp
│ ├── libLogger.pc.in
│ ├── logger-scope.cpp
│ ├── logger-scope.hpp
│ ├── logger.cpp
│ └── logger.hpp
├── packaging
└── cargo.spec
└── tests
├── CMakeLists.txt
├── cppcheck
├── cppcheck.sh
└── cppcheck.suppress
├── scripts
├── CMakeLists.txt
├── cargo_all_tests.py
├── cargo_launch_test.py
└── cargo_test_parser.py
└── unit_tests
├── CMakeLists.txt
├── cargo-ipc
├── ut-ipc.cpp
├── ut-socket.cpp
└── ut-unique-id.cpp
├── cargo
├── testconfig-example.hpp
├── ut-cargo.cpp
├── ut-dynvisit.cpp
├── ut-kvstore.cpp
└── ut-validator.cpp
├── configs
├── CMakeLists.txt
├── systemd
│ ├── cargo-socket-test.service.in
│ └── cargo-socket-test.socket
└── utils
│ └── file.txt
├── epoll
└── ut-event-poll.cpp
├── log
└── ut-logger.cpp
├── socket_test_service
├── socket-test.cpp
└── socket-test.hpp
├── ut.cpp
├── ut.hpp
└── utils
├── ut-callback-guard.cpp
├── ut-cargs.cpp
├── ut-channel.cpp
├── ut-counting-map.cpp
├── ut-fd-utils.cpp
├── ut-fs.cpp
├── ut-glib-loop.cpp
├── ut-inotify.cpp
├── ut-paths.cpp
├── ut-same-thread-guard.cpp
├── ut-signalfd.cpp
├── ut-text.cpp
├── ut-value-latch.cpp
└── ut-worker.cpp
/.gitignore:
--------------------------------------------------------------------------------
1 | # Compiled Object files
2 | *.slo
3 | *.lo
4 | *.o
5 |
6 | # Compiled Dynamic libraries
7 | *.so
8 |
9 | # Compiled Static libraries
10 | *.lai
11 | *.la
12 | *.a
13 |
14 | # CMake/Makefile files
15 | *.cmake
16 | CMakeCache.txt
17 | CMakeFiles
18 | Makefile
19 |
20 | # Others
21 | doc/html
22 |
--------------------------------------------------------------------------------
/common/CMakeLists.txt:
--------------------------------------------------------------------------------
1 | # Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
2 | #
3 | # Licensed under the Apache License, Version 2.0 (the "License");
4 | # you may not use this file except in compliance with the License.
5 | # You may obtain a copy of the License at
6 | #
7 | # http://www.apache.org/licenses/LICENSE-2.0
8 | #
9 | # Unless required by applicable law or agreed to in writing, software
10 | # distributed under the License is distributed on an "AS IS" BASIS,
11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 | # See the License for the specific language governing permissions and
13 | # limitations under the License.
14 | #
15 | #
16 | # @file CMakeLists.txt
17 | # @author Pawel Kubik (p.kubik@samsung.com)
18 | #
19 |
20 | PROJECT(cargo-utils)
21 |
22 | MESSAGE(STATUS "")
23 | MESSAGE(STATUS "Generating makefile for the libcargo-utils...")
24 | FILE(GLOB_RECURSE SRCS *.cpp *.hpp)
25 |
26 | ## Setup target ################################################################
27 | ADD_LIBRARY(${PROJECT_NAME} STATIC ${SRCS})
28 |
29 | FIND_PACKAGE (Boost REQUIRED COMPONENTS system filesystem)
30 | PKG_CHECK_MODULES(CARGO_UTILS_DEPS REQUIRED glib-2.0)
31 |
32 | INCLUDE_DIRECTORIES(${COMMON_FOLDER} ${LIBS_FOLDER})
33 | INCLUDE_DIRECTORIES(SYSTEM ${Boost_INCLUDE_DIRS} ${CARGO_UTILS_DEPS_INCLUDE_DIRS})
34 |
35 | TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${Boost_LIBRARIES} ${CARGO_UTILS_DEPS_LIBRARIES})
36 |
37 | ## Install #####################################################################
38 | INSTALL(TARGETS ${PROJECT_NAME}
39 | DESTINATION ${LIB_INSTALL_DIR}
40 | COMPONENT DevelopmentLibraries)
41 |
42 | INSTALL(DIRECTORY . DESTINATION ${INCLUDE_INSTALL_DIR}/${PROJECT_NAME}
43 | FILES_MATCHING PATTERN "*.hpp"
44 | PATTERN "CMakeFiles" EXCLUDE)
45 |
--------------------------------------------------------------------------------
/common/utils/callback-guard.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Callback guard
23 | */
24 |
25 | #ifndef COMMON_UTILS_CALLBACK_GUARD_HPP
26 | #define COMMON_UTILS_CALLBACK_GUARD_HPP
27 |
28 | #include
29 |
30 |
31 | namespace utils {
32 |
33 | /**
34 | * Callback guard.
35 | * An utility class to control and/or monitor callback lifecycle.
36 | */
37 | class CallbackGuard {
38 | public:
39 | typedef std::shared_ptr Tracker;
40 |
41 | /**
42 | * Creates a guard.
43 | */
44 | CallbackGuard();
45 |
46 | /**
47 | * Waits for all trackers.
48 | */
49 | ~CallbackGuard();
50 |
51 | /**
52 | * Creates a tracker.
53 | */
54 | Tracker spawn() const;
55 |
56 | /**
57 | * Gets trackers count
58 | */
59 | long getTrackersCount() const;
60 |
61 | /**
62 | * Wait for all trackers.
63 | */
64 | bool waitForTrackers(const unsigned int timeoutMs);
65 | private:
66 | class SharedState;
67 | std::shared_ptr mSharedState;
68 |
69 | CallbackGuard(const CallbackGuard&) = delete;
70 | CallbackGuard& operator=(const CallbackGuard&) = delete;
71 | };
72 |
73 | } // namespace utils
74 |
75 |
76 | #endif // COMMON_UTILS_CALLBACK_GUARD_HPP
77 |
--------------------------------------------------------------------------------
/common/utils/ccolor.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Dariusz Michaluk
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Dariusz Michaluk (d.michaluk@samsung.com)
22 | * @brief Console colors utility
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/ccolor.hpp"
28 |
29 | #include
30 |
31 | namespace utils {
32 |
33 | std::string getConsoleEscapeSequence(Attributes attr, Color color)
34 | {
35 | char command[10];
36 |
37 | // Command is the control command to the terminal
38 | snprintf(command, sizeof(command), "%c[%u;%um", 0x1B, (unsigned int)attr, (unsigned int)color);
39 | return std::string(command);
40 | }
41 |
42 | } // namespace utils
43 |
--------------------------------------------------------------------------------
/common/utils/ccolor.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Dariusz Michaluk
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Dariusz Michaluk (d.michaluk@samsung.com)
22 | * @brief Console colors utility
23 | */
24 |
25 | #ifndef COMMON_UTILS_CCOLOR_HPP
26 | #define COMMON_UTILS_CCOLOR_HPP
27 |
28 | #include
29 |
30 | namespace utils {
31 |
32 | enum class Color : unsigned int {
33 | DEFAULT = 0,
34 | BLACK = 30,
35 | RED = 31,
36 | GREEN = 32,
37 | YELLOW = 33,
38 | BLUE = 34,
39 | MAGENTA = 35,
40 | CYAN = 36,
41 | WHITE = 37
42 | };
43 |
44 | enum class Attributes : unsigned int {
45 | DEFAULT = 0,
46 | BOLD = 1
47 | };
48 |
49 | std::string getConsoleEscapeSequence(Attributes attr, Color color);
50 |
51 | } // namespace utils
52 |
53 | #endif // COMMON_UTILS_CCOLOR_HPP
54 |
--------------------------------------------------------------------------------
/common/utils/counting-map.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Counting map
23 | */
24 |
25 | #ifndef COMMON_UTILS_COUNTING_MAP_HPP
26 | #define COMMON_UTILS_COUNTING_MAP_HPP
27 |
28 | #include
29 |
30 | namespace utils {
31 |
32 |
33 | /**
34 | * Structure used to count elements.
35 | * It's like multiset + count but is more efficient.
36 | */
37 | template
38 | class CountingMap {
39 | public:
40 | size_t increment(const Key& key)
41 | {
42 | auto res = mMap.insert(typename Map::value_type(key, 1));
43 | if (!res.second) {
44 | ++res.first->second;
45 | }
46 | return res.first->second;
47 | }
48 |
49 | size_t decrement(const Key& key)
50 | {
51 | auto it = mMap.find(key);
52 | if (it == mMap.end()) {
53 | return 0;
54 | }
55 | if (--it->second == 0) {
56 | mMap.erase(it);
57 | return 0;
58 | }
59 | return it->second;
60 | }
61 |
62 | void clear()
63 | {
64 | mMap.clear();
65 | }
66 |
67 | size_t get(const Key& key) const
68 | {
69 | auto it = mMap.find(key);
70 | return it == mMap.end() ? 0 : it->second;
71 | }
72 |
73 | bool empty() const
74 | {
75 | return mMap.empty();
76 | }
77 | private:
78 | typedef std::unordered_map Map;
79 | Map mMap;
80 | };
81 |
82 |
83 | } // namespace utils
84 |
85 |
86 | #endif // COMMON_UTILS_COUNTING_MAP_HPP
87 |
--------------------------------------------------------------------------------
/common/utils/credentials.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Maciej Karpiuk (m.karpiuk2@samsung.com)
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Maciej Karpiuk (m.karpiuk2@samsung.com)
22 | * @brief Credential management related functions
23 | */
24 |
25 | #include "utils/credentials.hpp"
26 | #include "utils/exception.hpp"
27 | #include "logger/logger.hpp"
28 |
29 | #include
30 | #include
31 | #include
32 |
33 | namespace utils {
34 |
35 | // ------------------- syscall wrappers -------------------
36 | void setgroups(const std::vector& gids)
37 | {
38 | if (::setgroups(gids.size(), gids.data()) == -1) {
39 | THROW_EXCEPTION(UtilsException, "Error in setgroups()", errno);
40 | }
41 | }
42 |
43 | void setregid(const gid_t rgid, const gid_t egid)
44 | {
45 | if (::setregid(rgid, egid) == -1) {
46 | THROW_EXCEPTION(UtilsException, "Error in setregid()", errno);
47 | }
48 | }
49 |
50 | void setreuid(const uid_t ruid, const uid_t euid)
51 | {
52 | if (::setreuid(ruid, euid) == -1) {
53 | THROW_EXCEPTION(UtilsException, "Error in setreuid()", errno);
54 | }
55 | }
56 |
57 | pid_t setsid()
58 | {
59 | pid_t pid = ::setsid();
60 | if (pid == -1) {
61 | THROW_EXCEPTION(UtilsException, "Error in setsid()", errno);
62 | }
63 | return pid;
64 | }
65 |
66 | } // namespace utils
67 |
--------------------------------------------------------------------------------
/common/utils/credentials.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Maciej Karpiuk (m.karpiuk2@samsung.com)
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Maciej Karpiuk (m.karpiuk2@samsung.com)
22 | * @brief Credential management related functions
23 | */
24 |
25 | #include
26 | #include
27 |
28 | namespace utils {
29 |
30 | // ------------------- syscall wrappers -------------------
31 | // Throw exception on error
32 | void setgroups(const std::vector& groups);
33 | void setregid(const gid_t rgid, const gid_t egid);
34 | void setreuid(const uid_t ruid, const uid_t euid);
35 | pid_t setsid();
36 |
37 |
38 | } // namespace utils
39 |
--------------------------------------------------------------------------------
/common/utils/daemon.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Dariusz Michaluk
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Dariusz Michaluk
22 | * @brief Run a process as a daemon
23 | */
24 |
25 | #ifndef COMMON_UTILS_DAEMON_HPP
26 | #define COMMON_UTILS_DAEMON_HPP
27 |
28 | namespace utils {
29 |
30 | bool daemonize();
31 |
32 | } // namespace utils
33 |
34 | #endif // COMMON_UTILS_DAEMON_HPP
35 |
--------------------------------------------------------------------------------
/common/utils/environment.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Michal Witanowski
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Michal Witanowski (m.witanowski@samsung.com)
22 | * @brief Declaration of environment setup routines that requires root privileges
23 | */
24 |
25 | #ifndef COMMON_UTILS_ENVIRONMENT_HPP
26 | #define COMMON_UTILS_ENVIRONMENT_HPP
27 |
28 | #include
29 | #include
30 | #include
31 | #include
32 |
33 |
34 | namespace utils {
35 |
36 |
37 | /**
38 | * Set supplementary groups to the current process.
39 | */
40 | bool setSuppGroups(const std::vector& groups);
41 |
42 | /**
43 | * Set effective and permitted capabilities on the current process and drop root privileges.
44 | */
45 | bool dropRoot(uid_t uid, gid_t gid, const std::vector& caps);
46 |
47 | /**
48 | * Launch binary as root user
49 | *
50 | * This function forks, sets UID 0 to child process and calls binary.
51 | */
52 | bool launchAsRoot(const std::vector& argv);
53 |
54 | /**
55 | * Join to namespace
56 | */
57 | bool joinToNs(int nsPid, int ns);
58 |
59 | /**
60 | * Pass file descriptor from namespace of some process
61 | */
62 | int passNamespacedFd(int nsPid, int ns, const std::function& fdFactory);
63 |
64 | } // namespace utils
65 |
66 |
67 | #endif // COMMON_UTILS_ENVIRONMENT_HPP
68 |
--------------------------------------------------------------------------------
/common/utils/eventfd.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Jan Olszak
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Jan Olszak (j.olszak@samsung.com)
22 | * @brief Linux socket wrapper
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/eventfd.hpp"
28 | #include "utils/exception.hpp"
29 | #include "utils/fd-utils.hpp"
30 | #include "logger/logger.hpp"
31 |
32 | #include
33 | #include
34 | #include
35 | #include
36 |
37 | namespace utils {
38 |
39 | EventFD::EventFD()
40 | {
41 | mFD = ::eventfd(0, EFD_SEMAPHORE | EFD_CLOEXEC);
42 | if (mFD == -1) {
43 | THROW_EXCEPTION(EventFDException, "Error in eventfd", errno);
44 | }
45 | }
46 |
47 | EventFD::~EventFD()
48 | {
49 | utils::close(mFD);
50 | }
51 |
52 | int EventFD::getFD() const
53 | {
54 | return mFD;
55 | }
56 |
57 | void EventFD::send()
58 | {
59 | const std::uint64_t toSend = 1;
60 | utils::write(mFD, &toSend, sizeof(toSend));
61 | }
62 |
63 | void EventFD::receive()
64 | {
65 | std::uint64_t readBuffer;
66 | utils::read(mFD, &readBuffer, sizeof(readBuffer));
67 | }
68 |
69 |
70 | } // namespace utils
71 |
--------------------------------------------------------------------------------
/common/utils/eventfd.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Jan Olszak
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Jan Olszak (j.olszak@samsung.com)
22 | * @brief Eventfd wrapper
23 | */
24 |
25 | #ifndef COMMON_UTILS_EVENTFD_HPP
26 | #define COMMON_UTILS_EVENTFD_HPP
27 |
28 | namespace utils {
29 |
30 | class EventFD {
31 | public:
32 |
33 | EventFD();
34 | virtual ~EventFD();
35 |
36 | EventFD(const EventFD& eventfd) = delete;
37 | EventFD& operator=(const EventFD&) = delete;
38 |
39 | /**
40 | * @return event's file descriptor.
41 | */
42 | int getFD() const;
43 |
44 | /**
45 | * Send an event of a given value
46 | */
47 | void send();
48 |
49 | /**
50 | * Receives the signal.
51 | * Blocks if there is no event.
52 | */
53 | void receive();
54 |
55 | private:
56 | int mFD;
57 | };
58 |
59 | } // namespace utils
60 |
61 | #endif // COMMON_UTILS_EVENTFD_HPP
62 |
--------------------------------------------------------------------------------
/common/utils/execute.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Execute utils
23 | */
24 |
25 | #ifndef COMMON_UTILS_EXECUTE_HPP
26 | #define COMMON_UTILS_EXECUTE_HPP
27 |
28 | #include
29 | #include
30 | #include
31 |
32 | namespace utils {
33 |
34 | /**
35 | * Execute function - deprecated
36 | */
37 | bool executeAndWait(const std::function& func, int& status);
38 |
39 | bool executeAndWait(const std::function& func);
40 |
41 |
42 | /**
43 | * Execute binary
44 | */
45 | ///@{
46 | bool executeAndWait(uid_t uid, const char* fname, const char* const* argv, int& status);
47 |
48 | bool executeAndWait(const char* fname, const char* const* argv);
49 |
50 | bool executeAndWait(const char* fname, const char* const* argv, int& status);
51 |
52 | bool executeAndWait(uid_t uid, const std::vector& argv, int& status);
53 |
54 | bool executeAndWait(uid_t uid, const std::vector& argv);
55 |
56 | bool executeAndWait(const std::vector& argv);
57 | ///@}
58 |
59 | /**
60 | * Wait until child processes ends
61 | */
62 | bool waitPid(pid_t pid, int& status);
63 |
64 | } // namespace utils
65 |
66 |
67 | #endif // COMMON_UTILS_EXECUTE_HPP
68 |
--------------------------------------------------------------------------------
/common/utils/file-wait.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Wait for file utility function
23 | */
24 |
25 | #ifndef COMMON_UTILS_FILE_WAIT_HPP
26 | #define COMMON_UTILS_FILE_WAIT_HPP
27 |
28 | #include
29 |
30 |
31 | namespace utils {
32 |
33 |
34 | //TODO It is used in unit tests now, but it is unclear
35 | // whether the same solution will be used in daemon.
36 | void waitForFile(const std::string& filename, const unsigned int timeoutMs);
37 |
38 |
39 | } // namespace utils
40 |
41 |
42 | #endif // COMMON_UTILS_FILE_WAIT_HPP
43 |
--------------------------------------------------------------------------------
/common/utils/glib-loop.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief C++ wrapper of glib main loop
23 | */
24 |
25 | #ifndef COMMON_UTILS_GLIB_LOOP_HPP
26 | #define COMMON_UTILS_GLIB_LOOP_HPP
27 |
28 | #include "utils/callback-guard.hpp"
29 |
30 | #include
31 | #include
32 | #include
33 |
34 |
35 | namespace utils {
36 |
37 |
38 | /**
39 | * Glib loop controller. Loop is running in separate thread.
40 | */
41 | class ScopedGlibLoop {
42 | public:
43 | /**
44 | * Starts a loop in separate thread.
45 | */
46 | ScopedGlibLoop();
47 |
48 | /**
49 | * Stops loop and waits for a thread.
50 | */
51 | ~ScopedGlibLoop();
52 |
53 | private:
54 | std::unique_ptr mLoop;
55 | std::thread mLoopThread;
56 | };
57 |
58 | /**
59 | * Miscellaneous helpers for the Glib library
60 | */
61 | class Glib {
62 | public:
63 | /**
64 | * A user provided function that will be called succesively after an interval has passed.
65 | *
66 | * Return true if the callback is supposed to be called further,
67 | * false if the callback is not to be called anymore and be destroyed.
68 | */
69 | typedef std::function OnTimerEventCallback;
70 |
71 | /**
72 | * Adds a timer event to the glib main loop.
73 | */
74 | static void addTimerEvent(const unsigned int intervalMs,
75 | const OnTimerEventCallback& callback,
76 | const CallbackGuard& guard);
77 |
78 | private:
79 | static gboolean onTimerEvent(gpointer data);
80 | };
81 |
82 | } // namespace utils
83 |
84 |
85 | #endif // COMMON_UTILS_GLIB_LOOP_HPP
86 |
--------------------------------------------------------------------------------
/common/utils/glib-utils.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief C++ wrapper of glib main loop
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/glib-utils.hpp"
28 | #include "utils/callback-wrapper.hpp"
29 |
30 | #include
31 |
32 | namespace utils {
33 |
34 | namespace {
35 |
36 | gboolean onIddle(gpointer data)
37 | {
38 | const VoidCallback& callback = getCallbackFromPointer(data);
39 | callback();
40 | return FALSE;
41 | }
42 |
43 | } // namespace
44 |
45 | void executeInGlibThread(const VoidCallback& callback, const CallbackGuard& guard)
46 | {
47 | if (!callback) {
48 | return;
49 | }
50 | g_idle_add_full(G_PRIORITY_DEFAULT,
51 | &onIddle,
52 | utils::createCallbackWrapper(callback, guard.spawn()),
53 | &utils::deleteCallbackWrapper);
54 |
55 | }
56 |
57 |
58 | } // namespace utils
59 |
--------------------------------------------------------------------------------
/common/utils/glib-utils.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Miscellaneous helpers for the Glib library
23 | */
24 |
25 | #ifndef COMMON_UTILS_GLIB_UTILS_HPP
26 | #define COMMON_UTILS_GLIB_UTILS_HPP
27 |
28 | #include "utils/callback-guard.hpp"
29 |
30 | namespace utils {
31 |
32 | typedef std::function VoidCallback;
33 |
34 | /**
35 | * Executes a callback in glib thread (adds an iddle event to glib)
36 | */
37 | void executeInGlibThread(const VoidCallback& callback, const CallbackGuard& guard);
38 |
39 |
40 | } // namespace utils
41 |
42 | #endif // COMMON_UTILS_GLIB_UTILS_HPP
43 |
--------------------------------------------------------------------------------
/common/utils/img.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Lukasz Kostyra
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Lukasz Kostyra (l.kostyra@samsung.com)
22 | * @brief Image utility functions declaration
23 | */
24 |
25 | #ifndef COMMON_UTILS_IMG_HPP
26 | #define COMMON_UTILS_IMG_HPP
27 |
28 | namespace utils {
29 |
30 | /**
31 | * Returns string with first free loop device.
32 | */
33 | bool getFreeLoopDevice(std::string& ret);
34 |
35 | /**
36 | * Mount an ext4 image from file on a given path by using a loop device.
37 | */
38 | bool mountImage(const std::string& image, const std::string& loopdev, const std::string& path);
39 |
40 | /**
41 | * Umounts previously mounted image.
42 | * This call will also free loop device used to mount specified path.
43 | */
44 | bool umountImage(const std::string& path, const std::string& loopdev);
45 |
46 | /**
47 | * Mounts an image and copies its contents to dst directory.
48 | */
49 | bool copyImageContents(const std::string& img, const std::string& dst);
50 |
51 | } // namespace utils
52 |
53 | #endif // COMMON_UTILS_IMG_HPP
54 |
--------------------------------------------------------------------------------
/common/utils/initctl.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Api for talking to init via initctl
23 | */
24 |
25 | #ifndef COMMON_UTILS_INITCTL_HPP
26 | #define COMMON_UTILS_INITCTL_HPP
27 |
28 |
29 | namespace utils {
30 |
31 | enum RunLevel : int {
32 | RUNLEVEL_POWEROFF = 0,
33 | RUNLEVEL_REBOOT = 6
34 | };
35 |
36 | bool setRunLevel(RunLevel runLevel);
37 |
38 |
39 | } // namespace utils
40 |
41 |
42 | #endif // COMMON_UTILS_INITCTL_HPP
43 |
--------------------------------------------------------------------------------
/common/utils/inotify.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Jan Olszak
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Jan Olszak (j.olszak@samsung.com)
22 | * @brief Inotify wrapper
23 | */
24 |
25 | #ifndef COMMON_UTILS_INOTIFY_HPP
26 | #define COMMON_UTILS_INOTIFY_HPP
27 |
28 | #include "cargo-ipc/epoll/event-poll.hpp"
29 |
30 | #include
31 | #include
32 | #include
33 |
34 | #include
35 |
36 |
37 | namespace utils {
38 |
39 | /**
40 | * Inotify monitors a directory and when a specified file or folder
41 | * is created or deleted it calls a corresponding handler.
42 | */
43 | class Inotify {
44 | public:
45 | typedef std::function Callback;
46 |
47 | Inotify(cargo::ipc::epoll::EventPoll& eventPoll);
48 | virtual ~Inotify();
49 |
50 | Inotify(const Inotify&) = delete;
51 | Inotify& operator=(const Inotify&) = delete;
52 |
53 | /**
54 | * Add a callback for a specified path
55 | */
56 | void setHandler(const std::string& path, const uint32_t eventMask, const Callback&& callback);
57 |
58 | /**
59 | * Stop watching the path
60 | */
61 | void removeHandler(const std::string& path);
62 |
63 | /**
64 | * @return inotify file descriptor
65 | */
66 | int getFD() const;
67 |
68 | private:
69 | struct Handler {
70 | std::string path;
71 | int watchID;
72 | Callback call;
73 | };
74 |
75 | typedef std::lock_guard Lock;
76 | std::recursive_mutex mMutex;
77 |
78 | int mFD;
79 | cargo::ipc::epoll::EventPoll& mEventPoll;
80 | std::vector mHandlers;
81 |
82 | void handleInternal();
83 | void removeHandlerInternal(const std::string& path);
84 | };
85 |
86 | } // namespace utils
87 |
88 | #endif // COMMON_UTILS_INOTIFY_HPP
89 |
--------------------------------------------------------------------------------
/common/utils/latch.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Synchronization latch
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/latch.hpp"
28 |
29 | #include
30 |
31 |
32 | namespace utils {
33 |
34 |
35 | Latch::Latch()
36 | : mCount(0)
37 | {
38 | }
39 |
40 | void Latch::set()
41 | {
42 | std::unique_lock lock(mMutex);
43 | ++mCount;
44 | mCondition.notify_one();
45 | }
46 |
47 | void Latch::wait()
48 | {
49 | waitForN(1);
50 | }
51 |
52 | bool Latch::wait(const unsigned int timeoutMs)
53 | {
54 | return waitForN(1, timeoutMs);
55 | }
56 |
57 | void Latch::waitForN(const unsigned int n)
58 | {
59 | std::unique_lock lock(mMutex);
60 | mCondition.wait(lock, [this, &n] {return mCount >= n;});
61 | mCount -= n;
62 | }
63 |
64 | bool Latch::waitForN(const unsigned int n, const unsigned int timeoutMs)
65 | {
66 | std::unique_lock lock(mMutex);
67 | if (!mCondition.wait_for(lock, std::chrono::milliseconds(timeoutMs),
68 | [this, &n] {return mCount >= n;})) {
69 | return false;
70 | }
71 | mCount -= n;
72 | return true;
73 | }
74 |
75 |
76 | bool Latch::empty()
77 | {
78 | std::unique_lock lock(mMutex);
79 | return mCount == 0;
80 | }
81 |
82 |
83 | } // namespace utils
84 |
--------------------------------------------------------------------------------
/common/utils/make-clean.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Mateusz Malicki
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Mateusz Malicki (m.malicki2@samsung.com)
22 | * @brief Function used to initialize C structures
23 | */
24 |
25 | #ifndef COMMON_UTILS_MAKE_CLEAN_HPP
26 | #define COMMON_UTILS_MAKE_CLEAN_HPP
27 |
28 | #include
29 | #include
30 |
31 | namespace utils {
32 |
33 | template
34 | void make_clean(T& value)
35 | {
36 | static_assert(std::is_pod::value, "make_clean require trivial and standard-layout");
37 | std::fill_n(reinterpret_cast(&value), sizeof(value), 0);
38 | }
39 |
40 | template
41 | T make_clean()
42 | {
43 | T value;
44 | make_clean(value);
45 | return value;
46 | }
47 |
48 | } // namespace utils
49 |
50 |
51 | #endif // COMMON_UTILS_MAKE_CLEAN_HPP
52 |
--------------------------------------------------------------------------------
/common/utils/same-thread-guard.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Same thread guard
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/same-thread-guard.hpp"
28 |
29 | #ifdef ENABLE_SAME_THREAD_GUARD
30 |
31 | #include "logger/logger.hpp"
32 | #include "logger/formatter.hpp"
33 |
34 | namespace utils {
35 |
36 | namespace {
37 |
38 | typedef decltype(logger::LogFormatter::getCurrentThread()) ThreadId;
39 | const ThreadId NOT_SET = 0;
40 |
41 | ThreadId getCurrentThreadId() {
42 | // use the same thread id numbering mechanism as in logger
43 | // to allow analyse accesses in log
44 | return logger::LogFormatter::getCurrentThread();
45 | }
46 |
47 | } // namespace
48 |
49 | SameThreadGuard::SameThreadGuard() : mThreadId(NOT_SET)
50 | {
51 | static_assert(std::is_same::value,
52 | "thread id type mismatch");
53 | }
54 |
55 | bool SameThreadGuard::check()
56 | {
57 | const ThreadId thisThreadId = getCurrentThreadId();
58 |
59 | ThreadId saved = NOT_SET;
60 | if (!mThreadId.compare_exchange_strong(saved, thisThreadId) && saved != thisThreadId) {
61 | LOGE("Detected thread id mismatch; saved: " << saved << "; current: " << thisThreadId);
62 | return false;
63 | }
64 | return true;
65 | }
66 |
67 | void SameThreadGuard::reset()
68 | {
69 | mThreadId.store(NOT_SET);
70 | }
71 |
72 | } // namespace utils
73 |
74 | #endif // ENABLE_SAME_THREAD_GUARD
75 |
--------------------------------------------------------------------------------
/common/utils/same-thread-guard.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Same thread guard
23 | */
24 |
25 | #ifndef COMMON_UTILS_SAME_THREAD_GUARD_HPP
26 | #define COMMON_UTILS_SAME_THREAD_GUARD_HPP
27 |
28 | #ifndef NDEBUG
29 | #define ENABLE_SAME_THREAD_GUARD
30 | #endif
31 |
32 | #ifdef ENABLE_SAME_THREAD_GUARD
33 | #include
34 | #include
35 | #endif
36 |
37 | namespace utils {
38 |
39 | /**
40 | * Same thread guard.
41 | * There are two purposes of this guard:
42 | * - reports invalid assumptions about synchronization needs (only in debug builds)
43 | * - acts as an annotation in the source code about the thread safety
44 | *
45 | * Usage example:
46 | * ASSERT_SAME_THREAD(workerThreadGuard);
47 | */
48 | class SameThreadGuard {
49 | public:
50 | #ifdef ENABLE_SAME_THREAD_GUARD
51 | # define ASSERT_SAME_THREAD(g) assert(g.check())
52 | SameThreadGuard();
53 |
54 | /**
55 | * On the first call it remembers the current thread id.
56 | * On the next call it verifies that current thread is the same as before.
57 | */
58 | bool check();
59 |
60 | /**
61 | * Reset thread id
62 | */
63 | void reset();
64 |
65 | private:
66 | std::atomic mThreadId;
67 |
68 | #else // ENABLE_SAME_THREAD_GUARD
69 | # define ASSERT_SAME_THREAD(g)
70 | static bool check() {return true;}
71 | static void reset() {}
72 | #endif // ENABLE_SAME_THREAD_GUARD
73 | };
74 |
75 | } // namespace utils
76 |
77 |
78 | #endif // COMMON_UTILS_SAME_THREAD_GUARD_HPP
79 |
--------------------------------------------------------------------------------
/common/utils/scoped-daemon.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Starts external daemon in constructor, stops it in destructor
23 | */
24 |
25 | #ifndef UNIT_TESTS_UTILS_SCOPED_DAEMON_HPP
26 | #define UNIT_TESTS_UTILS_SCOPED_DAEMON_HPP
27 |
28 | #include
29 |
30 |
31 | namespace utils {
32 |
33 |
34 | /**
35 | * External daemon launcher helper.
36 | */
37 | class ScopedDaemon {
38 | public:
39 | ScopedDaemon();
40 |
41 | /**
42 | * Stops a daemon if it is not stopped already.
43 | */
44 | ~ScopedDaemon();
45 |
46 | /**
47 | * Starts a daemon.
48 | * @param path daemon path
49 | * @param argv arguments passed to the daemon
50 | * @param useLauncher use additional launcher process
51 | */
52 | void start(const char* path, const char* const argv[], const bool useLauncher = false);
53 |
54 | /**
55 | * Stops a daemon by sending SIGTERM and waits for a process.
56 | */
57 | void stop();
58 | private:
59 | pid_t mPid;
60 | };
61 |
62 |
63 | } // namespace utils
64 |
65 |
66 | #endif // UNIT_TESTS_UTILS_SCOPED_DAEMON_HPP
67 |
--------------------------------------------------------------------------------
/common/utils/scoped-dir.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Create directory in constructor, delete it in destructor
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/scoped-dir.hpp"
28 | #include "utils/fs.hpp"
29 | #include "utils/exception.hpp"
30 | #include "logger/logger.hpp"
31 |
32 | namespace utils {
33 |
34 | ScopedDir::ScopedDir()
35 | {
36 | }
37 |
38 | ScopedDir::ScopedDir(const std::string& path)
39 | {
40 | create(path);
41 | }
42 |
43 | ScopedDir::~ScopedDir()
44 | {
45 | remove();
46 | }
47 |
48 | void ScopedDir::create(const std::string& path)
49 | {
50 | remove();
51 | if (!path.empty()) {
52 | mPath = path;
53 | remove();
54 | utils::createDirs(path);
55 | }
56 | }
57 |
58 | void ScopedDir::remove()
59 | {
60 | if (mPath.empty()) {
61 | return ;
62 | }
63 | try {
64 | utils::removeDir(mPath);
65 | } catch (const UtilsException&) {
66 | LOGE("ScopedDir: can't remove " + mPath);
67 | }
68 | }
69 |
70 | } // namespace utils
71 |
--------------------------------------------------------------------------------
/common/utils/scoped-dir.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief Create directory in constructor, delete it in destructor
23 | */
24 |
25 | #ifndef UNIT_TESTS_UTILS_SCOPED_DIR_HPP
26 | #define UNIT_TESTS_UTILS_SCOPED_DIR_HPP
27 |
28 | #include
29 |
30 |
31 | namespace utils {
32 |
33 |
34 | /**
35 | * Scoped directory
36 | * To be used in tests only
37 | */
38 | class ScopedDir {
39 | public:
40 | ScopedDir();
41 | ScopedDir(const std::string& path);
42 | ~ScopedDir();
43 |
44 | /**
45 | * Creates a dir or if exists ensures it is empty
46 | */
47 | void create(const std::string& path);
48 |
49 | /**
50 | * Deletes this dir with all content
51 | */
52 | void remove();
53 |
54 | private:
55 | std::string mPath;
56 | };
57 |
58 |
59 | } // namespace utils
60 |
61 |
62 | #endif // UNIT_TESTS_UTILS_SCOPED_DIR_HPP
63 |
--------------------------------------------------------------------------------
/common/utils/scoped-gerror.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Jan Olszak
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Jan Olszak (j.olszak@samsung.com)
22 | * @brief Implementation of the wrapper for GError
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/scoped-gerror.hpp"
28 |
29 | namespace utils {
30 |
31 | ScopedGError::ScopedGError()
32 | : mError(NULL)
33 | {
34 | }
35 |
36 | ScopedGError::ScopedGError(ScopedGError&& other)
37 | : mError(other.mError)
38 | {
39 | other.mError = NULL;
40 | }
41 |
42 | ScopedGError::~ScopedGError()
43 | {
44 | if (mError) {
45 | g_error_free(mError);
46 | }
47 | }
48 |
49 | bool ScopedGError::strip()
50 | {
51 | return g_dbus_error_strip_remote_error(mError);
52 | }
53 |
54 | ScopedGError::operator bool () const
55 | {
56 | return mError != nullptr;
57 | }
58 |
59 | GError** ScopedGError::operator& ()
60 | {
61 | return &mError;
62 | }
63 |
64 | const GError* ScopedGError::operator->() const
65 | {
66 | return mError;
67 | }
68 |
69 | std::ostream& operator<<(std::ostream& os, const ScopedGError& e)
70 | {
71 | os << e->message;
72 | return os;
73 | }
74 |
75 | } // namespace utils
76 |
--------------------------------------------------------------------------------
/common/utils/scoped-gerror.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Jan Olszak
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Jan Olszak (j.olszak@samsung.com)
22 | * @brief Declaration of the wrapper for GError
23 | */
24 |
25 | #ifndef COMMON_SCOPED_GERROR_HPP
26 | #define COMMON_SCOPED_GERROR_HPP
27 |
28 | #include
29 | #include
30 |
31 | namespace utils {
32 |
33 | class ScopedGError {
34 | public:
35 | ScopedGError();
36 | ScopedGError(ScopedGError&&);
37 | ~ScopedGError();
38 |
39 | ScopedGError(const ScopedGError&) = delete;
40 | ScopedGError& operator=(const ScopedGError&) = delete;
41 |
42 | /**
43 | * Strip the error
44 | */
45 | bool strip();
46 |
47 | /**
48 | * Is error pointer NULL?
49 | */
50 | operator bool () const;
51 |
52 | /**
53 | * @return pointer to the GError
54 | */
55 | GError** operator& ();
56 |
57 | /**
58 | * @return the GError
59 | */
60 | const GError* operator->() const;
61 |
62 | /**
63 | * Writes out the error message
64 | * @param os the output stream
65 | * @param e error to write out
66 | */
67 | friend std::ostream& operator<<(std::ostream& os, const ScopedGError& e);
68 |
69 | private:
70 | GError* mError;
71 |
72 | };
73 |
74 | } // namespace utils
75 |
76 | #endif // COMMON_SCOPED_GERROR_HPP
77 |
--------------------------------------------------------------------------------
/common/utils/signal.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Jan Olszak
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Jan Olszak (j.olszak@samsung.com)
22 | * @brief Signal related functions
23 | */
24 |
25 | #ifndef COMMON_UTILS_SIGNAL_HPP
26 | #define COMMON_UTILS_SIGNAL_HPP
27 |
28 | #include
29 | #include
30 | #include
31 |
32 | namespace utils {
33 |
34 | // ------------------- syscall wrappers -------------------
35 | // Throw exception on error
36 | void pthread_sigmask(int how, const ::sigset_t *set, ::sigset_t *get);
37 | void sigemptyset(::sigset_t *set);
38 | void sigfillset(::sigset_t *set);
39 | void sigaddset(::sigset_t *set, int signum);
40 | void sigpending(::sigset_t *set);
41 | bool sigismember(const ::sigset_t *set, int signum);
42 | int sigtimedwait(const sigset_t *set, siginfo_t *info, const struct timespec *timeout);
43 | void sigaction(int signum, const struct sigaction *act, struct sigaction *oldact);
44 |
45 |
46 | // ------------------- higher level functions -------------------
47 | ::sigset_t getSignalMask();
48 | bool isSignalPending(const int sigNum);
49 | bool waitForSignal(const int sigNum, int timeoutMs);
50 | bool isSignalBlocked(const int sigNum);
51 | void signalBlockAllExcept(const std::initializer_list& signals);
52 | void signalBlock(const int sigNum);
53 | void signalUnblock(const int sigNum);
54 | std::vector> signalIgnore(const std::initializer_list& signals);
55 | struct ::sigaction signalSet(const int sigNum, const struct ::sigaction *sigAct);
56 | void sendSignal(const pid_t pid, const int sigNum);
57 |
58 |
59 | } // namespace utils
60 |
61 |
62 | #endif // COMMON_UTILS_SIGNAL_HPP
63 |
--------------------------------------------------------------------------------
/common/utils/spin-wait-for.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Jan Olszak
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Jan Olszak (j.olszak@samsung.com)
22 | * @brief Simple spin lock implementation
23 | */
24 |
25 | #include
26 | #include
27 |
28 | namespace utils {
29 |
30 | template
31 | bool spinWaitFor(int timeoutMs, Predicate pred)
32 | {
33 | auto until = std::chrono::steady_clock::now() + std::chrono::milliseconds(timeoutMs);
34 | while (!pred()) {
35 | if (std::chrono::steady_clock::now() >= until) {
36 | return false;
37 | }
38 | std::this_thread::sleep_for(std::chrono::milliseconds(100));
39 | }
40 | return true;
41 | }
42 |
43 | } // namespace utils
--------------------------------------------------------------------------------
/common/utils/text.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Krzysztof Dynowski (k.dynowski@samsumg.com)
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Krzysztof Dynowski (k.dynowski@samsumg.com)
22 | * @brief Text related utility
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/text.hpp"
28 |
29 | namespace utils {
30 | namespace {
31 | const char hexmap[] = {'0', '1', '2', '3', '4', '5', '6', '7',
32 | '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
33 | }
34 |
35 | std::string toHexString(const void *data, unsigned len)
36 | {
37 | const unsigned char *d = static_cast(data);
38 | std::string s(len * 2, ' ');
39 | for (unsigned i = 0; i < len; ++i) {
40 | s[2 * i] = hexmap[(d[i] >> 4) & 0x0F];
41 | s[2 * i + 1] = hexmap[d[i] & 0x0F];
42 | }
43 | return s;
44 | }
45 |
46 | std::vector split(const std::string& str, const std::string& delim)
47 | {
48 | std::vector tokens;
49 | if (str.empty()) {
50 | return tokens;
51 | }
52 |
53 | for (std::string::size_type startPos = 0; ; ) {
54 | std::string::size_type endPos = str.find_first_of(delim, startPos);
55 |
56 | if (endPos == std::string::npos) {
57 | tokens.push_back(str.substr(startPos, endPos));
58 | break;
59 | }
60 | tokens.push_back(str.substr(startPos, endPos - startPos));
61 |
62 | startPos = endPos + 1;
63 | }
64 | return tokens;
65 | }
66 |
67 | } // namespace utils
68 |
--------------------------------------------------------------------------------
/common/utils/text.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Krzysztof Dynowski (k.dynowski@samsumg.com)
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Krzysztof Dynowski (k.dynowski@samsumg.com)
22 | * @brief Text related utils
23 | */
24 |
25 | #ifndef COMMON_UTILS_TEXT_HPP
26 | #define COMMON_UTILS_TEXT_HPP
27 |
28 | #include
29 | #include
30 | #include
31 | #include
32 |
33 | namespace utils {
34 |
35 | inline bool beginsWith(std::string const &value, std::string const &part)
36 | {
37 | if (part.size() > value.size()) {
38 | return false;
39 | }
40 | return std::equal(part.begin(), part.end(), value.begin());
41 | }
42 |
43 | inline bool endsWith(std::string const &value, std::string const &part)
44 | {
45 | if (part.size() > value.size()) {
46 | return false;
47 | }
48 | return std::equal(part.rbegin(), part.rend(), value.rbegin());
49 | }
50 |
51 | /**
52 | * Convert binary bytes array to hex string representation
53 | */
54 | std::string toHexString(const void *data, unsigned len);
55 |
56 | template
57 | std::string join(const std::vector& vec, const char *delim)
58 | {
59 | std::stringstream res;
60 | for (const auto& s : vec) {
61 | if (res.tellp()>0) {
62 | res << delim;
63 | }
64 | res << s;
65 | }
66 | return res.str();
67 | }
68 |
69 | template
70 | std::string join(const std::set& vec, const char *delim)
71 | {
72 | std::stringstream res;
73 | for (const auto& s : vec) {
74 | if (res.tellp()>0) {
75 | res << delim;
76 | }
77 | res << s;
78 | }
79 | return res.str();
80 | }
81 |
82 | std::vector split(const std::string& str, const std::string& delim);
83 |
84 | } // namespace utils
85 |
86 | #endif // COMMON_UTILS_TEXT_HPP
87 |
--------------------------------------------------------------------------------
/common/utils/typeinfo.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Jan Olszak (j.olszak@samsung.com)
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Jan Olszak (j.olszak@samsung.com)
22 | * @brief Synchronization latch
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/typeinfo.hpp"
28 |
29 | #include
30 | #include
31 | #include
32 |
33 | namespace utils {
34 |
35 | std::string getTypeName(const std::type_info& ti)
36 | {
37 | int status;
38 | char* demangled = abi::__cxa_demangle(ti.name() , 0, 0, &status);
39 | if (status) {
40 | std::string message = "abi::__cxa_demangle failed with ret code: " + std::to_string(status);
41 | throw std::runtime_error(message);
42 | }
43 |
44 | std::string ret(demangled);
45 | free(demangled);
46 | return ret;
47 | }
48 |
49 | } // namespace utils
50 |
--------------------------------------------------------------------------------
/common/utils/typeinfo.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Jan Olszak (j.olszak@samsung.com)
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Jan Olszak (j.olszak@samsung.com)
22 | * @brief Synchronization latch
23 | */
24 |
25 | #ifndef COMMON_TYPE_INFO_HPP
26 | #define COMMON_TYPE_INFO_HPP
27 |
28 | #include
29 | #include
30 |
31 | namespace utils {
32 |
33 | std::string getTypeName(const std::type_info& ti);
34 |
35 | template std::string getTypeName(const T& t)
36 | {
37 | return getTypeName(typeid(t));
38 | }
39 |
40 | } // namespace utils
41 |
42 |
43 | #endif // COMMON_TYPE_INFO_HPP
44 |
--------------------------------------------------------------------------------
/common/utils/vt.cpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Lukasz Kostyra
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Lukasz Kostyra (l.kostyra@samsung.com)
22 | * @brief VT-related utility functions
23 | */
24 |
25 | #include "config.hpp"
26 |
27 | #include "utils/vt.hpp"
28 | #include "logger/logger.hpp"
29 | #include "utils/exception.hpp"
30 | #include "utils/fd-utils.hpp"
31 |
32 | #include
33 | #include
34 | #include
35 | #include
36 | #include
37 | #include
38 |
39 | namespace {
40 |
41 | const std::string TTY_DEV = "/dev/tty0";
42 |
43 | } // namespace
44 |
45 | namespace utils {
46 |
47 | bool activateVT(const int& vt)
48 | {
49 | int consoleFD = -1;
50 |
51 | try {
52 | consoleFD = utils::open(TTY_DEV, O_WRONLY);
53 |
54 | struct vt_stat vtstat;
55 | vtstat.v_active = 0;
56 | utils::ioctl(consoleFD, VT_GETSTATE, &vtstat);
57 |
58 | if (vtstat.v_active == vt) {
59 | LOGW("vt" << vt << " is already active.");
60 | }
61 | else {
62 | // activate vt
63 | utils::ioctl(consoleFD, VT_ACTIVATE, reinterpret_cast(vt));
64 |
65 | // wait until activation is finished
66 | utils::ioctl(consoleFD, VT_WAITACTIVE, reinterpret_cast(vt));
67 | }
68 |
69 | utils::close(consoleFD);
70 | return true;
71 | } catch(const UtilsException & e) {
72 | LOGE("Failed to activate vt" << vt << ": " << e.what() << " (" << getSystemErrorMessage(e.mErrno) << ")");
73 | utils::close(consoleFD);
74 | return false;
75 | }
76 | }
77 |
78 | } // namespace utils
79 |
--------------------------------------------------------------------------------
/common/utils/vt.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Lukasz Kostyra
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Lukasz Kostyra (l.kostyra@samsung.com)
22 | * @brief VT-related utility functions
23 | */
24 |
25 | #ifndef COMMON_UTILS_VT_HPP
26 | #define COMMON_UTILS_VT_HPP
27 |
28 | namespace utils {
29 |
30 | bool activateVT(const int& vt);
31 |
32 | } // namespace utils
33 |
34 | #endif // COMMON_UTILS_VT_HPP
35 |
--------------------------------------------------------------------------------
/common/utils/worker.hpp:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
3 | *
4 | * Contact: Piotr Bartosiewicz
5 | *
6 | * Licensed under the Apache License, Version 2.0 (the "License");
7 | * you may not use this file except in compliance with the License.
8 | * You may obtain a copy of the License at
9 | *
10 | * http://www.apache.org/licenses/LICENSE-2.0
11 | *
12 | * Unless required by applicable law or agreed to in writing, software
13 | * distributed under the License is distributed on an "AS IS" BASIS,
14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 | * See the License for the specific language governing permissions and
16 | * limitations under the License
17 | */
18 |
19 | /**
20 | * @file
21 | * @author Piotr Bartosiewicz (p.bartosiewi@partner.samsung.com)
22 | * @brief A worker thread that executes tasks
23 | */
24 |
25 | #ifndef COMMON_UTILS_WORKER_HPP
26 | #define COMMON_UTILS_WORKER_HPP
27 |
28 | #include
29 | #include
30 |
31 | namespace utils {
32 |
33 | /**
34 | * A queue with tasks executed in a dedicated thread.
35 | * Current implementation creates a thread on the first use.
36 | */
37 | class Worker {
38 | public:
39 | typedef std::shared_ptr Pointer;
40 | typedef std::function Task;
41 |
42 | ~Worker();
43 |
44 | /**
45 | * Creates a worker with its own thread
46 | */
47 | static Pointer create();
48 |
49 | /**
50 | * Creates a worker that share a thread with its parent
51 | */
52 | Pointer createSubWorker();
53 |
54 | /**
55 | * Adds a task to the queue.
56 | */
57 | void addTask(const Task& task);
58 | void addTaskAndWait(const Task& task);
59 |
60 | private:
61 | typedef unsigned int GroupID;
62 | class WorkerQueue;
63 |
64 | const std::shared_ptr mWorkerQueue;
65 | const GroupID mGroupID;
66 |
67 | Worker(const std::shared_ptr& workerQueue);
68 | };
69 |
70 | } // namespace utils
71 |
72 |
73 | #endif // COMMON_UTILS_WORKER_HPP
74 |
--------------------------------------------------------------------------------
/doc/cargo_usage.md:
--------------------------------------------------------------------------------
1 | cargo libraries usage example {#mainpage}
2 | =============================
3 |
4 | @ingroup libcargo
5 |
6 | @code
7 | #include "cargo/fields.hpp"
8 | #include "cargo-gvariant/cargo-gvariant.hpp"
9 | #include "cargo-json/cargo-json.hpp"
10 | #include "cargo-sqlite/cargo-sqlite.hpp"
11 | #include "cargo-sqlite-json/cargo-sqlite-json.hpp"
12 | #include "cargo-fd/cargo-fd.hpp"
13 | #include
14 | #include
15 |
16 | struct Foo
17 | {
18 | std::string bar = "plain-text";
19 | std::vector tab = std::vector{1, 2, 4, 8};
20 | double number = 3.14;
21 |
22 | CARGO_REGISTER
23 | (
24 | bar,
25 | tab,
26 | number
27 | )
28 | };
29 |
30 | int main()
31 | {
32 | Foo foo;
33 |
34 | const std::string jsonString = cargo::saveToJsonString(foo);
35 | cargo::loadFromJsonString(jsonString, foo);
36 |
37 | const GVariant* gVariantPointer = cargo::saveToGVariant(foo);
38 | cargo::loadFromGVariant(gVariantPointer, foo);
39 | g_variant_unref(gVariantPointer);
40 |
41 | constexpr std::string jsonFile = "foo.json";
42 | cargo::saveToJsonFile(jsonFile, foo);
43 | cargo::loadFromJsonFile(jsonFile, foo);
44 |
45 | constexpr std::string kvDBPath = "kvdb";
46 | constexpr std::string key = "foo";
47 | cargo::saveToKVStore(kvDBPath, foo, key);
48 | cargo::loadFromKVStore(kvDBPath, foo, key);
49 |
50 | cargo::loadFromKVStoreWithJson(kvDBPath, jsonString, foo, key);
51 | cargo::loadFromKVStoreWithJsonFile(kvDBPath, jsonFile, foo, key);
52 |
53 | FILE* file = fopen("blob", "wb");
54 | if (!file)
55 | {
56 | return EXIT_FAILURE;
57 | }
58 | const int fd = ::fileno(file);
59 | cargo::saveToFD(fd, foo);
60 | ::fclose(file);
61 | file = ::fopen("blob", "rb");
62 | if(!file) {
63 | return EXIT_FAILURE;
64 | }
65 | cargo::loadFromFD(fd, foo);
66 | ::fclose(file);
67 |
68 | return 0;
69 | }
70 | @endcode
71 |
--------------------------------------------------------------------------------
/doc/footer.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
12 |
13 |
14 |
19 |
20 |