gio/gdbusaddress.c: Silence RunDLL errors
[glib.git] / glib / giounix.c
blob517e36bdd6e2dc5c925ab8b2b60e31fe42637acd
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>
45 #include <glib/gstdio.h>
47 #include "giochannel.h"
49 #include "gerror.h"
50 #include "gfileutils.h"
51 #include "gstrfuncs.h"
52 #include "gtestutils.h"
55 * Unix IO Channels
58 typedef struct _GIOUnixChannel GIOUnixChannel;
59 typedef struct _GIOUnixWatch GIOUnixWatch;
61 struct _GIOUnixChannel
63 GIOChannel channel;
64 gint fd;
67 struct _GIOUnixWatch
69 GSource source;
70 GPollFD pollfd;
71 GIOChannel *channel;
72 GIOCondition condition;
76 static GIOStatus g_io_unix_read (GIOChannel *channel,
77 gchar *buf,
78 gsize count,
79 gsize *bytes_read,
80 GError **err);
81 static GIOStatus g_io_unix_write (GIOChannel *channel,
82 const gchar *buf,
83 gsize count,
84 gsize *bytes_written,
85 GError **err);
86 static GIOStatus g_io_unix_seek (GIOChannel *channel,
87 gint64 offset,
88 GSeekType type,
89 GError **err);
90 static GIOStatus g_io_unix_close (GIOChannel *channel,
91 GError **err);
92 static void g_io_unix_free (GIOChannel *channel);
93 static GSource* g_io_unix_create_watch (GIOChannel *channel,
94 GIOCondition condition);
95 static GIOStatus g_io_unix_set_flags (GIOChannel *channel,
96 GIOFlags flags,
97 GError **err);
98 static GIOFlags g_io_unix_get_flags (GIOChannel *channel);
100 static gboolean g_io_unix_prepare (GSource *source,
101 gint *timeout);
102 static gboolean g_io_unix_check (GSource *source);
103 static gboolean g_io_unix_dispatch (GSource *source,
104 GSourceFunc callback,
105 gpointer user_data);
106 static void g_io_unix_finalize (GSource *source);
108 GSourceFuncs g_io_watch_funcs = {
109 g_io_unix_prepare,
110 g_io_unix_check,
111 g_io_unix_dispatch,
112 g_io_unix_finalize
115 static GIOFuncs unix_channel_funcs = {
116 g_io_unix_read,
117 g_io_unix_write,
118 g_io_unix_seek,
119 g_io_unix_close,
120 g_io_unix_create_watch,
121 g_io_unix_free,
122 g_io_unix_set_flags,
123 g_io_unix_get_flags,
126 static gboolean
127 g_io_unix_prepare (GSource *source,
128 gint *timeout)
130 GIOUnixWatch *watch = (GIOUnixWatch *)source;
131 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
133 *timeout = -1;
135 /* Only return TRUE here if _all_ bits in watch->condition will be set
137 return ((watch->condition & buffer_condition) == watch->condition);
140 static gboolean
141 g_io_unix_check (GSource *source)
143 GIOUnixWatch *watch = (GIOUnixWatch *)source;
144 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
145 GIOCondition poll_condition = watch->pollfd.revents;
147 return ((poll_condition | buffer_condition) & watch->condition);
150 static gboolean
151 g_io_unix_dispatch (GSource *source,
152 GSourceFunc callback,
153 gpointer user_data)
156 GIOFunc func = (GIOFunc)callback;
157 GIOUnixWatch *watch = (GIOUnixWatch *)source;
158 GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
160 if (!func)
162 g_warning ("IO watch dispatched without callback\n"
163 "You must call g_source_connect().");
164 return FALSE;
167 return (*func) (watch->channel,
168 (watch->pollfd.revents | buffer_condition) & watch->condition,
169 user_data);
172 static void
173 g_io_unix_finalize (GSource *source)
175 GIOUnixWatch *watch = (GIOUnixWatch *)source;
177 g_io_channel_unref (watch->channel);
180 static GIOStatus
181 g_io_unix_read (GIOChannel *channel,
182 gchar *buf,
183 gsize count,
184 gsize *bytes_read,
185 GError **err)
187 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
188 gssize result;
190 if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
191 count = SSIZE_MAX;
193 retry:
194 result = read (unix_channel->fd, buf, count);
196 if (result < 0)
198 int errsv = errno;
199 *bytes_read = 0;
201 switch (errsv)
203 #ifdef EINTR
204 case EINTR:
205 goto retry;
206 #endif
207 #ifdef EAGAIN
208 case EAGAIN:
209 return G_IO_STATUS_AGAIN;
210 #endif
211 default:
212 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
213 g_io_channel_error_from_errno (errsv),
214 g_strerror (errsv));
215 return G_IO_STATUS_ERROR;
219 *bytes_read = result;
221 return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
224 static GIOStatus
225 g_io_unix_write (GIOChannel *channel,
226 const gchar *buf,
227 gsize count,
228 gsize *bytes_written,
229 GError **err)
231 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
232 gssize result;
234 retry:
235 result = write (unix_channel->fd, buf, count);
237 if (result < 0)
239 int errsv = errno;
240 *bytes_written = 0;
242 switch (errsv)
244 #ifdef EINTR
245 case EINTR:
246 goto retry;
247 #endif
248 #ifdef EAGAIN
249 case EAGAIN:
250 return G_IO_STATUS_AGAIN;
251 #endif
252 default:
253 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
254 g_io_channel_error_from_errno (errsv),
255 g_strerror (errsv));
256 return G_IO_STATUS_ERROR;
260 *bytes_written = result;
262 return G_IO_STATUS_NORMAL;
265 static GIOStatus
266 g_io_unix_seek (GIOChannel *channel,
267 gint64 offset,
268 GSeekType type,
269 GError **err)
271 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
272 int whence;
273 off_t tmp_offset;
274 off_t result;
276 switch (type)
278 case G_SEEK_SET:
279 whence = SEEK_SET;
280 break;
281 case G_SEEK_CUR:
282 whence = SEEK_CUR;
283 break;
284 case G_SEEK_END:
285 whence = SEEK_END;
286 break;
287 default:
288 whence = -1; /* Shut the compiler up */
289 g_assert_not_reached ();
292 tmp_offset = offset;
293 if (tmp_offset != offset)
295 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
296 g_io_channel_error_from_errno (EINVAL),
297 g_strerror (EINVAL));
298 return G_IO_STATUS_ERROR;
301 result = lseek (unix_channel->fd, tmp_offset, whence);
303 if (result < 0)
305 int errsv = errno;
306 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
307 g_io_channel_error_from_errno (errsv),
308 g_strerror (errsv));
309 return G_IO_STATUS_ERROR;
312 return G_IO_STATUS_NORMAL;
316 static GIOStatus
317 g_io_unix_close (GIOChannel *channel,
318 GError **err)
320 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
322 if (close (unix_channel->fd) < 0)
324 int errsv = errno;
325 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
326 g_io_channel_error_from_errno (errsv),
327 g_strerror (errsv));
328 return G_IO_STATUS_ERROR;
331 return G_IO_STATUS_NORMAL;
334 static void
335 g_io_unix_free (GIOChannel *channel)
337 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
339 g_free (unix_channel);
342 static GSource *
343 g_io_unix_create_watch (GIOChannel *channel,
344 GIOCondition condition)
346 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
347 GSource *source;
348 GIOUnixWatch *watch;
351 source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
352 g_source_set_name (source, "GIOChannel (Unix)");
353 watch = (GIOUnixWatch *)source;
355 watch->channel = channel;
356 g_io_channel_ref (channel);
358 watch->condition = condition;
360 watch->pollfd.fd = unix_channel->fd;
361 watch->pollfd.events = condition;
363 g_source_add_poll (source, &watch->pollfd);
365 return source;
368 static GIOStatus
369 g_io_unix_set_flags (GIOChannel *channel,
370 GIOFlags flags,
371 GError **err)
373 glong fcntl_flags;
374 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
376 fcntl_flags = 0;
378 if (flags & G_IO_FLAG_APPEND)
379 fcntl_flags |= O_APPEND;
380 if (flags & G_IO_FLAG_NONBLOCK)
381 #ifdef O_NONBLOCK
382 fcntl_flags |= O_NONBLOCK;
383 #else
384 fcntl_flags |= O_NDELAY;
385 #endif
387 if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
389 int errsv = errno;
390 g_set_error_literal (err, G_IO_CHANNEL_ERROR,
391 g_io_channel_error_from_errno (errsv),
392 g_strerror (errsv));
393 return G_IO_STATUS_ERROR;
396 return G_IO_STATUS_NORMAL;
399 static GIOFlags
400 g_io_unix_get_flags (GIOChannel *channel)
402 GIOFlags flags = 0;
403 glong fcntl_flags;
404 GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
406 fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
408 if (fcntl_flags == -1)
410 int err = errno;
411 g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
412 g_strerror (err), err);
413 return 0;
416 if (fcntl_flags & O_APPEND)
417 flags |= G_IO_FLAG_APPEND;
418 #ifdef O_NONBLOCK
419 if (fcntl_flags & O_NONBLOCK)
420 #else
421 if (fcntl_flags & O_NDELAY)
422 #endif
423 flags |= G_IO_FLAG_NONBLOCK;
425 switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
427 case O_RDONLY:
428 channel->is_readable = TRUE;
429 channel->is_writeable = FALSE;
430 break;
431 case O_WRONLY:
432 channel->is_readable = FALSE;
433 channel->is_writeable = TRUE;
434 break;
435 case O_RDWR:
436 channel->is_readable = TRUE;
437 channel->is_writeable = TRUE;
438 break;
439 default:
440 g_assert_not_reached ();
443 return flags;
446 GIOChannel *
447 g_io_channel_new_file (const gchar *filename,
448 const gchar *mode,
449 GError **error)
451 int fid, flags;
452 mode_t create_mode;
453 GIOChannel *channel;
454 enum { /* Cheesy hack */
455 MODE_R = 1 << 0,
456 MODE_W = 1 << 1,
457 MODE_A = 1 << 2,
458 MODE_PLUS = 1 << 3,
459 MODE_R_PLUS = MODE_R | MODE_PLUS,
460 MODE_W_PLUS = MODE_W | MODE_PLUS,
461 MODE_A_PLUS = MODE_A | MODE_PLUS
462 } mode_num;
463 struct stat buffer;
465 g_return_val_if_fail (filename != NULL, NULL);
466 g_return_val_if_fail (mode != NULL, NULL);
467 g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
469 switch (mode[0])
471 case 'r':
472 mode_num = MODE_R;
473 break;
474 case 'w':
475 mode_num = MODE_W;
476 break;
477 case 'a':
478 mode_num = MODE_A;
479 break;
480 default:
481 g_warning ("Invalid GIOFileMode %s.\n", mode);
482 return NULL;
485 switch (mode[1])
487 case '\0':
488 break;
489 case '+':
490 if (mode[2] == '\0')
492 mode_num |= MODE_PLUS;
493 break;
495 /* Fall through */
496 default:
497 g_warning ("Invalid GIOFileMode %s.\n", mode);
498 return NULL;
501 switch (mode_num)
503 case MODE_R:
504 flags = O_RDONLY;
505 break;
506 case MODE_W:
507 flags = O_WRONLY | O_TRUNC | O_CREAT;
508 break;
509 case MODE_A:
510 flags = O_WRONLY | O_APPEND | O_CREAT;
511 break;
512 case MODE_R_PLUS:
513 flags = O_RDWR;
514 break;
515 case MODE_W_PLUS:
516 flags = O_RDWR | O_TRUNC | O_CREAT;
517 break;
518 case MODE_A_PLUS:
519 flags = O_RDWR | O_APPEND | O_CREAT;
520 break;
521 case MODE_PLUS:
522 default:
523 g_assert_not_reached ();
524 flags = 0;
527 create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
529 fid = g_open (filename, flags, create_mode);
530 if (fid == -1)
532 int err = errno;
533 g_set_error_literal (error, G_FILE_ERROR,
534 g_file_error_from_errno (err),
535 g_strerror (err));
536 return (GIOChannel *)NULL;
539 if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
541 int err = errno;
542 close (fid);
543 g_set_error_literal (error, G_FILE_ERROR,
544 g_file_error_from_errno (err),
545 g_strerror (err));
546 return (GIOChannel *)NULL;
549 channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
551 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
552 || S_ISBLK (buffer.st_mode);
554 switch (mode_num)
556 case MODE_R:
557 channel->is_readable = TRUE;
558 channel->is_writeable = FALSE;
559 break;
560 case MODE_W:
561 case MODE_A:
562 channel->is_readable = FALSE;
563 channel->is_writeable = TRUE;
564 break;
565 case MODE_R_PLUS:
566 case MODE_W_PLUS:
567 case MODE_A_PLUS:
568 channel->is_readable = TRUE;
569 channel->is_writeable = TRUE;
570 break;
571 case MODE_PLUS:
572 default:
573 g_assert_not_reached ();
576 g_io_channel_init (channel);
577 channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
578 channel->funcs = &unix_channel_funcs;
580 ((GIOUnixChannel *) channel)->fd = fid;
581 return channel;
585 * g_io_channel_unix_new:
586 * @fd: a file descriptor.
588 * Creates a new #GIOChannel given a file descriptor. On UNIX systems
589 * this works for plain files, pipes, and sockets.
591 * The returned #GIOChannel has a reference count of 1.
593 * The default encoding for #GIOChannel is UTF-8. If your application
594 * is reading output from a command using via pipe, you may need to set
595 * the encoding to the encoding of the current locale (see
596 * g_get_charset()) with the g_io_channel_set_encoding() function.
598 * If you want to read raw binary data without interpretation, then
599 * call the g_io_channel_set_encoding() function with %NULL for the
600 * encoding argument.
602 * This function is available in GLib on Windows, too, but you should
603 * avoid using it on Windows. The domain of file descriptors and
604 * sockets overlap. There is no way for GLib to know which one you mean
605 * in case the argument you pass to this function happens to be both a
606 * valid file descriptor and socket. If that happens a warning is
607 * issued, and GLib assumes that it is the file descriptor you mean.
609 * Returns: a new #GIOChannel.
611 GIOChannel *
612 g_io_channel_unix_new (gint fd)
614 struct stat buffer;
615 GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
616 GIOChannel *channel = (GIOChannel *)unix_channel;
618 g_io_channel_init (channel);
619 channel->funcs = &unix_channel_funcs;
621 unix_channel->fd = fd;
623 /* I'm not sure if fstat on a non-file (e.g., socket) works
624 * it should be safe to say if it fails, the fd isn't seekable.
626 /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
627 * succeed in most cases.
629 if (fstat (unix_channel->fd, &buffer) == 0)
630 channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
631 || S_ISBLK (buffer.st_mode);
632 else /* Assume not seekable */
633 channel->is_seekable = FALSE;
635 g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
637 return channel;
641 * g_io_channel_unix_get_fd:
642 * @channel: a #GIOChannel, created with g_io_channel_unix_new().
644 * Returns the file descriptor of the #GIOChannel.
646 * On Windows this function returns the file descriptor or socket of
647 * the #GIOChannel.
649 * Returns: the file descriptor of the #GIOChannel.
651 gint
652 g_io_channel_unix_get_fd (GIOChannel *channel)
654 GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
655 return unix_channel->fd;