Use GTestDBus in all GDBus unit tests
[glib.git] / glib / giounix.c
blob969c3cc5900ecc5e77efc04654bfc844a3e2c9c9
1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * giounix.c: IO Channels using unix file descriptors
5 * Copyright 1998 Owen Taylor
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library 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 GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
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/.
31 * MT safe
34 #include "config.h"
36 #define _POSIX_SOURCE /* for SSIZE_MAX */
38 #include <sys/types.h>
39 #include <sys/stat.h>
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <errno.h>
43 #include <string.h>
44 #include <fcntl.h>
46 #include "giochannel.h"
48 #include "gerror.h"
49 #include "gfileutils.h"
50 #include "gstrfuncs.h"
51 #include "gtestutils.h"
54 * Unix IO Channels
57 typedef struct _GIOUnixChannel GIOUnixChannel;
58 typedef struct _GIOUnixWatch GIOUnixWatch;
60 struct _GIOUnixChannel
62 GIOChannel channel;
63 gint fd;
66 struct _GIOUnixWatch
68 GSource source;
69 GPollFD pollfd;
70 GIOChannel *channel;
71 GIOCondition condition;
75 static GIOStatus g_io_unix_read (GIOChannel *channel,
76 gchar *buf,
77 gsize count,
78 gsize *bytes_read,
79 GError **err);
80 static GIOStatus g_io_unix_write (GIOChannel *channel,
81 const gchar *buf,
82 gsize count,
83 gsize *bytes_written,
84 GError **err);
85 static GIOStatus g_io_unix_seek (GIOChannel *channel,
86 gint64 offset,
87 GSeekType type,
88 GError **err);
89 static GIOStatus g_io_unix_close (GIOChannel *channel,
90 GError **err);
91 static void g_io_unix_free (GIOChannel *channel);
92 static GSource* g_io_unix_create_watch (GIOChannel *channel,
93 GIOCondition condition);
94 static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
95 GIOFlags flags,
96 GError **err);
97 static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
99 static gboolean g_io_unix_prepare (GSource *source,
100 gint *timeout);
101 static gboolean g_io_unix_check (GSource *source);
102 static gboolean g_io_unix_dispatch (GSource *source,
103 GSourceFunc callback,
104 gpointer user_data);
105 static void g_io_unix_finalize (GSource *source);
107 GSourceFuncs g_io_watch_funcs = {
108 g_io_unix_prepare,
109 g_io_unix_check,
110 g_io_unix_dispatch,
111 g_io_unix_finalize
114 static GIOFuncs unix_channel_funcs = {
115 g_io_unix_read,
116 g_io_unix_write,
117 g_io_unix_seek,
118 g_io_unix_close,
119 g_io_unix_create_watch,
120 g_io_unix_free,
121 g_io_unix_set_flags,
122 g_io_unix_get_flags,
125 static gboolean
126 g_io_unix_prepare (GSource *source,
127 gint *timeout)
129 GIOUnixWatch *watch = (GIOUnixWatch *)source;
130 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
132 *timeout = -1;
134 /* Only return TRUE here if _all_ bits in watch->condition will be set
136 return ((watch->condition & buffer_condition) == watch->condition);
139 static gboolean
140 g_io_unix_check (GSource *source)
142 GIOUnixWatch *watch = (GIOUnixWatch *)source;
143 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
144 GIOCondition poll_condition = watch->pollfd.revents;
146 return ((poll_condition | buffer_condition) & watch->condition);
149 static gboolean
150 g_io_unix_dispatch (GSource *source,
151 GSourceFunc callback,
152 gpointer user_data)
155 GIOFunc func = (GIOFunc)callback;
156 GIOUnixWatch *watch = (GIOUnixWatch *)source;
157 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
159 if (!func)
161 g_warning ("IO watch dispatched without callback\n"
162 "You must call g_source_connect().");
163 return FALSE;
166 return (*func) (watch->channel,
167 (watch->pollfd.revents | buffer_condition) & watch->condition,
168 user_data);
171 static void
172 g_io_unix_finalize (GSource *source)
174 GIOUnixWatch *watch = (GIOUnixWatch *)source;
176 g_io_channel_unref (watch->channel);
179 static GIOStatus
180 g_io_unix_read (GIOChannel *channel,
181 gchar *buf,
182 gsize count,
183 gsize *bytes_read,
184 GError **err)
186 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
187 gssize result;
189 if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
190 count = SSIZE_MAX;
192 retry:
193 result = read (unix_channel->fd, buf, count);
195 if (result < 0)
197 int errsv = errno;
198 *bytes_read = 0;
200 switch (errsv)
202 #ifdef EINTR
203 case EINTR:
204 goto retry;
205 #endif
206 #ifdef EAGAIN
207 case EAGAIN:
208 return G_IO_STATUS_AGAIN;
209 #endif
210 default:
211 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
212 g_io_channel_error_from_errno (errsv),
213 g_strerror (errsv));
214 return G_IO_STATUS_ERROR;
218 *bytes_read = result;
220 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
223 static GIOStatus
224 g_io_unix_write (GIOChannel *channel,
225 const gchar *buf,
226 gsize count,
227 gsize *bytes_written,
228 GError **err)
230 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
231 gssize result;
233 retry:
234 result = write (unix_channel->fd, buf, count);
236 if (result < 0)
238 int errsv = errno;
239 *bytes_written = 0;
241 switch (errsv)
243 #ifdef EINTR
244 case EINTR:
245 goto retry;
246 #endif
247 #ifdef EAGAIN
248 case EAGAIN:
249 return G_IO_STATUS_AGAIN;
250 #endif
251 default:
252 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
253 g_io_channel_error_from_errno (errsv),
254 g_strerror (errsv));
255 return G_IO_STATUS_ERROR;
259 *bytes_written = result;
261 return G_IO_STATUS_NORMAL;
264 static GIOStatus
265 g_io_unix_seek (GIOChannel *channel,
266 gint64 offset,
267 GSeekType type,
268 GError **err)
270 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
271 int whence;
272 off_t tmp_offset;
273 off_t result;
275 switch (type)
277 case G_SEEK_SET:
278 whence = SEEK_SET;
279 break;
280 case G_SEEK_CUR:
281 whence = SEEK_CUR;
282 break;
283 case G_SEEK_END:
284 whence = SEEK_END;
285 break;
286 default:
287 whence = -1; /* Shut the compiler up */
288 g_assert_not_reached ();
291 tmp_offset = offset;
292 if (tmp_offset != offset)
294 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
295 g_io_channel_error_from_errno (EINVAL),
296 g_strerror (EINVAL));
297 return G_IO_STATUS_ERROR;
300 result = lseek (unix_channel->fd, tmp_offset, whence);
302 if (result < 0)
304 int errsv = errno;
305 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
306 g_io_channel_error_from_errno (errsv),
307 g_strerror (errsv));
308 return G_IO_STATUS_ERROR;
311 return G_IO_STATUS_NORMAL;
315 static GIOStatus
316 g_io_unix_close (GIOChannel *channel,
317 GError **err)
319 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
321 if (close (unix_channel->fd) < 0)
323 int errsv = errno;
324 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
325 g_io_channel_error_from_errno (errsv),
326 g_strerror (errsv));
327 return G_IO_STATUS_ERROR;
330 return G_IO_STATUS_NORMAL;
333 static void
334 g_io_unix_free (GIOChannel *channel)
336 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
338 g_free (unix_channel);
341 static GSource *
342 g_io_unix_create_watch (GIOChannel *channel,
343 GIOCondition condition)
345 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
346 GSource *source;
347 GIOUnixWatch *watch;
350 source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
351 g_source_set_name (source, "GIOChannel (Unix)");
352 watch = (GIOUnixWatch *)source;
354 watch->channel = channel;
355 g_io_channel_ref (channel);
357 watch->condition = condition;
359 watch->pollfd.fd = unix_channel->fd;
360 watch->pollfd.events = condition;
362 g_source_add_poll (source, &watch->pollfd);
364 return source;
367 static GIOStatus
368 g_io_unix_set_flags (GIOChannel *channel,
369 GIOFlags flags,
370 GError **err)
372 glong fcntl_flags;
373 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
375 fcntl_flags = 0;
377 if (flags & G_IO_FLAG_APPEND)
378 fcntl_flags |= O_APPEND;
379 if (flags & G_IO_FLAG_NONBLOCK)
380 #ifdef O_NONBLOCK
381 fcntl_flags |= O_NONBLOCK;
382 #else
383 fcntl_flags |= O_NDELAY;
384 #endif
386 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
388 int errsv = errno;
389 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
390 g_io_channel_error_from_errno (errsv),
391 g_strerror (errsv));
392 return G_IO_STATUS_ERROR;
395 return G_IO_STATUS_NORMAL;
398 static GIOFlags
399 g_io_unix_get_flags (GIOChannel *channel)
401 GIOFlags flags = 0;
402 glong fcntl_flags;
403 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
405 fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
407 if (fcntl_flags == -1)
409 int err = errno;
410 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
411 g_strerror (err), err);
412 return 0;
415 if (fcntl_flags & O_APPEND)
416 flags |= G_IO_FLAG_APPEND;
417 #ifdef O_NONBLOCK
418 if (fcntl_flags & O_NONBLOCK)
419 #else
420 if (fcntl_flags & O_NDELAY)
421 #endif
422 flags |= G_IO_FLAG_NONBLOCK;
424 switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
426 case O_RDONLY:
427 channel->is_readable = TRUE;
428 channel->is_writeable = FALSE;
429 break;
430 case O_WRONLY:
431 channel->is_readable = FALSE;
432 channel->is_writeable = TRUE;
433 break;
434 case O_RDWR:
435 channel->is_readable = TRUE;
436 channel->is_writeable = TRUE;
437 break;
438 default:
439 g_assert_not_reached ();
442 return flags;
445 GIOChannel *
446 g_io_channel_new_file (const gchar *filename,
447 const gchar *mode,
448 GError **error)
450 int fid, flags;
451 mode_t create_mode;
452 GIOChannel *channel;
453 enum { /* Cheesy hack */
454 MODE_R = 1 << 0,
455 MODE_W = 1 << 1,
456 MODE_A = 1 << 2,
457 MODE_PLUS = 1 << 3,
458 MODE_R_PLUS = MODE_R | MODE_PLUS,
459 MODE_W_PLUS = MODE_W | MODE_PLUS,
460 MODE_A_PLUS = MODE_A | MODE_PLUS
461 } mode_num;
462 struct stat buffer;
464 g_return_val_if_fail (filename != NULL, NULL);
465 g_return_val_if_fail (mode != NULL, NULL);
466 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
468 switch (mode[0])
470 case 'r':
471 mode_num = MODE_R;
472 break;
473 case 'w':
474 mode_num = MODE_W;
475 break;
476 case 'a':
477 mode_num = MODE_A;
478 break;
479 default:
480 g_warning ("Invalid GIOFileMode %s.\n", mode);
481 return NULL;
484 switch (mode[1])
486 case '\0':
487 break;
488 case '+':
489 if (mode[2] == '\0')
491 mode_num |= MODE_PLUS;
492 break;
494 /* Fall through */
495 default:
496 g_warning ("Invalid GIOFileMode %s.\n", mode);
497 return NULL;
500 switch (mode_num)
502 case MODE_R:
503 flags = O_RDONLY;
504 break;
505 case MODE_W:
506 flags = O_WRONLY | O_TRUNC | O_CREAT;
507 break;
508 case MODE_A:
509 flags = O_WRONLY | O_APPEND | O_CREAT;
510 break;
511 case MODE_R_PLUS:
512 flags = O_RDWR;
513 break;
514 case MODE_W_PLUS:
515 flags = O_RDWR | O_TRUNC | O_CREAT;
516 break;
517 case MODE_A_PLUS:
518 flags = O_RDWR | O_APPEND | O_CREAT;
519 break;
520 case MODE_PLUS:
521 default:
522 g_assert_not_reached ();
523 flags = 0;
526 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
530 fid = open (filename, flags, create_mode);
532 while (fid == -1 && errno == EINTR);
534 if (fid == -1)
536 int err = errno;
537 g_set_error_literal (error, G_FILE_ERROR,
538 g_file_error_from_errno (err),
539 g_strerror (err));
540 return (GIOChannel *)NULL;
543 if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
545 int err = errno;
546 close (fid);
547 g_set_error_literal (error, G_FILE_ERROR,
548 g_file_error_from_errno (err),
549 g_strerror (err));
550 return (GIOChannel *)NULL;
553 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
555 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
556 || S_ISBLK (buffer.st_mode);
558 switch (mode_num)
560 case MODE_R:
561 channel->is_readable = TRUE;
562 channel->is_writeable = FALSE;
563 break;
564 case MODE_W:
565 case MODE_A:
566 channel->is_readable = FALSE;
567 channel->is_writeable = TRUE;
568 break;
569 case MODE_R_PLUS:
570 case MODE_W_PLUS:
571 case MODE_A_PLUS:
572 channel->is_readable = TRUE;
573 channel->is_writeable = TRUE;
574 break;
575 case MODE_PLUS:
576 default:
577 g_assert_not_reached ();
580 g_io_channel_init (channel);
581 channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
582 channel->funcs = &unix_channel_funcs;
584 ((GIOUnixChannel *) channel)->fd = fid;
585 return channel;
589 * g_io_channel_unix_new:
590 * @fd: a file descriptor.
591 * @Returns: a new #GIOChannel.
593 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
594 * this works for plain files, pipes, and sockets.
596 * The returned #GIOChannel has a reference count of 1.
598 * The default encoding for #GIOChannel is UTF-8. If your application
599 * is reading output from a command using via pipe, you may need to set
600 * the encoding to the encoding of the current locale (see
601 * g_get_charset()) with the g_io_channel_set_encoding() function.
603 * If you want to read raw binary data without interpretation, then
604 * call the g_io_channel_set_encoding() function with %NULL for the
605 * encoding argument.
607 * This function is available in GLib on Windows, too, but you should
608 * avoid using it on Windows. The domain of file descriptors and
609 * sockets overlap. There is no way for GLib to know which one you mean
610 * in case the argument you pass to this function happens to be both a
611 * valid file descriptor and socket. If that happens a warning is
612 * issued, and GLib assumes that it is the file descriptor you mean.
614 GIOChannel *
615 g_io_channel_unix_new (gint fd)
617 struct stat buffer;
618 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
619 GIOChannel *channel = (GIOChannel *)unix_channel;
621 g_io_channel_init (channel);
622 channel->funcs = &unix_channel_funcs;
624 unix_channel->fd = fd;
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.
629 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
630 * succeed in most cases.
632 if (fstat (unix_channel->fd, &buffer) == 0)
633 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
634 || S_ISBLK (buffer.st_mode);
635 else /* Assume not seekable */
636 channel->is_seekable = FALSE;
638 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
640 return channel;
644 * g_io_channel_unix_get_fd:
645 * @channel: a #GIOChannel, created with g_io_channel_unix_new().
646 * @Returns: the file descriptor of the #GIOChannel.
648 * Returns the file descriptor of the #GIOChannel.
650 * On Windows this function returns the file descriptor or socket of
651 * the #GIOChannel.
653 gint
654 g_io_channel_unix_get_fd (GIOChannel *channel)
656 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
657 return unix_channel->fd;