1 /* Serial interface for local domain connections on Un*x like systems. 2 3 Copyright (C) 1992-2020 Free Software Foundation, Inc. 4 5 This file is part of GDB. 6 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. 11 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. 16 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/>. */ 19 20 #include "defs.h" 21 #include "serial.h" 22 #include "ser-base.h" 23 24 #include <sys/socket.h> 25 #include <sys/un.h> 26 27 #ifndef UNIX_PATH_MAX 28 #define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path) 29 #endif 30 31 /* Open an AF_UNIX socket. */ 32 33 static int 34 uds_open (struct serial *scb, const char *name) 35 { 36 struct sockaddr_un addr; 37 38 if (strlen (name) > UNIX_PATH_MAX - 1) 39 { 40 warning 41 (_("The socket name is too long. It may be no longer than %s bytes."), 42 pulongest (UNIX_PATH_MAX - 1L)); 43 return -1; 44 } 45 46 memset (&addr, 0, sizeof addr); 47 addr.sun_family = AF_UNIX; 48 strncpy (addr.sun_path, name, UNIX_PATH_MAX - 1); 49 50 int sock = socket (AF_UNIX, SOCK_STREAM, 0); 51 52 if (connect (sock, (struct sockaddr *) &addr, 53 sizeof (struct sockaddr_un)) < 0) 54 { 55 close (sock); 56 scb->fd = -1; 57 return -1; 58 } 59 60 scb->fd = sock; 61 62 return 0; 63 } 64 65 static void 66 uds_close (struct serial *scb) 67 { 68 if (scb->fd == -1) 69 return; 70 71 close (scb->fd); 72 scb->fd = -1; 73 } 74 75 static int 76 uds_read_prim (struct serial *scb, size_t count) 77 { 78 return recv (scb->fd, scb->buf, count, 0); 79 } 80 81 static int 82 uds_write_prim (struct serial *scb, const void *buf, size_t count) 83 { 84 return send (scb->fd, buf, count, 0); 85 } 86 87 /* The local socket ops. */ 88 89 static const struct serial_ops uds_ops = 90 { 91 "local", 92 uds_open, 93 uds_close, 94 NULL, 95 ser_base_readchar, 96 ser_base_write, 97 ser_base_flush_output, 98 ser_base_flush_input, 99 ser_base_send_break, 100 ser_base_raw, 101 ser_base_get_tty_state, 102 ser_base_copy_tty_state, 103 ser_base_set_tty_state, 104 ser_base_print_tty_state, 105 ser_base_setbaudrate, 106 ser_base_setstopbits, 107 ser_base_setparity, 108 ser_base_drain_output, 109 ser_base_async, 110 uds_read_prim, 111 uds_write_prim 112 }; 113 114 void _initialize_ser_socket (); 115 void 116 _initialize_ser_socket () 117 { 118 serial_add_interface (&uds_ops); 119 } 120