SNode.C
Loading...
Searching...
No Matches
PhysicalSocket.hpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "net/un/phy/PhysicalSocket.h" // IWYU pragma: export
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include "core/system/unistd.h"
25#include "log/Logger.h"
26
27#include <cerrno>
28#include <cstdio>
29#include <filesystem>
30#include <string>
31#include <utility>
32
33#endif /* DOXYGEN_SHOULD_SKIP_THIS */
34
35namespace net::un::phy {
36
37 template <template <typename SocketAddress> typename PhysicalPeerSocket>
39 : Super(PF_UNIX, type, protocol) {
40 }
42 template <template <typename SocketAddress> typename PhysicalPeerSocket>
46 }
47
48 template <template <typename SocketAddress> typename PhysicalPeerSocket>
55
56 template <template <typename SocketAddress> typename PhysicalPeerSocket>
58 if (lockFd >= 0) {
59 if (std::remove(Super::getBindAddress().getSunPath().data()) == 0) {
60 LOG(DEBUG) << "Remove sun path: " << Super::getBindAddress().getSunPath();
61 } else {
62 PLOG(ERROR) << "Remove sun path: " << Super::getBindAddress().getSunPath();
63 }
64
65 if (core::system::flock(lockFd, LOCK_UN) == 0) {
66 LOG(DEBUG) << "Remove lock from file: " << Super::getBindAddress().getSunPath().append(".lock");
67 } else {
68 PLOG(ERROR) << "Remove lock from file: " << Super::getBindAddress().getSunPath().append(".lock");
69 }
70
71 if (std::remove(Super::bindAddress.getSunPath().append(".lock").data()) == 0) {
72 LOG(DEBUG) << "Remove lock file: " << Super::getBindAddress().getSunPath().append(".lock");
73 } else {
74 PLOG(ERROR) << "Remove lock file: " << Super::getBindAddress().getSunPath().append(".lock");
75 }
76
78 lockFd = -1;
79 }
80 }
81
82 template <template <typename SocketAddress> typename PhysicalPeerSocket>
85 if ((lockFd = open(bindAddress.getSunPath().append(".lock").data(), O_RDONLY | O_CREAT, 0600)) >= 0) {
86 LOG(DEBUG) << "Opening lock file: " << bindAddress.getSunPath().append(".lock").data();
87 if (core::system::flock(lockFd, LOCK_EX | LOCK_NB) == 0) {
88 LOG(DEBUG) << "Locking lock file: " << bindAddress.getSunPath().append(".lock").data();
90 if (std::remove(bindAddress.getSunPath().data()) == 0) {
91 LOG(WARNING) << "Removed stalled sun_path: " << bindAddress.getSunPath().data();
92 } else {
93 PLOG(ERROR) << "Removed stalled sun path: " << bindAddress.getSunPath().data();
94 }
95 }
96 } else {
97 PLOG(ERROR) << "Locking lock file " << bindAddress.getSunPath().append(".lock").data();
98
100 lockFd = -1;
101 }
102 } else {
103 PLOG(ERROR) << "Opening lock file: " << bindAddress.getSunPath().append(".lock").data();
104 }
105 }
106
107 return Super::bind(bindAddress);
108 }
109
110} // namespace net::un::phy
PhysicalSocketClient(PhysicalSocketClient &&) noexcept=default