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/*
21 * MIT License
22 *
23 * Permission is hereby granted, free of charge, to any person obtaining a copy
24 * of this software and associated documentation files (the "Software"), to deal
25 * in the Software without restriction, including without limitation the rights
26 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
27 * copies of the Software, and to permit persons to whom the Software is
28 * furnished to do so, subject to the following conditions:
29 *
30 * The above copyright notice and this permission notice shall be included in
31 * all copies or substantial portions of the Software.
32 *
33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
34 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
35 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
36 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
37 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
38 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
39 * THE SOFTWARE.
40 */
41
42#include "net/un/phy/PhysicalSocket.h" // IWYU pragma: export
43
44#ifndef DOXYGEN_SHOULD_SKIP_THIS
45
46#include "core/system/unistd.h"
47#include "log/Logger.h"
48
49#include <cerrno>
50#include <cstdio>
51#include <filesystem>
52#include <string>
53#include <utility>
54
55#endif /* DOXYGEN_SHOULD_SKIP_THIS */
56
57namespace net::un::phy {
58
59 template <template <typename SocketAddress> typename PhysicalPeerSocket>
60 PhysicalSocket<PhysicalPeerSocket>::PhysicalSocket(int type, int protocol)
61 : Super(PF_UNIX, type, protocol) {
62 }
64 template <template <typename SocketAddress> typename PhysicalPeerSocket>
65 PhysicalSocket<PhysicalPeerSocket>::PhysicalSocket(PhysicalSocket&& physicalSocket) noexcept
66 : Super(std::move(physicalSocket))
67 , lockFd(std::exchange(physicalSocket.lockFd, -1)) {
68 }
69
70 template <template <typename SocketAddress> typename PhysicalPeerSocket>
71 PhysicalSocket<PhysicalPeerSocket>& PhysicalSocket<PhysicalPeerSocket>::operator=(PhysicalSocket&& physicalSocket) noexcept {
72 Super::operator=(std::move(physicalSocket));
73 lockFd = std::exchange(physicalSocket.lockFd, -1);
74
75 return *this;
76 }
77
78 template <template <typename SocketAddress> typename PhysicalPeerSocket>
79 PhysicalSocket<PhysicalPeerSocket>::~PhysicalSocket() {
80 if (lockFd >= 0) {
81 if (std::remove(Super::getBindAddress().getSunPath().data()) == 0) {
82 LOG(DEBUG) << "Remove sun path: " << Super::getBindAddress().getSunPath();
83 } else {
84 PLOG(ERROR) << "Remove sun path: " << Super::getBindAddress().getSunPath();
85 }
86
87 if (core::system::flock(lockFd, LOCK_UN) == 0) {
88 LOG(DEBUG) << "Remove lock from file: " << Super::getBindAddress().getSunPath().append(".lock");
89 } else {
90 PLOG(ERROR) << "Remove lock from file: " << Super::getBindAddress().getSunPath().append(".lock");
91 }
92
93 if (std::remove(Super::bindAddress.getSunPath().append(".lock").data()) == 0) {
94 LOG(DEBUG) << "Remove lock file: " << Super::getBindAddress().getSunPath().append(".lock");
95 } else {
96 PLOG(ERROR) << "Remove lock file: " << Super::getBindAddress().getSunPath().append(".lock");
97 }
98
100 lockFd = -1;
101 }
102 }
103
104 template <template <typename SocketAddress> typename PhysicalPeerSocket>
105 int PhysicalSocket<PhysicalPeerSocket>::bind(SocketAddress& bindAddress) {
106 if (!bindAddress.getSunPath().empty() && !bindAddress.getSunPath().starts_with('\0')) {
107 if ((lockFd = open(bindAddress.getSunPath().append(".lock").data(), O_RDONLY | O_CREAT, 0600)) >= 0) {
108 LOG(DEBUG) << "Opening lock file: " << bindAddress.getSunPath().append(".lock").data();
109 if (core::system::flock(lockFd, LOCK_EX | LOCK_NB) == 0) {
110 LOG(DEBUG) << "Locking lock file: " << bindAddress.getSunPath().append(".lock").data();
111 if (std::filesystem::exists(bindAddress.getSunPath().data())) {
112 if (std::remove(bindAddress.getSunPath().data()) == 0) {
113 LOG(WARNING) << "Removed stalled sun_path: " << bindAddress.getSunPath().data();
114 } else {
115 PLOG(ERROR) << "Removed stalled sun path: " << bindAddress.getSunPath().data();
116 }
117 }
118 } else {
119 PLOG(ERROR) << "Locking lock file " << bindAddress.getSunPath().append(".lock").data();
120
122 lockFd = -1;
123 }
124 } else {
125 PLOG(ERROR) << "Opening lock file: " << bindAddress.getSunPath().append(".lock").data();
126 }
127 }
128
129 return Super::bind(bindAddress);
130 }
131
132} // namespace net::un::phy
int getFd() const
const SockLen & getSockAddrLen() const
const sockaddr & getSockAddr()
std::string getSunPath() const
ssize_t recvFd(int *recvfd)
Definition Socket.cpp:97
ssize_t sendFd(SocketAddress &&destAddress, int sendfd)
Definition Socket.cpp:63
ssize_t sendFd(SocketAddress &destAddress, int sendfd)
Definition Socket.cpp:67
PhysicalSocket & operator=(PhysicalSocket &&physicalSocket) noexcept
PhysicalSocket(int type, int protocol)
PhysicalSocket(PhysicalSocket &&physicalSocket) noexcept
int bind(SocketAddress &bindAddress)
int close(int fd)
Definition unistd.cpp:67
int flock(int lockFd, int operation)
Definition unistd.cpp:77