Branch data Line data Source code
1 : : /* GLIB - Library of useful routines for C programming
2 : : * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 : : *
4 : : * giounix.c: IO Channels using unix file descriptors
5 : : * Copyright 1998 Owen Taylor
6 : : *
7 : : * SPDX-License-Identifier: LGPL-2.1-or-later
8 : : *
9 : : * This library is free software; you can redistribute it and/or
10 : : * modify it under the terms of the GNU Lesser General Public
11 : : * License as published by the Free Software Foundation; either
12 : : * version 2.1 of the License, or (at your option) any later version.
13 : : *
14 : : * This library is distributed in the hope that it will be useful,
15 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : * Lesser General Public License for more details.
18 : : *
19 : : * You should have received a copy of the GNU Lesser General Public
20 : : * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 : : */
22 : :
23 : : /*
24 : : * Modified by the GLib Team and others 1997-2000. See the AUTHORS
25 : : * file for a list of people on the GLib Team. See the ChangeLog
26 : : * files for a list of changes. These files are distributed with
27 : : * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 : : */
29 : :
30 : : /*
31 : : * MT safe
32 : : */
33 : :
34 : : #include "config.h"
35 : :
36 : : #include <sys/types.h>
37 : : #include <sys/stat.h>
38 : : #include <stdio.h>
39 : : #include <unistd.h>
40 : : #include <errno.h>
41 : : #include <string.h>
42 : : #include <fcntl.h>
43 : : #include <glib/gstdio.h>
44 : :
45 : : #ifndef O_CLOEXEC
46 : : #define O_CLOEXEC 0
47 : : #endif
48 : :
49 : : #include "giochannel.h"
50 : :
51 : : #include "gerror.h"
52 : : #include "gfileutils.h"
53 : : #include "gstrfuncs.h"
54 : : #include "gtestutils.h"
55 : :
56 : : /*
57 : : * Unix IO Channels
58 : : */
59 : :
60 : : typedef struct _GIOUnixChannel GIOUnixChannel;
61 : : typedef struct _GIOUnixWatch GIOUnixWatch;
62 : :
63 : : struct _GIOUnixChannel
64 : : {
65 : : GIOChannel channel;
66 : : gint fd;
67 : : };
68 : :
69 : : struct _GIOUnixWatch
70 : : {
71 : : GSource source;
72 : : GPollFD pollfd;
73 : : GIOChannel *channel;
74 : : GIOCondition condition;
75 : : };
76 : :
77 : :
78 : : static GIOStatus g_io_unix_read (GIOChannel *channel,
79 : : gchar *buf,
80 : : gsize count,
81 : : gsize *bytes_read,
82 : : GError **err);
83 : : static GIOStatus g_io_unix_write (GIOChannel *channel,
84 : : const gchar *buf,
85 : : gsize count,
86 : : gsize *bytes_written,
87 : : GError **err);
88 : : static GIOStatus g_io_unix_seek (GIOChannel *channel,
89 : : gint64 offset,
90 : : GSeekType type,
91 : : GError **err);
92 : : static GIOStatus g_io_unix_close (GIOChannel *channel,
93 : : GError **err);
94 : : static void g_io_unix_free (GIOChannel *channel);
95 : : static GSource* g_io_unix_create_watch (GIOChannel *channel,
96 : : GIOCondition condition);
97 : : static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
98 : : GIOFlags flags,
99 : : GError **err);
100 : : static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
101 : :
102 : : static gboolean g_io_unix_prepare (GSource *source,
103 : : gint *timeout);
104 : : static gboolean g_io_unix_check (GSource *source);
105 : : static gboolean g_io_unix_dispatch (GSource *source,
106 : : GSourceFunc callback,
107 : : gpointer user_data);
108 : : static void g_io_unix_finalize (GSource *source);
109 : :
110 : : GSourceFuncs g_io_watch_funcs = {
111 : : g_io_unix_prepare,
112 : : g_io_unix_check,
113 : : g_io_unix_dispatch,
114 : : g_io_unix_finalize,
115 : : NULL, NULL
116 : : };
117 : :
118 : : static GIOFuncs unix_channel_funcs = {
119 : : g_io_unix_read,
120 : : g_io_unix_write,
121 : : g_io_unix_seek,
122 : : g_io_unix_close,
123 : : g_io_unix_create_watch,
124 : : g_io_unix_free,
125 : : g_io_unix_set_flags,
126 : : g_io_unix_get_flags,
127 : : };
128 : :
129 : : static gboolean
130 : 7619 : g_io_unix_prepare (GSource *source,
131 : : gint *timeout)
132 : : {
133 : 7619 : GIOUnixWatch *watch = (GIOUnixWatch *)source;
134 : 7619 : GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
135 : :
136 : : /* Only return TRUE here if _all_ bits in watch->condition will be set
137 : : */
138 : 7619 : return ((watch->condition & buffer_condition) == watch->condition);
139 : : }
140 : :
141 : : static gboolean
142 : 7618 : g_io_unix_check (GSource *source)
143 : : {
144 : 7618 : GIOUnixWatch *watch = (GIOUnixWatch *)source;
145 : 7618 : GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
146 : 7618 : GIOCondition poll_condition = watch->pollfd.revents;
147 : :
148 : 7618 : return ((poll_condition | buffer_condition) & watch->condition);
149 : : }
150 : :
151 : : static gboolean
152 : 1796 : g_io_unix_dispatch (GSource *source,
153 : : GSourceFunc callback,
154 : : gpointer user_data)
155 : :
156 : : {
157 : 1796 : GIOFunc func = (GIOFunc)callback;
158 : 1796 : GIOUnixWatch *watch = (GIOUnixWatch *)source;
159 : 1796 : GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
160 : :
161 : 1796 : if (!func)
162 : : {
163 : 0 : g_warning ("IO watch dispatched without callback. "
164 : : "You must call g_source_connect().");
165 : 0 : return FALSE;
166 : : }
167 : :
168 : 1796 : return (*func) (watch->channel,
169 : 1796 : (watch->pollfd.revents | buffer_condition) & watch->condition,
170 : : user_data);
171 : : }
172 : :
173 : : static void
174 : 865 : g_io_unix_finalize (GSource *source)
175 : : {
176 : 865 : GIOUnixWatch *watch = (GIOUnixWatch *)source;
177 : :
178 : 865 : g_io_channel_unref (watch->channel);
179 : 865 : }
180 : :
181 : : static GIOStatus
182 : 6106 : g_io_unix_read (GIOChannel *channel,
183 : : gchar *buf,
184 : : gsize count,
185 : : gsize *bytes_read,
186 : : GError **err)
187 : : {
188 : 6106 : GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
189 : : gssize result;
190 : :
191 : 6106 : if (count > G_MAXSSIZE) /* At least according to the Debian manpage for read */
192 : 0 : count = G_MAXSSIZE;
193 : :
194 : 6106 : retry:
195 : 6106 : result = read (unix_channel->fd, buf, count);
196 : :
197 : 6106 : if (result < 0)
198 : : {
199 : 19 : int errsv = errno;
200 : 19 : *bytes_read = 0;
201 : :
202 : 19 : switch (errsv)
203 : : {
204 : : #ifdef EINTR
205 : 0 : case EINTR:
206 : 0 : goto retry;
207 : : #endif
208 : : #ifdef EAGAIN
209 : 19 : case EAGAIN:
210 : 19 : return G_IO_STATUS_AGAIN;
211 : : #endif
212 : 0 : default:
213 : 0 : g_set_error_literal (err, G_IO_CHANNEL_ERROR,
214 : 0 : g_io_channel_error_from_errno (errsv),
215 : : g_strerror (errsv));
216 : 0 : return G_IO_STATUS_ERROR;
217 : : }
218 : : }
219 : :
220 : 6087 : *bytes_read = result;
221 : :
222 : 6087 : return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
223 : : }
224 : :
225 : : static GIOStatus
226 : 297 : g_io_unix_write (GIOChannel *channel,
227 : : const gchar *buf,
228 : : gsize count,
229 : : gsize *bytes_written,
230 : : GError **err)
231 : : {
232 : 297 : GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
233 : : gssize result;
234 : :
235 : 297 : retry:
236 : 297 : result = write (unix_channel->fd, buf, count);
237 : :
238 : 297 : if (result < 0)
239 : : {
240 : 0 : int errsv = errno;
241 : 0 : *bytes_written = 0;
242 : :
243 : 0 : switch (errsv)
244 : : {
245 : : #ifdef EINTR
246 : 0 : case EINTR:
247 : 0 : goto retry;
248 : : #endif
249 : : #ifdef EAGAIN
250 : 0 : case EAGAIN:
251 : 0 : return G_IO_STATUS_AGAIN;
252 : : #endif
253 : 0 : default:
254 : 0 : g_set_error_literal (err, G_IO_CHANNEL_ERROR,
255 : 0 : g_io_channel_error_from_errno (errsv),
256 : : g_strerror (errsv));
257 : 0 : return G_IO_STATUS_ERROR;
258 : : }
259 : : }
260 : :
261 : 297 : *bytes_written = result;
262 : :
263 : 297 : return G_IO_STATUS_NORMAL;
264 : : }
265 : :
266 : : static GIOStatus
267 : 0 : g_io_unix_seek (GIOChannel *channel,
268 : : gint64 offset,
269 : : GSeekType type,
270 : : GError **err)
271 : : {
272 : 0 : GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
273 : : int whence;
274 : : off_t tmp_offset;
275 : : off_t result;
276 : :
277 : 0 : switch (type)
278 : : {
279 : 0 : case G_SEEK_SET:
280 : 0 : whence = SEEK_SET;
281 : 0 : break;
282 : 0 : case G_SEEK_CUR:
283 : 0 : whence = SEEK_CUR;
284 : 0 : break;
285 : 0 : case G_SEEK_END:
286 : 0 : whence = SEEK_END;
287 : 0 : break;
288 : 0 : default:
289 : 0 : whence = -1; /* Shut the compiler up */
290 : : g_assert_not_reached ();
291 : : }
292 : :
293 : 0 : tmp_offset = offset;
294 : 0 : if (tmp_offset != offset)
295 : : {
296 : 0 : g_set_error_literal (err, G_IO_CHANNEL_ERROR,
297 : 0 : g_io_channel_error_from_errno (EINVAL),
298 : : g_strerror (EINVAL));
299 : 0 : return G_IO_STATUS_ERROR;
300 : : }
301 : :
302 : 0 : result = lseek (unix_channel->fd, tmp_offset, whence);
303 : :
304 : 0 : if (result < 0)
305 : : {
306 : 0 : int errsv = errno;
307 : 0 : g_set_error_literal (err, G_IO_CHANNEL_ERROR,
308 : 0 : g_io_channel_error_from_errno (errsv),
309 : : g_strerror (errsv));
310 : 0 : return G_IO_STATUS_ERROR;
311 : : }
312 : :
313 : 0 : return G_IO_STATUS_NORMAL;
314 : : }
315 : :
316 : :
317 : : static GIOStatus
318 : 920 : g_io_unix_close (GIOChannel *channel,
319 : : GError **err)
320 : : {
321 : 920 : GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
322 : :
323 : 920 : if (close (unix_channel->fd) < 0)
324 : : {
325 : 0 : int errsv = errno;
326 : 0 : g_set_error_literal (err, G_IO_CHANNEL_ERROR,
327 : 0 : g_io_channel_error_from_errno (errsv),
328 : : g_strerror (errsv));
329 : 0 : return G_IO_STATUS_ERROR;
330 : : }
331 : :
332 : 920 : return G_IO_STATUS_NORMAL;
333 : : }
334 : :
335 : : static void
336 : 1114 : g_io_unix_free (GIOChannel *channel)
337 : : {
338 : 1114 : GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
339 : :
340 : 1114 : g_free (unix_channel);
341 : 1114 : }
342 : :
343 : : static GSource *
344 : 869 : g_io_unix_create_watch (GIOChannel *channel,
345 : : GIOCondition condition)
346 : : {
347 : 869 : GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
348 : : GSource *source;
349 : : GIOUnixWatch *watch;
350 : :
351 : :
352 : 869 : source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
353 : 869 : g_source_set_static_name (source, "GIOChannel (Unix)");
354 : 869 : watch = (GIOUnixWatch *)source;
355 : :
356 : 869 : watch->channel = channel;
357 : 869 : g_io_channel_ref (channel);
358 : :
359 : 869 : watch->condition = condition;
360 : :
361 : 869 : watch->pollfd.fd = unix_channel->fd;
362 : 869 : watch->pollfd.events = condition;
363 : :
364 : 869 : g_source_add_poll (source, &watch->pollfd);
365 : :
366 : 869 : return source;
367 : : }
368 : :
369 : : static GIOStatus
370 : 40 : g_io_unix_set_flags (GIOChannel *channel,
371 : : GIOFlags flags,
372 : : GError **err)
373 : : {
374 : : glong fcntl_flags;
375 : 40 : GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
376 : :
377 : 40 : fcntl_flags = 0;
378 : :
379 : 40 : if (flags & G_IO_FLAG_APPEND)
380 : 0 : fcntl_flags |= O_APPEND;
381 : 40 : if (flags & G_IO_FLAG_NONBLOCK)
382 : : #ifdef O_NONBLOCK
383 : 6 : fcntl_flags |= O_NONBLOCK;
384 : : #else
385 : : fcntl_flags |= O_NDELAY;
386 : : #endif
387 : :
388 : 40 : if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
389 : : {
390 : 0 : int errsv = errno;
391 : 0 : g_set_error_literal (err, G_IO_CHANNEL_ERROR,
392 : 0 : g_io_channel_error_from_errno (errsv),
393 : : g_strerror (errsv));
394 : 0 : return G_IO_STATUS_ERROR;
395 : : }
396 : :
397 : 40 : return G_IO_STATUS_NORMAL;
398 : : }
399 : :
400 : : static GIOFlags
401 : 1175 : g_io_unix_get_flags (GIOChannel *channel)
402 : : {
403 : 1175 : GIOFlags flags = G_IO_FLAG_NONE;
404 : : glong fcntl_flags;
405 : 1175 : GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
406 : :
407 : 1175 : fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
408 : :
409 : 1175 : if (fcntl_flags == -1)
410 : : {
411 : 0 : int err = errno;
412 : 0 : g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)",
413 : : g_strerror (err), err);
414 : 0 : return 0;
415 : : }
416 : :
417 : 1175 : if (fcntl_flags & O_APPEND)
418 : 0 : flags |= G_IO_FLAG_APPEND;
419 : : #ifdef O_NONBLOCK
420 : 1175 : if (fcntl_flags & O_NONBLOCK)
421 : : #else
422 : : if (fcntl_flags & O_NDELAY)
423 : : #endif
424 : 0 : flags |= G_IO_FLAG_NONBLOCK;
425 : :
426 : 1175 : switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
427 : : {
428 : 1048 : case O_RDONLY:
429 : 1048 : channel->is_readable = TRUE;
430 : 1048 : channel->is_writeable = FALSE;
431 : 1048 : break;
432 : 120 : case O_WRONLY:
433 : 120 : channel->is_readable = FALSE;
434 : 120 : channel->is_writeable = TRUE;
435 : 120 : break;
436 : 7 : case O_RDWR:
437 : 7 : channel->is_readable = TRUE;
438 : 7 : channel->is_writeable = TRUE;
439 : 7 : break;
440 : 0 : default:
441 : : g_assert_not_reached ();
442 : : }
443 : :
444 : 1175 : return flags;
445 : : }
446 : :
447 : : GIOChannel *
448 : 9 : g_io_channel_new_file (const gchar *filename,
449 : : const gchar *mode,
450 : : GError **error)
451 : : {
452 : : int fid, flags;
453 : : mode_t create_mode;
454 : : GIOChannel *channel;
455 : : enum { /* Cheesy hack */
456 : : MODE_R = 1 << 0,
457 : : MODE_W = 1 << 1,
458 : : MODE_A = 1 << 2,
459 : : MODE_PLUS = 1 << 3,
460 : : MODE_R_PLUS = MODE_R | MODE_PLUS,
461 : : MODE_W_PLUS = MODE_W | MODE_PLUS,
462 : : MODE_A_PLUS = MODE_A | MODE_PLUS
463 : : } G_GNUC_FLAG_ENUM mode_num;
464 : : struct stat buffer;
465 : :
466 : 9 : g_return_val_if_fail (filename != NULL, NULL);
467 : 9 : g_return_val_if_fail (mode != NULL, NULL);
468 : 9 : g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
469 : :
470 : 9 : switch (mode[0])
471 : : {
472 : 7 : case 'r':
473 : 7 : mode_num = MODE_R;
474 : 7 : break;
475 : 2 : case 'w':
476 : 2 : mode_num = MODE_W;
477 : 2 : break;
478 : 0 : case 'a':
479 : 0 : mode_num = MODE_A;
480 : 0 : break;
481 : 0 : default:
482 : 0 : g_warning ("Invalid GIOFileMode %s.", mode);
483 : 0 : return NULL;
484 : : }
485 : :
486 : 9 : switch (mode[1])
487 : : {
488 : 9 : case '\0':
489 : 9 : break;
490 : 0 : case '+':
491 : 0 : if (mode[2] == '\0')
492 : : {
493 : 0 : mode_num |= MODE_PLUS;
494 : 0 : break;
495 : : }
496 : : G_GNUC_FALLTHROUGH;
497 : : default:
498 : 0 : g_warning ("Invalid GIOFileMode %s.", mode);
499 : 0 : return NULL;
500 : : }
501 : :
502 : 9 : switch (mode_num)
503 : : {
504 : 7 : case MODE_R:
505 : 7 : flags = O_RDONLY;
506 : 7 : break;
507 : 2 : case MODE_W:
508 : 2 : flags = O_WRONLY | O_TRUNC | O_CREAT;
509 : 2 : break;
510 : 0 : case MODE_A:
511 : 0 : flags = O_WRONLY | O_APPEND | O_CREAT;
512 : 0 : break;
513 : 0 : case MODE_R_PLUS:
514 : 0 : flags = O_RDWR;
515 : 0 : break;
516 : 0 : case MODE_W_PLUS:
517 : 0 : flags = O_RDWR | O_TRUNC | O_CREAT;
518 : 0 : break;
519 : 0 : case MODE_A_PLUS:
520 : 0 : flags = O_RDWR | O_APPEND | O_CREAT;
521 : 0 : break;
522 : 0 : case MODE_PLUS:
523 : : default:
524 : : g_assert_not_reached ();
525 : : flags = 0;
526 : : }
527 : :
528 : 9 : create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
529 : :
530 : 9 : fid = g_open (filename, flags | O_CLOEXEC, create_mode);
531 : 9 : if (fid == -1)
532 : : {
533 : 0 : int err = errno;
534 : 0 : g_set_error_literal (error, G_FILE_ERROR,
535 : 0 : g_file_error_from_errno (err),
536 : : g_strerror (err));
537 : 0 : return (GIOChannel *)NULL;
538 : : }
539 : :
540 : 9 : if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
541 : : {
542 : 0 : int err = errno;
543 : 0 : close (fid);
544 : 0 : g_set_error_literal (error, G_FILE_ERROR,
545 : 0 : g_file_error_from_errno (err),
546 : : g_strerror (err));
547 : 0 : return (GIOChannel *)NULL;
548 : : }
549 : :
550 : 9 : channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
551 : :
552 : 1 : channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
553 : 10 : || S_ISBLK (buffer.st_mode);
554 : :
555 : 9 : switch (mode_num)
556 : : {
557 : 7 : case MODE_R:
558 : 7 : channel->is_readable = TRUE;
559 : 7 : channel->is_writeable = FALSE;
560 : 7 : break;
561 : 2 : case MODE_W:
562 : : case MODE_A:
563 : 2 : channel->is_readable = FALSE;
564 : 2 : channel->is_writeable = TRUE;
565 : 2 : break;
566 : 0 : case MODE_R_PLUS:
567 : : case MODE_W_PLUS:
568 : : case MODE_A_PLUS:
569 : 0 : channel->is_readable = TRUE;
570 : 0 : channel->is_writeable = TRUE;
571 : 0 : break;
572 : 0 : case MODE_PLUS:
573 : : default:
574 : : g_assert_not_reached ();
575 : : }
576 : :
577 : 9 : g_io_channel_init (channel);
578 : 9 : channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
579 : 9 : channel->funcs = &unix_channel_funcs;
580 : :
581 : 9 : ((GIOUnixChannel *) channel)->fd = fid;
582 : 9 : return channel;
583 : : }
584 : :
585 : : /**
586 : : * g_io_channel_unix_new:
587 : : * @fd: a file descriptor.
588 : : *
589 : : * Creates a new #GIOChannel given a file descriptor. On UNIX systems
590 : : * this works for plain files, pipes, and sockets.
591 : : *
592 : : * The returned #GIOChannel has a reference count of 1.
593 : : *
594 : : * The default encoding for #GIOChannel is UTF-8. If your application
595 : : * is reading output from a command using via pipe, you may need to set
596 : : * the encoding to the encoding of the current locale (see
597 : : * g_get_charset()) with the g_io_channel_set_encoding() function.
598 : : * By default, the fd passed will not be closed when the final reference
599 : : * to the #GIOChannel data structure is dropped.
600 : : *
601 : : * If you want to read raw binary data without interpretation, then
602 : : * call the g_io_channel_set_encoding() function with %NULL for the
603 : : * encoding argument.
604 : : *
605 : : * This function is available in GLib on Windows, too, but you should
606 : : * avoid using it on Windows. The domain of file descriptors and
607 : : * sockets overlap. There is no way for GLib to know which one you mean
608 : : * in case the argument you pass to this function happens to be both a
609 : : * valid file descriptor and socket. If that happens a warning is
610 : : * issued, and GLib assumes that it is the file descriptor you mean.
611 : : *
612 : : * Returns: a new #GIOChannel.
613 : : **/
614 : : GIOChannel *
615 : 1141 : g_io_channel_unix_new (gint fd)
616 : : {
617 : : struct stat buffer;
618 : 1141 : GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
619 : 1141 : GIOChannel *channel = (GIOChannel *)unix_channel;
620 : :
621 : 1141 : g_io_channel_init (channel);
622 : 1141 : channel->funcs = &unix_channel_funcs;
623 : :
624 : 1141 : unix_channel->fd = fd;
625 : :
626 : : /* I'm not sure if fstat on a non-file (e.g., socket) works
627 : : * it should be safe to say if it fails, the fd isn't seekable.
628 : : */
629 : : /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
630 : : * succeed in most cases.
631 : : */
632 : 1141 : if (fstat (unix_channel->fd, &buffer) == 0)
633 : 1137 : channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
634 : 2278 : || S_ISBLK (buffer.st_mode);
635 : : else /* Assume not seekable */
636 : 0 : channel->is_seekable = FALSE;
637 : :
638 : 1141 : g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
639 : :
640 : 1141 : return channel;
641 : : }
642 : :
643 : : /**
644 : : * g_io_channel_unix_get_fd:
645 : : * @channel: a #GIOChannel, created with g_io_channel_unix_new().
646 : : *
647 : : * Returns the file descriptor of the #GIOChannel.
648 : : *
649 : : * On Windows this function returns the file descriptor or socket of
650 : : * the #GIOChannel.
651 : : *
652 : : * Returns: the file descriptor of the #GIOChannel.
653 : : **/
654 : : gint
655 : 135 : g_io_channel_unix_get_fd (GIOChannel *channel)
656 : : {
657 : 135 : GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
658 : 135 : return unix_channel->fd;
659 : : }
|