aboutsummaryrefslogtreecommitdiff
path: root/gdb/ser-uds.c
blob: 55089dd306d9ae2f3c6b7741d4466630d3e2fd4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/* Serial interface for local domain connections on Un*x like systems.

   Copyright (C) 1992-2021 Free Software Foundation, Inc.

   This file is part of GDB.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

#include "defs.h"
#include "serial.h"
#include "ser-base.h"

#include <sys/socket.h>
#include <sys/un.h>

#ifndef UNIX_PATH_MAX
#define UNIX_PATH_MAX sizeof(((struct sockaddr_un *) NULL)->sun_path)
#endif

/* Open an AF_UNIX socket.  */

static int
uds_open (struct serial *scb, const char *name)
{
  struct sockaddr_un addr;

  if (strlen (name) > UNIX_PATH_MAX - 1)
    {
      warning
	(_("The socket name is too long.  It may be no longer than %s bytes."),
	 pulongest (UNIX_PATH_MAX - 1L));
      return -1;
    }

  memset (&addr, 0, sizeof addr);
  addr.sun_family = AF_UNIX;
  strncpy (addr.sun_path, name, UNIX_PATH_MAX - 1);

  int sock = socket (AF_UNIX, SOCK_STREAM, 0);

  if (connect (sock, (struct sockaddr *) &addr,
	       sizeof (struct sockaddr_un)) < 0)
    {
      close (sock);
      scb->fd = -1;
      return -1;
    }

  scb->fd = sock;

  return 0;
}

static void
uds_close (struct serial *scb)
{
  if (scb->fd == -1)
    return;

  close (scb->fd);
  scb->fd = -1;
}

static int
uds_read_prim (struct serial *scb, size_t count)
{
  return recv (scb->fd, scb->buf, count, 0);
}

static int
uds_write_prim (struct serial *scb, const void *buf, size_t count)
{
  return send (scb->fd, buf, count, 0);
}

/* The local socket ops.  */

static const struct serial_ops uds_ops =
{
  "local",
  uds_open,
  uds_close,
  NULL,
  ser_base_readchar,
  ser_base_write,
  ser_base_flush_output,
  ser_base_flush_input,
  ser_base_send_break,
  ser_base_raw,
  ser_base_get_tty_state,
  ser_base_copy_tty_state,
  ser_base_set_tty_state,
  ser_base_print_tty_state,
  ser_base_setbaudrate,
  ser_base_setstopbits,
  ser_base_setparity,
  ser_base_drain_output,
  ser_base_async,
  uds_read_prim,
  uds_write_prim
};

void _initialize_ser_socket ();
void
_initialize_ser_socket ()
{
  serial_add_interface (&uds_ops);
}