1998-12-02 15:55:27 +01:00
|
|
|
/* GLIB - Library of useful routines for C programming
|
|
|
|
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
|
|
|
|
*
|
|
|
|
* giounix.c: IO Channels using unix file descriptors
|
|
|
|
* Copyright 1998 Owen Taylor
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
2000-07-26 13:02:02 +02:00
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
1998-12-02 15:55:27 +01:00
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2000-07-26 13:02:02 +02:00
|
|
|
* Lesser General Public License for more details.
|
1998-12-02 15:55:27 +01:00
|
|
|
*
|
2000-07-26 13:02:02 +02:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
1998-12-02 15:55:27 +01:00
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
1999-02-24 07:14:27 +01:00
|
|
|
/*
|
2000-07-26 13:02:02 +02:00
|
|
|
* Modified by the GLib Team and others 1997-2000. See the AUTHORS
|
1999-02-24 07:14:27 +01:00
|
|
|
* 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/.
|
|
|
|
*/
|
|
|
|
|
1998-12-15 06:28:02 +01:00
|
|
|
/*
|
|
|
|
* MT safe
|
|
|
|
*/
|
|
|
|
|
1998-12-02 15:55:27 +01:00
|
|
|
#include "glib.h"
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Unix IO Channels
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct _GIOUnixChannel GIOUnixChannel;
|
|
|
|
typedef struct _GIOUnixWatch GIOUnixWatch;
|
|
|
|
|
2000-12-05 21:45:33 +01:00
|
|
|
struct _GIOUnixChannel
|
|
|
|
{
|
1998-12-15 23:30:15 +01:00
|
|
|
GIOChannel channel;
|
1998-12-02 15:55:27 +01:00
|
|
|
gint fd;
|
|
|
|
};
|
|
|
|
|
2000-12-05 21:45:33 +01:00
|
|
|
struct _GIOUnixWatch
|
|
|
|
{
|
|
|
|
GSource source;
|
1998-12-02 15:55:27 +01:00
|
|
|
GPollFD pollfd;
|
|
|
|
GIOChannel *channel;
|
|
|
|
GIOCondition condition;
|
|
|
|
GIOFunc callback;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2000-12-05 21:45:33 +01:00
|
|
|
static GIOError g_io_unix_read (GIOChannel *channel,
|
|
|
|
gchar *buf,
|
|
|
|
guint count,
|
|
|
|
guint *bytes_written);
|
|
|
|
static GIOError g_io_unix_write (GIOChannel *channel,
|
|
|
|
gchar *buf,
|
|
|
|
guint count,
|
|
|
|
guint *bytes_written);
|
|
|
|
static GIOError g_io_unix_seek (GIOChannel *channel,
|
|
|
|
gint offset,
|
|
|
|
GSeekType type);
|
|
|
|
static void g_io_unix_close (GIOChannel *channel);
|
|
|
|
static void g_io_unix_free (GIOChannel *channel);
|
|
|
|
static GSource *g_io_unix_create_watch (GIOChannel *channel,
|
|
|
|
GIOCondition condition);
|
|
|
|
|
|
|
|
static gboolean g_io_unix_prepare (GSource *source,
|
|
|
|
gint *timeout);
|
|
|
|
static gboolean g_io_unix_check (GSource *source);
|
|
|
|
static gboolean g_io_unix_dispatch (GSource *source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data);
|
|
|
|
static void g_io_unix_destroy (GSource *source);
|
1998-12-02 15:55:27 +01:00
|
|
|
|
|
|
|
GSourceFuncs unix_watch_funcs = {
|
|
|
|
g_io_unix_prepare,
|
|
|
|
g_io_unix_check,
|
|
|
|
g_io_unix_dispatch,
|
|
|
|
g_io_unix_destroy
|
|
|
|
};
|
|
|
|
|
|
|
|
GIOFuncs unix_channel_funcs = {
|
|
|
|
g_io_unix_read,
|
|
|
|
g_io_unix_write,
|
|
|
|
g_io_unix_seek,
|
|
|
|
g_io_unix_close,
|
2000-12-05 21:45:33 +01:00
|
|
|
g_io_unix_create_watch,
|
1998-12-02 15:55:27 +01:00
|
|
|
g_io_unix_free,
|
|
|
|
};
|
|
|
|
|
|
|
|
static gboolean
|
2000-12-05 21:45:33 +01:00
|
|
|
g_io_unix_prepare (GSource *source,
|
|
|
|
gint *timeout)
|
1998-12-02 15:55:27 +01:00
|
|
|
{
|
|
|
|
*timeout = -1;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2000-12-05 21:45:33 +01:00
|
|
|
g_io_unix_check (GSource *source)
|
1998-12-02 15:55:27 +01:00
|
|
|
{
|
2000-12-05 21:45:33 +01:00
|
|
|
GIOUnixWatch *watch = (GIOUnixWatch *)source;
|
1998-12-02 15:55:27 +01:00
|
|
|
|
2000-12-05 21:45:33 +01:00
|
|
|
return (watch->pollfd.revents & watch->condition);
|
1998-12-02 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
2000-12-05 21:45:33 +01:00
|
|
|
g_io_unix_dispatch (GSource *source,
|
|
|
|
GSourceFunc callback,
|
|
|
|
gpointer user_data)
|
1998-12-02 15:55:27 +01:00
|
|
|
|
|
|
|
{
|
2000-12-05 21:45:33 +01:00
|
|
|
GIOFunc func = (GIOFunc)callback;
|
|
|
|
GIOUnixWatch *watch = (GIOUnixWatch *)source;
|
1998-12-02 15:55:27 +01:00
|
|
|
|
2000-12-05 21:45:33 +01:00
|
|
|
if (!func)
|
|
|
|
{
|
|
|
|
g_warning ("IO watch dispatched without callback\n"
|
|
|
|
"You must call g_source_connect().");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (*func) (watch->channel,
|
|
|
|
watch->pollfd.revents & watch->condition,
|
|
|
|
user_data);
|
1998-12-02 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2000-12-05 21:45:33 +01:00
|
|
|
g_io_unix_destroy (GSource *source)
|
1998-12-02 15:55:27 +01:00
|
|
|
{
|
2000-12-05 21:45:33 +01:00
|
|
|
GIOUnixWatch *watch = (GIOUnixWatch *)source;
|
1998-12-02 15:55:27 +01:00
|
|
|
|
2000-12-05 21:45:33 +01:00
|
|
|
g_io_channel_unref (watch->channel);
|
1998-12-02 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static GIOError
|
|
|
|
g_io_unix_read (GIOChannel *channel,
|
|
|
|
gchar *buf,
|
|
|
|
guint count,
|
|
|
|
guint *bytes_read)
|
|
|
|
{
|
1998-12-15 23:30:15 +01:00
|
|
|
GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
|
1998-12-02 15:55:27 +01:00
|
|
|
gint result;
|
|
|
|
|
|
|
|
result = read (unix_channel->fd, buf, count);
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
*bytes_read = 0;
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
return G_IO_ERROR_INVAL;
|
|
|
|
case EAGAIN:
|
2000-03-20 14:19:20 +01:00
|
|
|
case EINTR:
|
1998-12-02 15:55:27 +01:00
|
|
|
return G_IO_ERROR_AGAIN;
|
|
|
|
default:
|
|
|
|
return G_IO_ERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*bytes_read = result;
|
|
|
|
return G_IO_ERROR_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GIOError
|
|
|
|
g_io_unix_write(GIOChannel *channel,
|
|
|
|
gchar *buf,
|
|
|
|
guint count,
|
|
|
|
guint *bytes_written)
|
|
|
|
{
|
1998-12-15 23:30:15 +01:00
|
|
|
GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
|
1998-12-02 15:55:27 +01:00
|
|
|
gint result;
|
|
|
|
|
|
|
|
result = write (unix_channel->fd, buf, count);
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
*bytes_written = 0;
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
return G_IO_ERROR_INVAL;
|
|
|
|
case EAGAIN:
|
2000-03-20 14:19:20 +01:00
|
|
|
case EINTR:
|
1998-12-02 15:55:27 +01:00
|
|
|
return G_IO_ERROR_AGAIN;
|
|
|
|
default:
|
|
|
|
return G_IO_ERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*bytes_written = result;
|
|
|
|
return G_IO_ERROR_NONE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static GIOError
|
|
|
|
g_io_unix_seek (GIOChannel *channel,
|
|
|
|
gint offset,
|
|
|
|
GSeekType type)
|
|
|
|
{
|
1998-12-15 23:30:15 +01:00
|
|
|
GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
|
1998-12-02 15:55:27 +01:00
|
|
|
int whence;
|
|
|
|
off_t result;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case G_SEEK_SET:
|
|
|
|
whence = SEEK_SET;
|
|
|
|
break;
|
|
|
|
case G_SEEK_CUR:
|
|
|
|
whence = SEEK_CUR;
|
|
|
|
break;
|
|
|
|
case G_SEEK_END:
|
|
|
|
whence = SEEK_END;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
g_warning ("g_io_unix_seek: unknown seek type");
|
|
|
|
return G_IO_ERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
|
|
|
result = lseek (unix_channel->fd, offset, whence);
|
|
|
|
|
|
|
|
if (result < 0)
|
|
|
|
{
|
|
|
|
switch (errno)
|
|
|
|
{
|
|
|
|
case EINVAL:
|
|
|
|
return G_IO_ERROR_INVAL;
|
|
|
|
default:
|
|
|
|
return G_IO_ERROR_UNKNOWN;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return G_IO_ERROR_NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_io_unix_close (GIOChannel *channel)
|
|
|
|
{
|
1998-12-15 23:30:15 +01:00
|
|
|
GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
|
1998-12-02 15:55:27 +01:00
|
|
|
|
|
|
|
close (unix_channel->fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
g_io_unix_free (GIOChannel *channel)
|
|
|
|
{
|
1998-12-15 23:30:15 +01:00
|
|
|
GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
|
1998-12-02 15:55:27 +01:00
|
|
|
|
|
|
|
g_free (unix_channel);
|
|
|
|
}
|
|
|
|
|
2000-12-05 21:45:33 +01:00
|
|
|
static GSource *
|
|
|
|
g_io_unix_create_watch (GIOChannel *channel,
|
|
|
|
GIOCondition condition)
|
1998-12-02 15:55:27 +01:00
|
|
|
{
|
1998-12-15 23:30:15 +01:00
|
|
|
GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
|
2000-12-05 21:45:33 +01:00
|
|
|
GSource *source;
|
|
|
|
GIOUnixWatch *watch;
|
|
|
|
|
|
|
|
|
|
|
|
source = g_source_new (&unix_watch_funcs, sizeof (GIOUnixWatch));
|
|
|
|
watch = (GIOUnixWatch *)source;
|
1998-12-02 15:55:27 +01:00
|
|
|
|
|
|
|
watch->channel = channel;
|
|
|
|
g_io_channel_ref (channel);
|
2000-12-05 21:45:33 +01:00
|
|
|
|
1998-12-02 15:55:27 +01:00
|
|
|
watch->condition = condition;
|
|
|
|
|
|
|
|
watch->pollfd.fd = unix_channel->fd;
|
|
|
|
watch->pollfd.events = condition;
|
|
|
|
|
2000-12-05 21:45:33 +01:00
|
|
|
g_source_add_poll (source, &watch->pollfd);
|
1998-12-02 15:55:27 +01:00
|
|
|
|
2000-12-05 21:45:33 +01:00
|
|
|
return source;
|
1998-12-02 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GIOChannel *
|
|
|
|
g_io_channel_unix_new (gint fd)
|
|
|
|
{
|
|
|
|
GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
|
1998-12-15 23:30:15 +01:00
|
|
|
GIOChannel *channel = (GIOChannel *)unix_channel;
|
|
|
|
|
|
|
|
g_io_channel_init (channel);
|
|
|
|
channel->funcs = &unix_channel_funcs;
|
1998-12-02 15:55:27 +01:00
|
|
|
|
|
|
|
unix_channel->fd = fd;
|
1998-12-15 23:30:15 +01:00
|
|
|
return channel;
|
1998-12-02 15:55:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
g_io_channel_unix_get_fd (GIOChannel *channel)
|
|
|
|
{
|
1998-12-16 15:57:42 +01:00
|
|
|
GIOUnixChannel *unix_channel = (GIOUnixChannel *)channel;
|
1998-12-02 15:55:27 +01:00
|
|
|
return unix_channel->fd;
|
|
|
|
}
|