[kdbus] sync with kdbus (kdbus.h - commit: 5ae1ecac44cb)
[platform/upstream/glib.git] / glib / giounix.c
index c7fe84d..bb773c3 100644 (file)
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
- * License along with this library; if not, write to the
- * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
- * Boston, MA 02111-1307, USA.
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  */
 
 /*
  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
  * file for a list of people on the GLib Team.  See the ChangeLog
  * files for a list of changes.  These files are distributed with
- * GLib at ftp://ftp.gtk.org/pub/gtk/. 
+ * GLib at ftp://ftp.gtk.org/pub/gtk/.
  */
 
-/* 
+/*
  * MT safe
  */
 
-#include "glib.h"
+#include "config.h"
+
+#define _POSIX_SOURCE          /* for SSIZE_MAX */
+
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <stdio.h>
 #include <errno.h>
 #include <string.h>
 #include <fcntl.h>
+#include <glib/gstdio.h>
+
+#include "giochannel.h"
+
+#include "gerror.h"
+#include "gfileutils.h"
+#include "gstrfuncs.h"
+#include "gtestutils.h"
 
 /*
  * Unix IO Channels
@@ -59,7 +68,6 @@ struct _GIOUnixWatch
   GPollFD       pollfd;
   GIOChannel   *channel;
   GIOCondition  condition;
-  GIOFunc       callback;
 };
 
 
@@ -74,7 +82,7 @@ static GIOStatus      g_io_unix_write         (GIOChannel   *channel,
                                                 gsize        *bytes_written,
                                                 GError      **err);
 static GIOStatus       g_io_unix_seek          (GIOChannel   *channel,
-                                                glong         offset,
+                                                gint64        offset,
                                                 GSeekType     type,
                                                 GError      **err);
 static GIOStatus       g_io_unix_close         (GIOChannel   *channel,
@@ -95,14 +103,14 @@ static gboolean g_io_unix_dispatch (GSource     *source,
                                    gpointer     user_data);
 static void     g_io_unix_finalize (GSource     *source);
 
-GSourceFuncs unix_watch_funcs = {
+GSourceFuncs g_io_watch_funcs = {
   g_io_unix_prepare,
   g_io_unix_check,
   g_io_unix_dispatch,
   g_io_unix_finalize
 };
 
-GIOFuncs unix_channel_funcs = {
+static GIOFuncs unix_channel_funcs = {
   g_io_unix_read,
   g_io_unix_write,
   g_io_unix_seek,
@@ -177,14 +185,18 @@ g_io_unix_read (GIOChannel *channel,
   GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
   gssize result;
 
+  if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
+    count = SSIZE_MAX;
+
  retry:
   result = read (unix_channel->fd, buf, count);
 
   if (result < 0)
     {
+      int errsv = errno;
       *bytes_read = 0;
 
-      switch (errno)
+      switch (errsv)
         {
 #ifdef EINTR
           case EINTR:
@@ -195,9 +207,9 @@ g_io_unix_read (GIOChannel *channel,
             return G_IO_STATUS_AGAIN;
 #endif
           default:
-            g_set_error (err, G_IO_CHANNEL_ERROR,
-                         g_channel_error_from_errno (errno),
-                         strerror (errno));
+            g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+                                 g_io_channel_error_from_errno (errsv),
+                                 g_strerror (errsv));
             return G_IO_STATUS_ERROR;
         }
     }
@@ -222,9 +234,10 @@ g_io_unix_write (GIOChannel  *channel,
 
   if (result < 0)
     {
+      int errsv = errno;
       *bytes_written = 0;
 
-      switch (errno)
+      switch (errsv)
         {
 #ifdef EINTR
           case EINTR:
@@ -235,9 +248,9 @@ g_io_unix_write (GIOChannel  *channel,
             return G_IO_STATUS_AGAIN;
 #endif
           default:
-            g_set_error (err, G_IO_CHANNEL_ERROR,
-                         g_channel_error_from_errno (errno),
-                         strerror (errno));
+            g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+                                 g_io_channel_error_from_errno (errsv),
+                                 g_strerror (errsv));
             return G_IO_STATUS_ERROR;
         }
     }
@@ -249,12 +262,13 @@ g_io_unix_write (GIOChannel  *channel,
 
 static GIOStatus
 g_io_unix_seek (GIOChannel *channel,
-               glong       offset, 
+               gint64      offset, 
                GSeekType   type,
                 GError    **err)
 {
   GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
   int whence;
+  off_t tmp_offset;
   off_t result;
 
   switch (type)
@@ -273,13 +287,23 @@ g_io_unix_seek (GIOChannel *channel,
       g_assert_not_reached ();
     }
 
-  result = lseek (unix_channel->fd, offset, whence);
+  tmp_offset = offset;
+  if (tmp_offset != offset)
+    {
+      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+                           g_io_channel_error_from_errno (EINVAL),
+                           g_strerror (EINVAL));
+      return G_IO_STATUS_ERROR;
+    }
+  
+  result = lseek (unix_channel->fd, tmp_offset, whence);
 
   if (result < 0)
     {
-      g_set_error (err, G_IO_CHANNEL_ERROR,
-                  g_channel_error_from_errno (errno),
-                  strerror (errno));
+      int errsv = errno;
+      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+                           g_io_channel_error_from_errno (errsv),
+                           g_strerror (errsv));
       return G_IO_STATUS_ERROR;
     }
 
@@ -295,9 +319,10 @@ g_io_unix_close (GIOChannel *channel,
 
   if (close (unix_channel->fd) < 0)
     {
-      g_set_error (err, G_IO_CHANNEL_ERROR,
-                  g_channel_error_from_errno (errno),
-                  strerror (errno));
+      int errsv = errno;
+      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+                           g_io_channel_error_from_errno (errsv),
+                           g_strerror (errsv));
       return G_IO_STATUS_ERROR;
     }
 
@@ -321,7 +346,8 @@ g_io_unix_create_watch (GIOChannel   *channel,
   GIOUnixWatch *watch;
 
 
-  source = g_source_new (&unix_watch_funcs, sizeof (GIOUnixWatch));
+  source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
+  g_source_set_name (source, "GIOChannel (Unix)");
   watch = (GIOUnixWatch *)source;
   
   watch->channel = channel;
@@ -337,53 +363,31 @@ g_io_unix_create_watch (GIOChannel   *channel,
   return source;
 }
 
-static const GIOFlags g_io_unix_fcntl_flags[] = {
-  G_IO_FLAG_APPEND,
-  G_IO_FLAG_NONBLOCK,
-};
-static const glong g_io_unix_fcntl_posix_flags[] = {
-  O_APPEND,
-#ifdef O_NONBLOCK
-  O_NONBLOCK,
-#else
-  O_NDELAY,
-#endif
-};
-#define G_IO_UNIX_NUM_FCNTL_FLAGS G_N_ELEMENTS (g_io_unix_fcntl_flags)
-
-static const GIOFlags g_io_unix_fcntl_flags_read_only[] = {
-  G_IO_FLAG_IS_READABLE,
-  G_IO_FLAG_IS_WRITEABLE,
-};
-static const glong g_io_unix_fcntl_posix_flags_read_only[] = {
-  O_RDONLY | O_RDWR,
-  O_WRONLY | O_RDWR,
-};
-/* Only need to map posix_flags -> flags for read only, not the
- * other way around, so this works.
- */
-#define G_IO_UNIX_NUM_FCNTL_FLAGS_READ_ONLY G_N_ELEMENTS (g_io_unix_fcntl_flags_read_only)
-
 static GIOStatus
 g_io_unix_set_flags (GIOChannel *channel,
                      GIOFlags    flags,
                      GError    **err)
 {
   glong fcntl_flags;
-  gint loop;
   GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
 
   fcntl_flags = 0;
 
-  for (loop = 0; loop < G_IO_UNIX_NUM_FCNTL_FLAGS; loop++)
-    if (flags & g_io_unix_fcntl_flags[loop])
-      fcntl_flags |= g_io_unix_fcntl_posix_flags[loop];
+  if (flags & G_IO_FLAG_APPEND)
+    fcntl_flags |= O_APPEND;
+  if (flags & G_IO_FLAG_NONBLOCK)
+#ifdef O_NONBLOCK
+    fcntl_flags |= O_NONBLOCK;
+#else
+    fcntl_flags |= O_NDELAY;
+#endif
 
   if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
     {
-      g_set_error (err, G_IO_CHANNEL_ERROR,
-                  g_channel_error_from_errno (errno),
-                  g_strerror (errno));
+      int errsv = errno;
+      g_set_error_literal (err, G_IO_CHANNEL_ERROR,
+                           g_io_channel_error_from_errno (errsv),
+                           g_strerror (errsv));
       return G_IO_STATUS_ERROR;
     }
 
@@ -395,99 +399,217 @@ g_io_unix_get_flags (GIOChannel *channel)
 {
   GIOFlags flags = 0;
   glong fcntl_flags;
-  gint loop;
   GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
-  struct stat buffer;
 
   fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
+
   if (fcntl_flags == -1)
     {
+      int err = errno;
       g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
-                g_strerror (errno), errno);
+                g_strerror (err), err);
       return 0;
     }
-  if (!channel->seekable_cached)
-    {
-      channel->seekable_cached = TRUE;
-
-      /* I'm not sure if fstat on a non-file (e.g., socket) works
-       * it should be safe to sat if it fails, the fd isn't seekable.
-       */
-      if (fstat (unix_channel->fd, &buffer) == -1 ||
-         !S_ISREG (buffer.st_mode))
-       channel->is_seekable = FALSE;
-      else
-       channel->is_seekable = TRUE;
-    }
-
-  for (loop = 0; loop < G_IO_UNIX_NUM_FCNTL_FLAGS; loop++)
-    if (fcntl_flags & g_io_unix_fcntl_posix_flags[loop])
-      flags |= g_io_unix_fcntl_flags[loop];
 
-  for (loop = 0; loop < G_IO_UNIX_NUM_FCNTL_FLAGS_READ_ONLY; loop++)
-    if (fcntl_flags & g_io_unix_fcntl_posix_flags_read_only[loop])
-      flags |= g_io_unix_fcntl_flags_read_only[loop];
+  if (fcntl_flags & O_APPEND)
+    flags |= G_IO_FLAG_APPEND;
+#ifdef O_NONBLOCK
+  if (fcntl_flags & O_NONBLOCK)
+#else
+  if (fcntl_flags & O_NDELAY)
+#endif
+    flags |= G_IO_FLAG_NONBLOCK;
 
-  if (channel->is_seekable)
-    flags |= G_IO_FLAG_IS_SEEKABLE;
+  switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
+    {
+      case O_RDONLY:
+        channel->is_readable = TRUE;
+        channel->is_writeable = FALSE;
+        break;
+      case O_WRONLY:
+        channel->is_readable = FALSE;
+        channel->is_writeable = TRUE;
+        break;
+      case O_RDWR:
+        channel->is_readable = TRUE;
+        channel->is_writeable = TRUE;
+        break;
+      default:
+        g_assert_not_reached ();
+    }
 
   return flags;
 }
 
 GIOChannel *
 g_io_channel_new_file (const gchar *filename,
-                       GIOFileMode  mode,
+                       const gchar *mode,
                        GError     **error)
 {
-  FILE *f;
-  int fid;
-  gchar *mode_name;
+  int fid, flags;
+  mode_t create_mode;
   GIOChannel *channel;
+  enum { /* Cheesy hack */
+    MODE_R = 1 << 0,
+    MODE_W = 1 << 1,
+    MODE_A = 1 << 2,
+    MODE_PLUS = 1 << 3,
+    MODE_R_PLUS = MODE_R | MODE_PLUS,
+    MODE_W_PLUS = MODE_W | MODE_PLUS,
+    MODE_A_PLUS = MODE_A | MODE_PLUS
+  } mode_num;
+  struct stat buffer;
+
+  g_return_val_if_fail (filename != NULL, NULL);
+  g_return_val_if_fail (mode != NULL, NULL);
+  g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
+
+  switch (mode[0])
+    {
+      case 'r':
+        mode_num = MODE_R;
+        break;
+      case 'w':
+        mode_num = MODE_W;
+        break;
+      case 'a':
+        mode_num = MODE_A;
+        break;
+      default:
+        g_warning ("Invalid GIOFileMode %s.\n", mode);
+        return NULL;
+    }
 
-  switch (mode)
+  switch (mode[1])
     {
-      case G_IO_FILE_MODE_READ:
-        mode_name = "r";
+      case '\0':
         break;
-      case G_IO_FILE_MODE_WRITE:
-        mode_name = "w";
+      case '+':
+        if (mode[2] == '\0')
+          {
+            mode_num |= MODE_PLUS;
+            break;
+          }
+        /* Fall through */
+      default:
+        g_warning ("Invalid GIOFileMode %s.\n", mode);
+        return NULL;
+    }
+
+  switch (mode_num)
+    {
+      case MODE_R:
+        flags = O_RDONLY;
         break;
-      case G_IO_FILE_MODE_APPEND:
-        mode_name = "a";
+      case MODE_W:
+        flags = O_WRONLY | O_TRUNC | O_CREAT;
         break;
-      case G_IO_FILE_MODE_READ_WRITE:
-        mode_name = "r+";
+      case MODE_A:
+        flags = O_WRONLY | O_APPEND | O_CREAT;
         break;
-      case G_IO_FILE_MODE_READ_WRITE_TRUNCATE:
-        mode_name = "w+";
+      case MODE_R_PLUS:
+        flags = O_RDWR;
         break;
-      case G_IO_FILE_MODE_READ_WRITE_APPEND:
-        mode_name = "a+";
+      case MODE_W_PLUS:
+        flags = O_RDWR | O_TRUNC | O_CREAT;
         break;
+      case MODE_A_PLUS:
+        flags = O_RDWR | O_APPEND | O_CREAT;
+        break;
+      case MODE_PLUS:
       default:
-        g_warning ("Invalid GIOFileMode %i.\n", mode);
-        return NULL;
+        g_assert_not_reached ();
+        flags = 0;
     }
 
-  f = fopen (filename, mode_name);
-  if (!f)
+  create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
+
+  fid = g_open (filename, flags, create_mode);
+  if (fid == -1)
+    {
+      int err = errno;
+      g_set_error_literal (error, G_FILE_ERROR,
+                           g_file_error_from_errno (err),
+                           g_strerror (err));
+      return (GIOChannel *)NULL;
+    }
+
+  if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
     {
-      g_set_error (error, G_FILE_ERROR,
-                   g_file_error_from_errno (errno),
-                   strerror (errno));
+      int err = errno;
+      close (fid);
+      g_set_error_literal (error, G_FILE_ERROR,
+                           g_file_error_from_errno (err),
+                           g_strerror (err));
       return (GIOChannel *)NULL;
     }
-    
-  fid = fileno (f);
 
-  channel = g_io_channel_unix_new (fid);
-  channel->close_on_unref = TRUE;
+  channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
+
+  channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
+                         || S_ISBLK (buffer.st_mode);
+
+  switch (mode_num)
+    {
+      case MODE_R:
+        channel->is_readable = TRUE;
+        channel->is_writeable = FALSE;
+        break;
+      case MODE_W:
+      case MODE_A:
+        channel->is_readable = FALSE;
+        channel->is_writeable = TRUE;
+        break;
+      case MODE_R_PLUS:
+      case MODE_W_PLUS:
+      case MODE_A_PLUS:
+        channel->is_readable = TRUE;
+        channel->is_writeable = TRUE;
+        break;
+      case MODE_PLUS:
+      default:
+        g_assert_not_reached ();
+    }
+
+  g_io_channel_init (channel);
+  channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
+  channel->funcs = &unix_channel_funcs;
+
+  ((GIOUnixChannel *) channel)->fd = fid;
   return channel;
 }
 
+/**
+ * g_io_channel_unix_new:
+ * @fd: a file descriptor.
+ *
+ * Creates a new #GIOChannel given a file descriptor. On UNIX systems
+ * this works for plain files, pipes, and sockets.
+ *
+ * The returned #GIOChannel has a reference count of 1.
+ *
+ * The default encoding for #GIOChannel is UTF-8. If your application
+ * is reading output from a command using via pipe, you may need to set
+ * the encoding to the encoding of the current locale (see
+ * g_get_charset()) with the g_io_channel_set_encoding() function.
+ *
+ * If you want to read raw binary data without interpretation, then
+ * call the g_io_channel_set_encoding() function with %NULL for the
+ * encoding argument.
+ *
+ * This function is available in GLib on Windows, too, but you should
+ * avoid using it on Windows. The domain of file descriptors and
+ * sockets overlap. There is no way for GLib to know which one you mean
+ * in case the argument you pass to this function happens to be both a
+ * valid file descriptor and socket. If that happens a warning is
+ * issued, and GLib assumes that it is the file descriptor you mean.
+ *
+ * Returns: a new #GIOChannel.
+ **/
 GIOChannel *
 g_io_channel_unix_new (gint fd)
 {
+  struct stat buffer;
   GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
   GIOChannel *channel = (GIOChannel *)unix_channel;
 
@@ -495,9 +617,35 @@ g_io_channel_unix_new (gint fd)
   channel->funcs = &unix_channel_funcs;
 
   unix_channel->fd = fd;
+
+  /* I'm not sure if fstat on a non-file (e.g., socket) works
+   * it should be safe to say if it fails, the fd isn't seekable.
+   */
+  /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
+   * succeed in most cases.
+   */
+  if (fstat (unix_channel->fd, &buffer) == 0)
+    channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
+                           || S_ISBLK (buffer.st_mode);
+  else /* Assume not seekable */
+    channel->is_seekable = FALSE;
+
+  g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
+
   return channel;
 }
 
+/**
+ * g_io_channel_unix_get_fd:
+ * @channel: a #GIOChannel, created with g_io_channel_unix_new().
+ *
+ * Returns the file descriptor of the #GIOChannel.
+ *
+ * On Windows this function returns the file descriptor or socket of
+ * the #GIOChannel.
+ *
+ * Returns: the file descriptor of the #GIOChannel.
+ **/
 gint
 g_io_channel_unix_get_fd (GIOChannel *channel)
 {