107163879Schristos /* Serial interface for local domain connections on Un*x like systems.
207163879Schristos
3*1424dfb3Schristos Copyright (C) 1992-2020 Free Software Foundation, Inc.
407163879Schristos
507163879Schristos This file is part of GDB.
607163879Schristos
707163879Schristos This program is free software; you can redistribute it and/or modify
807163879Schristos it under the terms of the GNU General Public License as published by
907163879Schristos the Free Software Foundation; either version 3 of the License, or
1007163879Schristos (at your option) any later version.
1107163879Schristos
1207163879Schristos This program is distributed in the hope that it will be useful,
1307163879Schristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1407163879Schristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1507163879Schristos GNU General Public License for more details.
1607163879Schristos
1707163879Schristos You should have received a copy of the GNU General Public License
1807163879Schristos along with this program. If not, see <http://www.gnu.org/licenses/>. */
1907163879Schristos
2007163879Schristos #include "defs.h"
2107163879Schristos #include "serial.h"
2207163879Schristos #include "ser-base.h"
2307163879Schristos
2407163879Schristos #include <sys/socket.h>
2507163879Schristos #include <sys/un.h>
2607163879Schristos
2707163879Schristos #ifndef UNIX_PATH_MAX
2807163879Schristos #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
2907163879Schristos #endif
3007163879Schristos
3107163879Schristos /* Open an AF_UNIX socket. */
3207163879Schristos
3307163879Schristos static int
uds_open(struct serial * scb,const char * name)3407163879Schristos uds_open (struct serial *scb, const char *name)
3507163879Schristos {
3607163879Schristos struct sockaddr_un addr;
3707163879Schristos
3807163879Schristos if (strlen (name) > UNIX_PATH_MAX - 1)
3907163879Schristos {
4007163879Schristos warning
4107163879Schristos (_("The socket name is too long. It may be no longer than %s bytes."),
4207163879Schristos pulongest (UNIX_PATH_MAX - 1L));
4307163879Schristos return -1;
4407163879Schristos }
4507163879Schristos
4607163879Schristos memset (&addr, 0, sizeof addr);
4707163879Schristos addr.sun_family = AF_UNIX;
4807163879Schristos strncpy (addr.sun_path, name, UNIX_PATH_MAX - 1);
4907163879Schristos
5007163879Schristos int sock = socket (AF_UNIX, SOCK_STREAM, 0);
5107163879Schristos
5207163879Schristos if (connect (sock, (struct sockaddr *) &addr,
5307163879Schristos sizeof (struct sockaddr_un)) < 0)
5407163879Schristos {
5507163879Schristos close (sock);
5607163879Schristos scb->fd = -1;
5707163879Schristos return -1;
5807163879Schristos }
5907163879Schristos
6007163879Schristos scb->fd = sock;
6107163879Schristos
6207163879Schristos return 0;
6307163879Schristos }
6407163879Schristos
6507163879Schristos static void
uds_close(struct serial * scb)6607163879Schristos uds_close (struct serial *scb)
6707163879Schristos {
6807163879Schristos if (scb->fd == -1)
6907163879Schristos return;
7007163879Schristos
7107163879Schristos close (scb->fd);
7207163879Schristos scb->fd = -1;
7307163879Schristos }
7407163879Schristos
7507163879Schristos static int
uds_read_prim(struct serial * scb,size_t count)7607163879Schristos uds_read_prim (struct serial *scb, size_t count)
7707163879Schristos {
7807163879Schristos return recv (scb->fd, scb->buf, count, 0);
7907163879Schristos }
8007163879Schristos
8107163879Schristos static int
uds_write_prim(struct serial * scb,const void * buf,size_t count)8207163879Schristos uds_write_prim (struct serial *scb, const void *buf, size_t count)
8307163879Schristos {
8407163879Schristos return send (scb->fd, buf, count, 0);
8507163879Schristos }
8607163879Schristos
8707163879Schristos /* The local socket ops. */
8807163879Schristos
8907163879Schristos static const struct serial_ops uds_ops =
9007163879Schristos {
9107163879Schristos "local",
9207163879Schristos uds_open,
9307163879Schristos uds_close,
9407163879Schristos NULL,
9507163879Schristos ser_base_readchar,
9607163879Schristos ser_base_write,
9707163879Schristos ser_base_flush_output,
9807163879Schristos ser_base_flush_input,
9907163879Schristos ser_base_send_break,
10007163879Schristos ser_base_raw,
10107163879Schristos ser_base_get_tty_state,
10207163879Schristos ser_base_copy_tty_state,
10307163879Schristos ser_base_set_tty_state,
10407163879Schristos ser_base_print_tty_state,
10507163879Schristos ser_base_setbaudrate,
10607163879Schristos ser_base_setstopbits,
10707163879Schristos ser_base_setparity,
10807163879Schristos ser_base_drain_output,
10907163879Schristos ser_base_async,
11007163879Schristos uds_read_prim,
11107163879Schristos uds_write_prim
11207163879Schristos };
11307163879Schristos
114*1424dfb3Schristos void _initialize_ser_socket ();
11507163879Schristos void
_initialize_ser_socket()116*1424dfb3Schristos _initialize_ser_socket ()
11707163879Schristos {
11807163879Schristos serial_add_interface (&uds_ops);
11907163879Schristos }
120