1 /* Serial interface for local domain connections on Un*x like systems.
3 Copyright (C) 1992-2018 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
24 #include <sys/socket.h>
30 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
33 /* Open an AF_UNIX socket. */
36 uds_open (struct serial
*scb
, const char *name
)
40 memset (&hint
, 0, sizeof (hint
));
41 /* Assume no prefix will be passed, therefore we should use
43 hint
.ai_family
= AF_UNSPEC
;
44 hint
.ai_socktype
= SOCK_STREAM
;
46 parsed_connection_spec parsed
= parse_connection_spec (name
, &hint
);
48 const char *socket_name
= parsed
.port_str
.empty() ? name
: parsed
.port_str
.c_str ();
50 struct sockaddr_un addr
;
52 if (strlen (socket_name
) > UNIX_PATH_MAX
- 1)
55 (_("The socket name is too long. It may be no longer than %s bytes."),
56 pulongest (UNIX_PATH_MAX
- 1L));
60 memset (&addr
, 0, sizeof addr
);
61 addr
.sun_family
= AF_UNIX
;
62 strncpy (addr
.sun_path
, socket_name
, UNIX_PATH_MAX
- 1);
64 int sock
= socket (AF_UNIX
, SOCK_STREAM
, 0);
66 if (connect (sock
, (struct sockaddr
*) &addr
,
67 sizeof (struct sockaddr_un
)) < 0)
80 uds_close (struct serial
*scb
)
90 uds_read_prim (struct serial
*scb
, size_t count
)
92 return recv (scb
->fd
, scb
->buf
, count
, 0);
96 uds_write_prim (struct serial
*scb
, const void *buf
, size_t count
)
98 return send (scb
->fd
, buf
, count
, 0);
101 /* The local socket ops. */
103 static const struct serial_ops uds_ops
=
111 ser_base_flush_output
,
112 ser_base_flush_input
,
115 ser_base_get_tty_state
,
116 ser_base_copy_tty_state
,
117 ser_base_set_tty_state
,
118 ser_base_print_tty_state
,
119 ser_base_setbaudrate
,
120 ser_base_setstopbits
,
122 ser_base_drain_output
,
129 _initialize_ser_socket (void)
131 serial_add_interface (&uds_ops
);