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
35
namespace
net::un::
phy
{
36
37
template
<
template
<
typename
SocketAddress
>
typename
PhysicalPeerSocket
>
38
PhysicalSocket
<
PhysicalPeerSocket
>::
PhysicalSocket
(
int
type
,
int
protocol
)
39
:
Super
(PF_UNIX,
type
,
protocol
) {
40
}
41
42
template
<
template
<
typename
SocketAddress
>
typename
PhysicalPeerSocket
>
43
PhysicalSocket
<
PhysicalPeerSocket
>::
PhysicalSocket
(
PhysicalSocket
&&
physicalSocket
)
noexcept
44
:
Super
(
std
::
move
(
physicalSocket
))
45
,
lockFd
(
std
::
exchange
(
physicalSocket
.
lockFd
, -1)) {
46
}
47
48
template
<
template
<
typename
SocketAddress
>
typename
PhysicalPeerSocket
>
49
PhysicalSocket
<
PhysicalPeerSocket
>&
PhysicalSocket
<
PhysicalPeerSocket
>::
operator
=(
PhysicalSocket
&&
physicalSocket
)
noexcept
{
50
Super
::
operator
=(
std
::
move
(
physicalSocket
));
51
lockFd
=
std
::
exchange
(
physicalSocket
.
lockFd
, -1);
52
53
return
*
this
;
54
}
55
56
template
<
template
<
typename
SocketAddress
>
typename
PhysicalPeerSocket
>
57
PhysicalSocket
<
PhysicalPeerSocket
>::~
PhysicalSocket
() {
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
77
core
::
system
::
close
(
lockFd
);
78
lockFd
= -1;
79
}
80
}
81
82
template
<
template
<
typename
SocketAddress
>
typename
PhysicalPeerSocket
>
83
int
PhysicalSocket
<
PhysicalPeerSocket
>::
bind
(
SocketAddress
&
bindAddress
) {
84
if
(!
bindAddress
.
getSunPath
().
empty
() && !
bindAddress
.
getSunPath
().
starts_with
(
'\0'
)) {
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
();
89
if
(
std
::
filesystem
::
exists
(
bindAddress
.
getSunPath
().
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
99
core
::
system
::
close
(
lockFd
);
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
net::un::phy::stream::PhysicalSocketClient
Definition
PhysicalSocketClient.h:34
net::un::phy::stream::PhysicalSocketClient::~PhysicalSocketClient
~PhysicalSocketClient() override
Definition
PhysicalSocketClient.cpp:33
net::un::phy::stream::PhysicalSocketClient::PhysicalSocketClient
PhysicalSocketClient(PhysicalSocketClient &&) noexcept=default
net::un::phy::stream::PhysicalSocketClient::connectInProgress
static bool connectInProgress(int cErrno)
Definition
PhysicalSocketClient.cpp:36
net::un::phy::stream
Definition
PhysicalSocket.cpp:28
net::un::phy
Definition
PhysicalSocket.hpp:35
net
un
phy
PhysicalSocket.hpp
Generated on Mon Feb 10 2025 20:21:32 for SNode.C by
1.11.0