os/ossrv/glib/glib/giounix.c
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
     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  * Portions copyright (c) 2006-2009 Nokia Corporation.  All rights reserved.
     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.
    11  *
    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.
    16  *
    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.
    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 #ifndef __SYMBIAN32__
    37 #define _POSIX_SOURCE		/* for SSIZE_MAX */
    38 #endif//__SYMBIAN32__
    39 
    40 #include <sys/types.h>
    41 #include <sys/stat.h>
    42 #include <stdio.h>
    43 #include <unistd.h>
    44 #include <errno.h>
    45 #include <string.h>
    46 #include <fcntl.h>
    47 
    48 #include "glib.h"
    49 #include "galias.h"
    50 
    51 #ifdef __SYMBIAN32__
    52 #include "glib_wsd.h"
    53 #endif /* __SYMBIAN32__ */
    54 
    55 /*
    56  * Unix IO Channels
    57  */
    58 
    59 typedef struct _GIOUnixChannel GIOUnixChannel;
    60 typedef struct _GIOUnixWatch GIOUnixWatch;
    61 
    62 struct _GIOUnixChannel
    63 {
    64   GIOChannel channel;
    65   gint fd;
    66 };
    67 
    68 struct _GIOUnixWatch
    69 {
    70   GSource       source;
    71   GPollFD       pollfd;
    72   GIOChannel   *channel;
    73   GIOCondition  condition;
    74 };
    75 
    76 
    77 static GIOStatus	g_io_unix_read		(GIOChannel   *channel,
    78 						 gchar        *buf,
    79 						 gsize         count,
    80 						 gsize        *bytes_read,
    81 						 GError      **err);
    82 static GIOStatus	g_io_unix_write		(GIOChannel   *channel,
    83 						 const gchar  *buf,
    84 						 gsize         count,
    85 						 gsize        *bytes_written,
    86 						 GError      **err);
    87 static GIOStatus	g_io_unix_seek		(GIOChannel   *channel,
    88 						 gint64        offset,
    89 						 GSeekType     type,
    90 						 GError      **err);
    91 static GIOStatus	g_io_unix_close		(GIOChannel   *channel,
    92 						 GError      **err);
    93 static void		g_io_unix_free		(GIOChannel   *channel);
    94 static GSource*		g_io_unix_create_watch	(GIOChannel   *channel,
    95 						 GIOCondition  condition);
    96 static GIOStatus	g_io_unix_set_flags	(GIOChannel   *channel,
    97                        				 GIOFlags      flags,
    98 						 GError      **err);
    99 static GIOFlags 	g_io_unix_get_flags	(GIOChannel   *channel);
   100 
   101 static gboolean g_io_unix_prepare  (GSource     *source,
   102 				    gint        *timeout);
   103 static gboolean g_io_unix_check    (GSource     *source);
   104 static gboolean g_io_unix_dispatch (GSource     *source,
   105 				    GSourceFunc  callback,
   106 				    gpointer     user_data);
   107 static void     g_io_unix_finalize (GSource     *source);
   108 
   109 #if EMULATOR
   110 
   111 PLS(g_io_watch_funcs,giounix,GSourceFuncs)
   112 #define g_io_watch_funcs  (*FUNCTION_NAME(g_io_watch_funcs,giounix)())
   113 
   114 const GSourceFuncs temp_g_io_watch_funcs = {
   115   g_io_unix_prepare,
   116   g_io_unix_check,
   117   g_io_unix_dispatch,
   118   g_io_unix_finalize
   119 };
   120 
   121 
   122 #else
   123 
   124 GSourceFuncs g_io_watch_funcs = {
   125   g_io_unix_prepare,
   126   g_io_unix_check,
   127   g_io_unix_dispatch,
   128   g_io_unix_finalize
   129 };
   130 
   131 #endif /* EMULATOR */
   132 
   133 #ifdef __SYMBIAN32__
   134 EXPORT_C GSourceFuncs *_g_io_watch_funcs()
   135 {
   136 	return &g_io_watch_funcs;
   137 }
   138 #endif /* __SYMBIAN32__ */
   139 
   140 #if EMULATOR
   141 
   142 PLS(unix_channel_funcs,giounix,GIOFuncs )
   143 #define unix_channel_funcs (*FUNCTION_NAME(unix_channel_funcs,giounix)())
   144 
   145 const GIOFuncs temp_unix_channel_funcs = {
   146   g_io_unix_read,
   147   g_io_unix_write,
   148   g_io_unix_seek,
   149   g_io_unix_close,
   150   g_io_unix_create_watch,
   151   g_io_unix_free,
   152   g_io_unix_set_flags,
   153   g_io_unix_get_flags,
   154 };
   155 
   156 
   157 #else
   158 
   159 static GIOFuncs unix_channel_funcs = {
   160   g_io_unix_read,
   161   g_io_unix_write,
   162   g_io_unix_seek,
   163   g_io_unix_close,
   164   g_io_unix_create_watch,
   165   g_io_unix_free,
   166   g_io_unix_set_flags,
   167   g_io_unix_get_flags,
   168 };
   169 
   170 #endif /* EMULATOR */
   171 
   172 static gboolean 
   173 g_io_unix_prepare (GSource  *source,
   174 		   gint     *timeout)
   175 {
   176   GIOUnixWatch *watch = (GIOUnixWatch *)source;
   177   GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
   178 
   179   *timeout = -1;
   180 
   181   /* Only return TRUE here if _all_ bits in watch->condition will be set
   182    */
   183   return ((watch->condition & buffer_condition) == watch->condition);
   184 }
   185 
   186 static gboolean 
   187 g_io_unix_check (GSource  *source)
   188 {
   189   GIOUnixWatch *watch = (GIOUnixWatch *)source;
   190   GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
   191   GIOCondition poll_condition = watch->pollfd.revents;
   192 
   193   return ((poll_condition | buffer_condition) & watch->condition);
   194 }
   195 
   196 static gboolean
   197 g_io_unix_dispatch (GSource     *source,
   198 		    GSourceFunc  callback,
   199 		    gpointer     user_data)
   200 
   201 {
   202   GIOFunc func = (GIOFunc)callback;
   203   GIOUnixWatch *watch = (GIOUnixWatch *)source;
   204   GIOCondition buffer_condition = g_io_channel_get_buffer_condition (watch->channel);
   205 
   206   if (!func)
   207     {
   208       g_warning ("IO watch dispatched without callback\n"
   209 		 "You must call g_source_connect().");
   210       return FALSE;
   211     }
   212   
   213   return (*func) (watch->channel,
   214 		  (watch->pollfd.revents | buffer_condition) & watch->condition,
   215 		  user_data);
   216 }
   217 
   218 static void 
   219 g_io_unix_finalize (GSource *source)
   220 {
   221   GIOUnixWatch *watch = (GIOUnixWatch *)source;
   222 
   223   g_io_channel_unref (watch->channel);
   224 }
   225 
   226 static GIOStatus
   227 g_io_unix_read (GIOChannel *channel, 
   228 		gchar      *buf, 
   229 		gsize       count,
   230 		gsize      *bytes_read,
   231 		GError    **err)
   232 {
   233   GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
   234   gssize result;
   235 
   236   if (count > SSIZE_MAX) /* At least according to the Debian manpage for read */
   237     count = SSIZE_MAX;
   238 
   239  retry:
   240   result = read (unix_channel->fd, buf, count);
   241 
   242   if (result < 0)
   243     {
   244       int errsv = errno;
   245       *bytes_read = 0;
   246 
   247       switch (errsv)
   248         {
   249 #ifdef EINTR
   250           case EINTR:
   251             goto retry;
   252 #endif
   253 #ifdef EAGAIN
   254           case EAGAIN:
   255             return G_IO_STATUS_AGAIN;
   256 #endif
   257           default:
   258             g_set_error_literal (err, G_IO_CHANNEL_ERROR,
   259                                  g_io_channel_error_from_errno (errsv),
   260                                  g_strerror (errsv));
   261             return G_IO_STATUS_ERROR;
   262         }
   263     }
   264 
   265   *bytes_read = result;
   266 
   267   return (result > 0) ? G_IO_STATUS_NORMAL : G_IO_STATUS_EOF;
   268 }
   269 
   270 static GIOStatus
   271 g_io_unix_write (GIOChannel  *channel, 
   272 		 const gchar *buf, 
   273 		 gsize       count,
   274 		 gsize      *bytes_written,
   275 		 GError    **err)
   276 {
   277   GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
   278   gssize result;
   279 
   280  retry:
   281   result = write (unix_channel->fd, buf, count);
   282 
   283   if (result < 0)
   284     {
   285       int errsv = errno;
   286       *bytes_written = 0;
   287 
   288       switch (errsv)
   289         {
   290 #ifdef EINTR
   291           case EINTR:
   292             goto retry;
   293 #endif
   294 #ifdef EAGAIN
   295           case EAGAIN:
   296             return G_IO_STATUS_AGAIN;
   297 #endif
   298           default:
   299             g_set_error_literal (err, G_IO_CHANNEL_ERROR,
   300                                  g_io_channel_error_from_errno (errsv),
   301                                  g_strerror (errsv));
   302             return G_IO_STATUS_ERROR;
   303         }
   304     }
   305 
   306   *bytes_written = result;
   307 
   308   return G_IO_STATUS_NORMAL;
   309 }
   310 
   311 static GIOStatus
   312 g_io_unix_seek (GIOChannel *channel,
   313 		gint64      offset, 
   314 		GSeekType   type,
   315                 GError    **err)
   316 {
   317   GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
   318   int whence;
   319   off_t tmp_offset;
   320   off_t result;
   321 
   322   switch (type)
   323     {
   324     case G_SEEK_SET:
   325       whence = SEEK_SET;
   326       break;
   327     case G_SEEK_CUR:
   328       whence = SEEK_CUR;
   329       break;
   330     case G_SEEK_END:
   331       whence = SEEK_END;
   332       break;
   333     default:
   334       whence = -1; /* Shut the compiler up */
   335       g_assert_not_reached ();
   336     }
   337 
   338   tmp_offset = offset;
   339   if (tmp_offset != offset)
   340     {
   341       g_set_error_literal (err, G_IO_CHANNEL_ERROR,
   342                            g_io_channel_error_from_errno (EINVAL),
   343                            g_strerror (EINVAL));
   344       return G_IO_STATUS_ERROR;
   345     }
   346   
   347   result = lseek (unix_channel->fd, tmp_offset, whence);
   348 
   349   if (result < 0)
   350     {
   351       int errsv = errno;
   352       g_set_error_literal (err, G_IO_CHANNEL_ERROR,
   353                            g_io_channel_error_from_errno (errsv),
   354                            g_strerror (errsv));
   355       return G_IO_STATUS_ERROR;
   356     }
   357 
   358   return G_IO_STATUS_NORMAL;
   359 }
   360 
   361 
   362 static GIOStatus
   363 g_io_unix_close (GIOChannel *channel,
   364 		 GError    **err)
   365 {
   366   GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
   367 
   368   if (close (unix_channel->fd) < 0)
   369     {
   370       int errsv = errno;
   371       g_set_error_literal (err, G_IO_CHANNEL_ERROR,
   372                            g_io_channel_error_from_errno (errsv),
   373                            g_strerror (errsv));
   374       return G_IO_STATUS_ERROR;
   375     }
   376 
   377   return G_IO_STATUS_NORMAL;
   378 }
   379 
   380 static void 
   381 g_io_unix_free (GIOChannel *channel)
   382 {
   383   GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
   384 
   385   g_free (unix_channel);
   386 }
   387 
   388 static GSource *
   389 g_io_unix_create_watch (GIOChannel   *channel,
   390 			GIOCondition  condition)
   391 {
   392   GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
   393   GSource *source;
   394   GIOUnixWatch *watch;
   395 
   396 
   397   source = g_source_new (&g_io_watch_funcs, sizeof (GIOUnixWatch));
   398   watch = (GIOUnixWatch *)source;
   399   
   400   watch->channel = channel;
   401   g_io_channel_ref (channel);
   402   
   403   watch->condition = condition;
   404 
   405   watch->pollfd.fd = unix_channel->fd;
   406   watch->pollfd.events = condition;
   407 
   408   g_source_add_poll (source, &watch->pollfd);
   409 
   410   return source;
   411 }
   412 
   413 static GIOStatus
   414 g_io_unix_set_flags (GIOChannel *channel,
   415                      GIOFlags    flags,
   416                      GError    **err)
   417 {
   418   glong fcntl_flags;
   419   GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
   420 
   421   fcntl_flags = 0;
   422 
   423   if (flags & G_IO_FLAG_APPEND)
   424     fcntl_flags |= O_APPEND;
   425   if (flags & G_IO_FLAG_NONBLOCK)
   426 #ifdef O_NONBLOCK
   427     fcntl_flags |= O_NONBLOCK;
   428 #else
   429     fcntl_flags |= O_NDELAY;
   430 #endif
   431 
   432   if (fcntl (unix_channel->fd, F_SETFL, fcntl_flags) == -1)
   433     {
   434       int errsv = errno;
   435       g_set_error_literal (err, G_IO_CHANNEL_ERROR,
   436                            g_io_channel_error_from_errno (errsv),
   437                            g_strerror (errsv));
   438       return G_IO_STATUS_ERROR;
   439     }
   440 
   441   return G_IO_STATUS_NORMAL;
   442 }
   443 
   444 static GIOFlags
   445 g_io_unix_get_flags (GIOChannel *channel)
   446 {
   447   GIOFlags flags = 0;
   448   glong fcntl_flags;
   449   GIOUnixChannel *unix_channel = (GIOUnixChannel *) channel;
   450 
   451   fcntl_flags = fcntl (unix_channel->fd, F_GETFL);
   452 
   453   if (fcntl_flags == -1)
   454     {
   455       int err = errno;
   456       g_warning (G_STRLOC "Error while getting flags for FD: %s (%d)\n",
   457 		 g_strerror (err), err);
   458       return 0;
   459     }
   460 
   461   if (fcntl_flags & O_APPEND)
   462     flags |= G_IO_FLAG_APPEND;
   463 #ifdef O_NONBLOCK
   464   if (fcntl_flags & O_NONBLOCK)
   465 #else
   466   if (fcntl_flags & O_NDELAY)
   467 #endif
   468     flags |= G_IO_FLAG_NONBLOCK;
   469 
   470   switch (fcntl_flags & (O_RDONLY | O_WRONLY | O_RDWR))
   471     {
   472       case O_RDONLY:
   473         channel->is_readable = TRUE;
   474         channel->is_writeable = FALSE;
   475         break;
   476       case O_WRONLY:
   477         channel->is_readable = FALSE;
   478         channel->is_writeable = TRUE;
   479         break;
   480       case O_RDWR:
   481         channel->is_readable = TRUE;
   482         channel->is_writeable = TRUE;
   483         break;
   484       default:
   485         g_assert_not_reached ();
   486     }
   487 
   488   return flags;
   489 }
   490 
   491 EXPORT_C GIOChannel *
   492 g_io_channel_new_file (const gchar *filename,
   493                        const gchar *mode,
   494                        GError     **error)
   495 {
   496   int fid, flags;
   497   mode_t create_mode;
   498   GIOChannel *channel;
   499   enum { /* Cheesy hack */
   500     MODE_R = 1 << 0,
   501     MODE_W = 1 << 1,
   502     MODE_A = 1 << 2,
   503     MODE_PLUS = 1 << 3
   504   } mode_num;
   505   struct stat buffer;
   506 
   507   g_return_val_if_fail (filename != NULL, NULL);
   508   g_return_val_if_fail (mode != NULL, NULL);
   509   g_return_val_if_fail ((error == NULL) || (*error == NULL), NULL);
   510 
   511   switch (mode[0])
   512     {
   513       case 'r':
   514         mode_num = MODE_R;
   515         break;
   516       case 'w':
   517         mode_num = MODE_W;
   518         break;
   519       case 'a':
   520         mode_num = MODE_A;
   521         break;
   522       default:
   523         g_warning ("Invalid GIOFileMode %s.\n", mode);
   524         return NULL;
   525     }
   526 
   527   switch (mode[1])
   528     {
   529       case '\0':
   530         break;
   531       case '+':
   532         if (mode[2] == '\0')
   533           {
   534             mode_num |= MODE_PLUS;
   535             break;
   536           }
   537         /* Fall through */
   538       default:
   539         g_warning ("Invalid GIOFileMode %s.\n", mode);
   540         return NULL;
   541     }
   542 
   543   switch (mode_num)
   544     {
   545       case MODE_R:
   546         flags = O_RDONLY;
   547         break;
   548       case MODE_W:
   549         flags = O_WRONLY | O_TRUNC | O_CREAT;
   550         break;
   551       case MODE_A:
   552         flags = O_WRONLY | O_APPEND | O_CREAT;
   553         break;
   554       case MODE_R | MODE_PLUS:
   555         flags = O_RDWR;
   556         break;
   557       case MODE_W | MODE_PLUS:
   558         flags = O_RDWR | O_TRUNC | O_CREAT;
   559         break;
   560       case MODE_A | MODE_PLUS:
   561         flags = O_RDWR | O_APPEND | O_CREAT;
   562         break;
   563       default:
   564         g_assert_not_reached ();
   565         flags = 0;
   566     }
   567 
   568   create_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
   569   fid = open (filename, flags, create_mode);
   570   if (fid == -1)
   571     {
   572       int err = errno;
   573       g_set_error_literal (error, G_FILE_ERROR,
   574                            g_file_error_from_errno (err),
   575                            g_strerror (err));
   576       return (GIOChannel *)NULL;
   577     }
   578 
   579   if (fstat (fid, &buffer) == -1) /* In case someone opens a FIFO */
   580     {
   581       int err = errno;
   582       close (fid);
   583       g_set_error_literal (error, G_FILE_ERROR,
   584                            g_file_error_from_errno (err),
   585                            g_strerror (err));
   586       return (GIOChannel *)NULL;
   587     }
   588 
   589   channel = (GIOChannel *) g_new (GIOUnixChannel, 1);
   590 
   591   channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
   592                          || S_ISBLK (buffer.st_mode);
   593 
   594   switch (mode_num)
   595     {
   596       case MODE_R:
   597         channel->is_readable = TRUE;
   598         channel->is_writeable = FALSE;
   599         break;
   600       case MODE_W:
   601       case MODE_A:
   602         channel->is_readable = FALSE;
   603         channel->is_writeable = TRUE;
   604         break;
   605       case MODE_R | MODE_PLUS:
   606       case MODE_W | MODE_PLUS:
   607       case MODE_A | MODE_PLUS:
   608         channel->is_readable = TRUE;
   609         channel->is_writeable = TRUE;
   610         break;
   611       default:
   612         g_assert_not_reached ();
   613     }
   614 
   615   g_io_channel_init (channel);
   616   channel->close_on_unref = TRUE; /* must be after g_io_channel_init () */
   617   channel->funcs = &unix_channel_funcs;
   618 
   619   ((GIOUnixChannel *) channel)->fd = fid;
   620   return channel;
   621 }
   622 
   623 EXPORT_C GIOChannel *
   624 g_io_channel_unix_new (gint fd)
   625 {
   626   struct stat buffer;
   627   GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
   628   GIOChannel *channel = (GIOChannel *)unix_channel;
   629 
   630   g_io_channel_init (channel);
   631   channel->funcs = &unix_channel_funcs;
   632 
   633   unix_channel->fd = fd;
   634 
   635   /* I'm not sure if fstat on a non-file (e.g., socket) works
   636    * it should be safe to say if it fails, the fd isn't seekable.
   637    */
   638   /* Newer UNIX versions support S_ISSOCK(), fstat() will probably
   639    * succeed in most cases.
   640    */
   641   if (fstat (unix_channel->fd, &buffer) == 0)
   642     channel->is_seekable = S_ISREG (buffer.st_mode) || S_ISCHR (buffer.st_mode)
   643                            || S_ISBLK (buffer.st_mode);
   644   else /* Assume not seekable */
   645     channel->is_seekable = FALSE;
   646 
   647   g_io_unix_get_flags (channel); /* Sets is_readable, is_writeable */
   648 
   649   return channel;
   650 }
   651 
   652 EXPORT_C gint
   653 g_io_channel_unix_get_fd (GIOChannel *channel)
   654 {
   655   GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
   656   return unix_channel->fd;
   657 }
   658 
   659 #define __G_IO_UNIX_C__
   660 #include "galiasdef.c"