Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
abstract_socket_test.cc
Go to the documentation of this file.
1// Copyright (c) 2021, the Dart project authors. Please see the AUTHORS file
2// for details. All rights reserved. Use of this source code is governed by a
3// BSD-style license that can be found in the LICENSE file.
4
5// This is a utility program for testing that a Dart program can connect to an
6// abstract UNIX socket created by a non-Dart program. It creates such a socket
7// accepts one connection, echoes back the first message it receives, and then
8// closes the connection and UNIX socket.
9
10#include "platform/globals.h"
11#if defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
12
13#include <stdio.h>
14#include <stdlib.h>
15#include <sys/socket.h>
16#include <sys/un.h>
17#include <unistd.h>
18
19const intptr_t kOffsetOfPtr = 32;
20
21#define OFFSET_OF(type, field) \
22 (reinterpret_cast<intptr_t>( \
23 &(reinterpret_cast<type*>(kOffsetOfPtr)->field)) - \
24 kOffsetOfPtr) // NOLINT
25
26int main(int argc, char* argv[]) {
27 struct sockaddr_un addr;
28 char* socket_path;
29 int server_socket;
30 char buf[1024];
31
32 if (argc < 2) {
33 fprintf(
34 stderr,
35 "Usage: abstract_socket_test <address>\n\n"
36 "<address> should be an abstract UNIX socket address like @hidden\n");
37 exit(-1);
38 }
39
40 socket_path = argv[1];
41 if (socket_path[0] != '@') {
42 fprintf(stderr,
43 "The first argument should be an abstract socket "
44 "address and start with '@'\n");
45 exit(-1);
46 }
47
48 if ((server_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
49 perror("socket error");
50 exit(-1);
51 }
52
53 memset(&addr, 0, sizeof(addr));
54 addr.sun_family = AF_UNIX;
55 addr.sun_path[0] = '\0';
56 strncpy(addr.sun_path + 1, socket_path + 1, sizeof(addr.sun_path) - 2);
57
58 int address_length =
59 OFFSET_OF(struct sockaddr_un, sun_path) + strlen(socket_path);
60 if (bind(server_socket, (struct sockaddr*)&addr, address_length) == -1) {
61 perror("bind error");
62 exit(-1);
63 }
64
65 if (listen(server_socket, 5) == -1) {
66 perror("listen error");
67 exit(-1);
68 }
69
70 int client_socket;
71 if ((client_socket = accept(server_socket, nullptr, nullptr)) == -1) {
72 perror("accept error");
73 exit(-1);
74 }
75
76 int read_count;
77 while ((read_count = read(client_socket, buf, sizeof(buf))) > 0) {
78 int write_count = 0;
79 while (write_count < read_count) {
80 int w;
81 if ((w = write(client_socket, buf, read_count)) < 0) {
82 perror("write");
83 exit(-1);
84 }
85 write_count += w;
86 }
87 }
88 if (read_count == -1) {
89 perror("read");
90 exit(-1);
91 }
92
93 close(client_socket);
94 close(server_socket);
95 return 0;
96}
97
98#else
99
100int main() {
101 return -1;
102}
103
104#endif // defined(DART_HOST_OS_LINUX) || defined(DART_HOST_OS_ANDROID)
static bool read(SkStream *stream, void *buffer, size_t amount)
int main()
char ** argv
Definition library.h:9
exit(kErrorExitCode)
const intptr_t kOffsetOfPtr
Definition globals.h:136
SkScalar w
void write(SkWStream *wStream, const T &text)
Definition skqp.cpp:188
#define OFFSET_OF(type, field)
Definition globals.h:138