1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18  * Boston, MA 02110-1301, USA.
19  *
20  * Author: Alexander Larsson <alexl@redhat.com>
21  */
22 
23 #include <config.h>
24 
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <sys/un.h>
29 
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gio/gunixfdlist.h>
33 #include "gvfsreadchannel.h"
34 #include "gvfsjobopenforread.h"
35 #include "gvfsdaemonutils.h"
36 
37 G_DEFINE_TYPE (GVfsJobOpenForRead, g_vfs_job_open_for_read, G_VFS_TYPE_JOB_DBUS)
38 
39 static void         run          (GVfsJob        *job);
40 static gboolean     try          (GVfsJob        *job);
41 static void         finished     (GVfsJob        *job);
42 static void         create_reply (GVfsJob               *job,
43                                   GVfsDBusMount         *object,
44                                   GDBusMethodInvocation *invocation);
45 
46 static void
g_vfs_job_open_for_read_finalize(GObject * object)47 g_vfs_job_open_for_read_finalize (GObject *object)
48 {
49   GVfsJobOpenForRead *job;
50 
51   job = G_VFS_JOB_OPEN_FOR_READ (object);
52 
53   /* TODO: manage backend_handle if not put in read channel */
54 
55   if (job->read_channel)
56     g_object_unref (job->read_channel);
57 
58   g_free (job->filename);
59 
60   if (G_OBJECT_CLASS (g_vfs_job_open_for_read_parent_class)->finalize)
61     (*G_OBJECT_CLASS (g_vfs_job_open_for_read_parent_class)->finalize) (object);
62 }
63 
64 static void
g_vfs_job_open_for_read_class_init(GVfsJobOpenForReadClass * klass)65 g_vfs_job_open_for_read_class_init (GVfsJobOpenForReadClass *klass)
66 {
67   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
68   GVfsJobClass *job_class = G_VFS_JOB_CLASS (klass);
69   GVfsJobDBusClass *job_dbus_class = G_VFS_JOB_DBUS_CLASS (klass);
70 
71   gobject_class->finalize = g_vfs_job_open_for_read_finalize;
72   job_class->run = run;
73   job_class->try = try;
74   job_class->finished = finished;
75   job_dbus_class->create_reply = create_reply;
76 }
77 
78 static void
g_vfs_job_open_for_read_init(GVfsJobOpenForRead * job)79 g_vfs_job_open_for_read_init (GVfsJobOpenForRead *job)
80 {
81 }
82 
83 gboolean
g_vfs_job_open_for_read_new_handle(GVfsDBusMount * object,GDBusMethodInvocation * invocation,GUnixFDList * fd_list,const gchar * arg_path_data,guint arg_pid,GVfsBackend * backend)84 g_vfs_job_open_for_read_new_handle (GVfsDBusMount *object,
85                                     GDBusMethodInvocation *invocation,
86                                     GUnixFDList *fd_list,
87                                     const gchar *arg_path_data,
88                                     guint arg_pid,
89                                     GVfsBackend *backend)
90 {
91   GVfsJobOpenForRead *job;
92 
93   if (g_vfs_backend_invocation_first_handler (object, invocation, backend))
94     return TRUE;
95 
96   job = g_object_new (G_VFS_TYPE_JOB_OPEN_FOR_READ,
97                       "object", object,
98                       "invocation", invocation,
99                       NULL);
100 
101   job->filename = g_strdup (arg_path_data);
102   job->backend = backend;
103   job->pid = arg_pid;
104 
105   g_vfs_job_source_new_job (G_VFS_JOB_SOURCE (backend), G_VFS_JOB (job));
106   g_object_unref (job);
107 
108   return TRUE;
109 }
110 
111 static void
run(GVfsJob * job)112 run (GVfsJob *job)
113 {
114   GVfsJobOpenForRead *op_job = G_VFS_JOB_OPEN_FOR_READ (job);
115   GVfsBackendClass *class = G_VFS_BACKEND_GET_CLASS (op_job->backend);
116 
117   if (class->open_for_read == NULL)
118     {
119       g_vfs_job_failed (job, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED,
120 			_("Operation not supported"));
121       return;
122     }
123 
124   class->open_for_read (op_job->backend,
125 			op_job,
126 			op_job->filename);
127 }
128 
129 static gboolean
try(GVfsJob * job)130 try (GVfsJob *job)
131 {
132   GVfsJobOpenForRead *op_job = G_VFS_JOB_OPEN_FOR_READ (job);
133   GVfsBackendClass *class = G_VFS_BACKEND_GET_CLASS (op_job->backend);
134 
135   if (class->try_open_for_read == NULL)
136     return FALSE;
137 
138   return class->try_open_for_read (op_job->backend,
139 				   op_job,
140 				   op_job->filename);
141 }
142 
143 
144 void
g_vfs_job_open_for_read_set_handle(GVfsJobOpenForRead * job,GVfsBackendHandle handle)145 g_vfs_job_open_for_read_set_handle (GVfsJobOpenForRead *job,
146 				    GVfsBackendHandle handle)
147 {
148   job->backend_handle = handle;
149 }
150 
151 void
g_vfs_job_open_for_read_set_can_seek(GVfsJobOpenForRead * job,gboolean can_seek)152 g_vfs_job_open_for_read_set_can_seek (GVfsJobOpenForRead *job,
153 				      gboolean            can_seek)
154 {
155   job->can_seek = can_seek;
156 }
157 
158 /* Might be called on an i/o thread */
159 static void
create_reply(GVfsJob * job,GVfsDBusMount * object,GDBusMethodInvocation * invocation)160 create_reply (GVfsJob *job,
161               GVfsDBusMount *object,
162               GDBusMethodInvocation *invocation)
163 {
164   GVfsJobOpenForRead *open_job = G_VFS_JOB_OPEN_FOR_READ (job);
165   GVfsReadChannel *channel;
166   GError *error;
167   int remote_fd;
168   int fd_id;
169   GUnixFDList *fd_list;
170 
171   g_assert (open_job->backend_handle != NULL);
172 
173   channel = g_vfs_read_channel_new (open_job->backend,
174                                     open_job->pid);
175 
176   remote_fd = g_vfs_channel_steal_remote_fd (G_VFS_CHANNEL (channel));
177   if (remote_fd < 0)
178     {
179       /* expecting we're out of fds when remote_fd == -1 */
180       g_dbus_method_invocation_return_error_literal (invocation,
181                                                      G_IO_ERROR,
182                                                      G_IO_ERROR_TOO_MANY_OPEN_FILES,
183                                                      _("Couldn’t get stream file descriptor"));
184       g_object_unref (channel);
185       return;
186     }
187 
188   fd_list = g_unix_fd_list_new ();
189   error = NULL;
190   fd_id = g_unix_fd_list_append (fd_list, remote_fd, &error);
191   if (fd_id == -1)
192     {
193       g_warning ("create_reply: %s (%s, %d)\n", error->message, g_quark_to_string (error->domain), error->code);
194       g_error_free (error);
195     }
196 
197   g_vfs_channel_set_backend_handle (G_VFS_CHANNEL (channel), open_job->backend_handle);
198   open_job->backend_handle = NULL;
199   open_job->read_channel = channel;
200 
201   g_signal_emit_by_name (job, "new-source", channel);
202 
203   if (open_job->read_icon)
204     gvfs_dbus_mount_complete_open_icon_for_read (object, invocation,
205                                                  fd_list, g_variant_new_handle (fd_id),
206                                                  open_job->can_seek);
207   else
208     gvfs_dbus_mount_complete_open_for_read (object, invocation,
209                                             fd_list, g_variant_new_handle (fd_id),
210                                             open_job->can_seek);
211 
212   /* FIXME: this could cause issues as long as fd_list closes all its fd's when it's finalized */
213   close (remote_fd);
214   g_object_unref (fd_list);
215 }
216 
217 static void
finished(GVfsJob * job)218 finished (GVfsJob *job)
219 {
220 }
221 
222 GPid
g_vfs_job_open_for_read_get_pid(GVfsJobOpenForRead * job)223 g_vfs_job_open_for_read_get_pid (GVfsJobOpenForRead *job)
224 {
225   return job->pid;
226 }
227