2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
19 * Copyright (C) 2019 Red Hat, Inc.
24 #include <sys/types.h>
31 A helper struct for creating/manipulating UNIX-domain sockets.
33 A UNIX-domain socket is either named or unnamed. If named, the name is either
34 a path in the filesystem namespace, or an abstract-domain identifier. Abstract-domain
35 names start with a null byte, and may contain non-printable characters.
37 std::string() can hold a sequence of arbitrary bytes, and has a length() attribute
38 that does not rely on using strlen(). Thus it is used here to hold the address.
40 struct unix_domain_addr {
41 const std::string name;
42 const int path_count; // either name.length() or name.length()+1. See path_length_aux() below.
44 explicit unix_domain_addr(const std::string& fn) : name{fn}, path_count{path_length_aux()} {}
46 explicit unix_domain_addr(const char* fn) : name{fn}, path_count{path_length_aux()} {}
48 int path_length() const { return path_count; }
50 // the following holds:
51 // for abstract name: name.length() == number of meaningful bytes, including the null in name[0].
52 // for filesystem path: name.length() does not count the implicit terminating null.
53 // Here we tweak the outside-visible length of the address.
54 int path_length_aux() const {
55 auto pl = (int)name.length();
56 if (!pl || (name[0] == '\0')) {
57 // unnamed, or abstract-namespace
63 const char* path_bytes() const { return name.c_str(); }
65 bool operator==(const unix_domain_addr& a) const {
66 return name == a.name;
68 bool operator!=(const unix_domain_addr& a) const {
73 std::ostream& operator<<(std::ostream&, const unix_domain_addr&);
75 } // namespace seastar