File: pipe.c

package info (click to toggle)
brltty 6.7-3.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 31,240 kB
  • sloc: ansic: 148,859; java: 13,418; sh: 9,623; xml: 5,699; tcl: 2,634; makefile: 2,333; awk: 713; lisp: 366; python: 321; ml: 301
file content (362 lines) | stat: -rw-r--r-- 9,087 bytes parent folder | download | duplicates (3)
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
 * BRLTTY - A background process providing access to the console screen (when in
 *          text mode) for a blind person using a refreshable braille display.
 *
 * Copyright (C) 1995-2024 by The BRLTTY Developers.
 *
 * BRLTTY comes with ABSOLUTELY NO WARRANTY.
 *
 * This is free software, placed under the terms of the
 * GNU Lesser General Public License, as published by the Free Software
 * Foundation; either version 2.1 of the License, or (at your option) any
 * later version. Please see the file LICENSE-LGPL for details.
 *
 * Web Page: http://brltty.app/
 *
 * This software is maintained by Dave Mielke <dave@mielke.cc>.
 */

#include "prologue.h"

#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>

#include "log.h"
#include "pipe.h"
#include "file.h"
#include "io_misc.h"
#include "async_handle.h"
#include "async_io.h"

struct NamedPipeObjectStruct {
  NamedPipeInputCallback *callback;
  void *data;

  int (*createPipe) (NamedPipeObject *obj);
  int (*monitorPipe) (NamedPipeObject *obj);
  void (*resetPipe) (NamedPipeObject *obj);
  void (*releaseResources) (NamedPipeObject *obj);

  struct {
    char *path;
  } host;

  struct {
    FileDescriptor descriptor;
    AsyncHandle monitor;
  } input;

#if defined(__MINGW32__)
  struct {
    struct {
      AsyncHandle monitor;
      HANDLE event;
      OVERLAPPED overlapped;
    } connect;
  } windows;

#endif /*  */
};

static void
initializeHostPath (NamedPipeObject *obj) {
  obj->host.path = NULL;
}

static void
deallocateHostPath (NamedPipeObject *obj) {
  free(obj->host.path);
  initializeHostPath(obj);
}

static void
removePipe (NamedPipeObject *obj) {
  unlink(obj->host.path);
}

static void
initializeInputDescriptor (NamedPipeObject *obj) {
  obj->input.descriptor = INVALID_FILE_DESCRIPTOR;
}

static void
closeInputDescriptor (NamedPipeObject *obj) {
  closeFileDescriptor(obj->input.descriptor);
  initializeInputDescriptor(obj);
}

static void
initializeInputMonitor (NamedPipeObject *obj) {
  obj->input.monitor = NULL;
}

static void
stopInputMonitor (NamedPipeObject *obj) {
  asyncCancelRequest(obj->input.monitor);
  initializeInputMonitor(obj);
}

ASYNC_INPUT_CALLBACK(handleNamedPipeInput) {
  NamedPipeObject *obj = parameters->data;

  if (parameters->error) {
    logMessage(LOG_WARNING, "named pipe input error: %s: %s",
               obj->host.path, strerror(parameters->error));
  } else if (parameters->end) {
    logMessage(LOG_WARNING, "named pipe end-of-file: %s", obj->host.path);
  } else {
    const NamedPipeInputCallbackParameters input = {
      .buffer = parameters->buffer,
      .length = parameters->length,
      .data = obj->data
    };

    return obj->callback(&input);
  }

  asyncDiscardHandle(obj->input.monitor);
  initializeInputMonitor(obj);

  if (obj->resetPipe) obj->resetPipe(obj);
  obj->monitorPipe(obj);

  return 0;
}

static int
monitorInput (NamedPipeObject *obj) {
  if (!obj->input.monitor) {
    if (!asyncReadFile(&obj->input.monitor, obj->input.descriptor, 0X1000, handleNamedPipeInput, obj)) {
      return 0;
    }
  }

  return 1;
}

#if defined(__MINGW32__)
static int
createWindowsPipe (NamedPipeObject *obj) {
  obj->windows.connect.monitor = NULL;

  if ((obj->windows.connect.event = CreateEvent(NULL, TRUE, FALSE, NULL))) {
    if ((obj->input.descriptor = CreateNamedPipe(obj->host.path,
                                                 PIPE_ACCESS_INBOUND | FILE_FLAG_OVERLAPPED,
                                                 PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE,
                                                 1, 0, 0, 0, NULL)) != INVALID_HANDLE_VALUE) {
      logMessage(LOG_DEBUG, "named pipe created: %s: handle=%u",
                 obj->host.path, (unsigned int)obj->input.descriptor);

      return 1;
    } else {
      logWindowsSystemError("CreateNamedPipe");
    }

    CloseHandle(obj->windows.connect.event);
  } else {
    logWindowsSystemError("CreateEvent");
  }

  return 0;
}

static int
doWindowsPipeConnected (NamedPipeObject *obj) {
  return monitorInput(obj);
}

ASYNC_MONITOR_CALLBACK(handleWindowsPipeConnected) {
  NamedPipeObject *obj = parameters->data;

  asyncDiscardHandle(obj->windows.connect.monitor);
  obj->windows.connect.monitor = NULL;

  doWindowsPipeConnected(obj);
  return 0;
}

static int
monitorWindowsPipeConnect (NamedPipeObject *obj) {
  if (ResetEvent(obj->windows.connect.event)) {
    ZeroMemory(&obj->windows.connect.overlapped, sizeof(obj->windows.connect.overlapped));
    obj->windows.connect.overlapped.hEvent = obj->windows.connect.event;

    if (ConnectNamedPipe(obj->input.descriptor, &obj->windows.connect.overlapped)) {
      if (doWindowsPipeConnected(obj)) {
        return 1;
      }
    } else {
      DWORD error = GetLastError();

      if (error == ERROR_PIPE_CONNECTED) {
        if (doWindowsPipeConnected(obj)) {
          return 1;
        }
      } else if (error == ERROR_IO_PENDING) {
        if (asyncMonitorFileInput(&obj->windows.connect.monitor, obj->windows.connect.event, handleWindowsPipeConnected, obj)) {
          return 1;
        }
      } else {
        logWindowsError(error, "ConnectNamedPipe");
      }
    }
  } else {
    logWindowsSystemError("ResetEvent");
  }

  return 0;
}

static void
disconnectWindowsPipe (NamedPipeObject *obj) {
  if (!DisconnectNamedPipe(obj->input.descriptor)) {
    logWindowsSystemError("DisconnectNamedPipe");
  }
}

static void
releaseWindowsResources (NamedPipeObject *obj) {
  if (obj->windows.connect.monitor) {
    asyncCancelRequest(obj->windows.connect.monitor);
    obj->windows.connect.monitor = NULL;
  }

  if (obj->windows.connect.event) {
    CloseHandle(obj->windows.connect.event);
    obj->windows.connect.event = NULL;
  }
}

static void
setNamedPipeMethods (NamedPipeObject *obj) {
  obj->createPipe = createWindowsPipe;
  obj->monitorPipe = monitorWindowsPipeConnect;
  obj->resetPipe = disconnectWindowsPipe;
  obj->releaseResources = releaseWindowsResources;
}

#elif defined(S_ISFIFO)
static int
createFifo (NamedPipeObject *obj) {
  lockUmask();
  int result = mkfifo(obj->host.path, 0);
  unlockUmask();

  if ((result == -1) && (errno == EEXIST)) {
    struct stat fifo;

    if (lstat(obj->host.path, &fifo) == -1) {
      logMessage(LOG_ERR, "cannot stat FIFO: %s: %s",
                 obj->host.path, strerror(errno));
    } else if (S_ISFIFO(fifo.st_mode)) {
      result = 0;
    }
  }

  if (result != -1) {
    lockUmask();
    int changed = chmod(obj->host.path, S_IRUSR|S_IWUSR|S_IWGRP|S_IWOTH) != -1;
    unlockUmask();

    if (changed) {
      // open read-write even though we only read to prevent an end-of-file condition
      if ((obj->input.descriptor = open(obj->host.path, O_RDWR|O_NONBLOCK)) != -1) {
        logMessage(LOG_DEBUG, "FIFO created: %s: fd=%d",
                   obj->host.path, obj->input.descriptor);

        setCloseOnExec(obj->input.descriptor, 1);
        return 1;
      } else {
        logMessage(LOG_ERR, "cannot open FIFO: %s: %s",
                   obj->host.path, strerror(errno));
      }
    } else {
      logMessage(LOG_ERR, "cannot set FIFO permissions: %s: %s",
                 obj->host.path, strerror(errno));
    }

    removePipe(obj);
  } else {
    logMessage(LOG_ERR, "cannot create FIFO: %s: %s",
               obj->host.path, strerror(errno));
  }

  return 0;
}

static void
setNamedPipeMethods (NamedPipeObject *obj) {
  obj->createPipe = createFifo;
}

#else /* named pipe functions */
#warning named pipes not supported on this platform

static void
setNamedPipeMethods (NamedPipeObject *obj) {
}
#endif /* named pipes functions */

NamedPipeObject *
newNamedPipeObject (const char *name, NamedPipeInputCallback *callback, void *data) {
  NamedPipeObject *obj;

  if ((obj = malloc(sizeof(*obj)))) {
    memset(obj, 0, sizeof(*obj));

    obj->callback = callback;
    obj->data = data;

    obj->createPipe = NULL;
    obj->monitorPipe = monitorInput;
    obj->resetPipe = NULL;
    obj->releaseResources = NULL;
    setNamedPipeMethods(obj);

    initializeHostPath(obj);
    initializeInputDescriptor(obj);
    initializeInputMonitor(obj);

    {
      const char *directory = getNamedPipeDirectory();

      obj->host.path = directory? makePath(directory, name): NULL;
    }

    if (obj->host.path) {
      if (!obj->createPipe) {
        logUnsupportedOperation("create named pipe");
      } else if (obj->createPipe(obj)) {
        if (!obj->monitorPipe) {
          logUnsupportedOperation("monitor named pipe");
        } else if (obj->monitorPipe(obj)) {
          return obj;
        }

        closeInputDescriptor(obj);
        removePipe(obj);
      }

      deallocateHostPath(obj);
    }

    free(obj);
  } else {
    logMallocError();
  }

  return NULL;
}

void
destroyNamedPipeObject (NamedPipeObject *obj) {
  logMessage(LOG_DEBUG, "destroying named pipe: %s", obj->host.path);
  if (obj->releaseResources) obj->releaseResources(obj);
  stopInputMonitor(obj);
  closeInputDescriptor(obj);
  removePipe(obj);
  deallocateHostPath(obj);
  free(obj);
}