1// Copyright 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef IPC_UNIX_DOMAIN_SOCKET_UTIL_H_
6#define IPC_UNIX_DOMAIN_SOCKET_UTIL_H_
7
8#include <sys/types.h>
9
10#include <string>
11
12#include "ipc/ipc_export.h"
13
14namespace base {
15class FilePath;
16}  // namespace base
17
18namespace IPC {
19
20// Creates a UNIX-domain socket at |socket_name| and bind()s it, then listen()s
21// on it. If successful, |server_listen_fd| will be set to the new file
22// descriptor, and the function will return true. Otherwise returns false.
23//
24// This function also effectively performs `mkdir -p` on the dirname of
25// |socket_name| to ensure that all the directories up to |socket_name| exist.
26// As a result of which this function must be run on a thread that allows
27// blocking I/O, e.g. the FILE thread in Chrome's browser process.
28IPC_EXPORT bool CreateServerUnixDomainSocket(const base::FilePath& socket_name,
29                                             int* server_listen_fd);
30
31// Opens a UNIX-domain socket at |socket_name| and connect()s to it. If
32// successful, |client_socket| will be set to the new file descriptor, and the
33// function will return true. Otherwise returns false.
34IPC_EXPORT bool CreateClientUnixDomainSocket(const base::FilePath& socket_name,
35                                             int* client_socket);
36
37// Gets the effective user ID of the other end of the UNIX-domain socket
38// specified by |fd|. If successful, sets |peer_euid| to the uid, and returns
39// true. Otherwise returns false.
40IPC_EXPORT bool GetPeerEuid(int fd, uid_t* peer_euid);
41
42// Checks that the process on the other end of the UNIX domain socket
43// represented by |peer_fd| shares the same EUID as this process.
44IPC_EXPORT bool IsPeerAuthorized(int peer_fd);
45
46// Accepts a client attempting to connect to |server_listen_fd|, storing the
47// new file descriptor for the connection in |server_socket|.
48//
49// Returns false if |server_listen_fd| encounters an unrecoverable error.
50// Returns true if it's valid to keep listening on |server_listen_fd|. In this
51// case, it's possible that a connection wasn't successfully established; then,
52// |server_socket| will be set to -1.
53IPC_EXPORT bool ServerAcceptConnection(int server_listen_fd,
54                                       int* server_socket);
55
56// The maximum length of the name of a socket for MODE_NAMED_SERVER or
57// MODE_NAMED_CLIENT if you want to pass in your own socket.
58// The standard size on linux is 108, mac is 104. To maintain consistency
59// across platforms we standardize on the smaller value.
60static const size_t kMaxSocketNameLength = 104;
61
62}  // namespace IPC
63
64#endif  // IPC_UNIX_DOMAIN_SOCKET_UTIL_H_
65