Debian Codegraph @

libgphoto2_port/usbdiskdirect/linux.c

raw
1 /* Direct IO to USB Mass storage devices port library for Linux
2 *
3 * Copyright (c) 2010 Hans de Goede <[email protected]>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation; either version 2.1 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301 USA
19 */
20 #define _DEFAULT_SOURCE
21
22 #include "config.h"
23 #include <gphoto2/gphoto2-port-library.h>
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #ifdef HAVE_FCNTL_H
29 # include <fcntl.h>
30 #endif
31 #ifdef HAVE_LIMITS_H
32 # include <limits.h>
33 #endif
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #ifdef HAVE_SYS_PARAM_H
37 # include <sys/param.h>
38 #endif
39 #ifdef HAVE_LOCKDEV
40 # include <lockdev.h>
41 #endif
42
43 #include <gphoto2/gphoto2-port-result.h>
44 #include <gphoto2/gphoto2-port-log.h>
45 #include <gphoto2/gphoto2-port.h>
46
47 #include "libgphoto2_port/i18n.h"
48
49
50 /* From Linux asm/fcntl.h */
51 #ifndef O_DIRECT
52 #define O_DIRECT 00040000 /* direct disk access hint */
53 #endif
54
55 #define CHECK(result) {int r=(result); if (r<0) return (r);}
56
57 struct _GPPortPrivateLibrary {
58 int fd; /* Device handle */
59 };
60
61 GPPortType
62 gp_port_library_type ()
63 {
64 return GP_PORT_USB_DISK_DIRECT;
65 }
66
67 static int
68 gp_port_usbdiskdirect_lock (GPPort *port, const char *path)
69 {
70 #ifdef HAVE_LOCKDEV
71 int pid;
72
73 GP_LOG_D ("Trying to lock '%s'...", path);
74
75 pid = dev_lock (path);
76 if (pid) {
77 if (port) {
78 if (pid > 0)
79 gp_port_set_error (port, _("Device '%s' is "
80 "locked by pid %d"), path, pid);
81 else
82 gp_port_set_error (port, _("Device '%s' could "
83 "not be locked (dev_lock returned "
84 "%d)"), path, pid);
85 }
86 return GP_ERROR_IO_LOCK;
87 }
88 #else
89 # ifdef __GCC__
90 # warning No locking library found.
91 # warning You will run into problems if you use
92 # warning gphoto2 with a usbdiskdirect picframe in
93 # warning combination with Konqueror (KDE) or Nautilus (GNOME).
94 # warning This will *not* concern USB cameras.
95 # endif
96 #endif
97
98 return GP_OK;
99 }
100
101 static int
102 gp_port_usbdiskdirect_unlock (GPPort *port, const char *path)
103 {
104 #ifdef HAVE_LOCKDEV
105 int pid;
106
107 pid = dev_unlock (path, 0);
108 if (pid) {
109 if (port) {
110 if (pid > 0)
111 gp_port_set_error (port, _("Device '%s' could "
112 "not be unlocked as it is locked by "
113 "pid %d."), path, pid);
114 else
115 gp_port_set_error (port, _("Device '%s' could "
116 "not be unlocked (dev_unlock "
117 "returned %d)"), path, pid);
118 }
119 return GP_ERROR_IO_LOCK;
120 }
121 #endif /* !HAVE_LOCKDEV */
122
123 return GP_OK;
124 }
125
126 static const char *
127 gp_port_usbdiskdirect_resolve_symlink (const char *link)
128 {
129 ssize_t ret;
130 static char path[PATH_MAX + 1];
131 char *slash, buf[PATH_MAX + 1];
132 struct stat st;
133 int len;
134
135 snprintf (path, sizeof(path), "%s", link);
136
137 do {
138 ret = readlink (path, buf, PATH_MAX);
139 if (ret < 0)
140 return NULL;
141 buf[ret] = 0;
142
143 slash = strrchr (path, '/');
144 if (buf[0] == '/' || slash == NULL) {
145 snprintf (path, sizeof(path), "%s", buf);
146 } else {
147 *slash = 0;
148 len = strlen (path);
149 if (snprintf (path + len, sizeof (path) - len, "/%s", buf) >= (int)sizeof (path) - len)
150 return NULL;
151 }
152
153 if (stat (path, &st))
154 return NULL;
155 } while (S_ISLNK(st.st_mode));
156
157 return path;
158 }
159
160 static int
161 gp_port_usbdiskdirect_get_usb_id (const char *disk,
162 unsigned short *vendor_id, unsigned short *product_id)
163 {
164 FILE *f;
165 char c, *s, buf[32], path[PATH_MAX + 1];
166
167 snprintf (path, sizeof (path), "/sys/block/%s", disk);
168 snprintf (path, sizeof (path), "%s/../../../../../modalias",
169 gp_port_usbdiskdirect_resolve_symlink(path));
170
171 f = fopen (path, "r");
172 if (!f)
173 return GP_ERROR_IO_SUPPORTED_USB;
174
175 s = fgets (buf, sizeof(buf), f);
176 fclose (f);
177
178 if (!s)
179 return GP_ERROR_IO_SUPPORTED_USB;
180
181 if (sscanf (s, "usb:v%4hxp%4hx%c", vendor_id, product_id, &c) != 3 ||
182 c != 'd')
183 return GP_ERROR_IO_SUPPORTED_USB;
184
185 return GP_OK;
186 }
187
188 int
189 gp_port_library_list (GPPortInfoList *list)
190 {
191 gp_system_dir dir;
192 gp_system_dirent dirent;
193 GPPortInfo info;
194 int ret;
195 unsigned short vendor_id, product_id;
196
197 dir = gp_system_opendir ("/sys/block");
198 if (dir == NULL)
199 return GP_OK;
200
201 while ((dirent = gp_system_readdir (dir))) {
202 char path[4096];
203 if (dirent->d_name[0] != 's' ||
204 dirent->d_name[1] != 'd' ||
205 dirent->d_name[2] < 'a' ||
206 dirent->d_name[2] > 'z')
207 continue;
208
209 if (gp_port_usbdiskdirect_get_usb_id (dirent->d_name,
210 &vendor_id, &product_id) != GP_OK)
211 continue; /* Not a usb device */
212
213 gp_port_info_new (&info);
214 gp_port_info_set_type (info, GP_PORT_USB_DISK_DIRECT);
215 snprintf (path, sizeof (path),
216 "usbdiskdirect:/dev/%s",
217 dirent->d_name);
218 gp_port_info_set_path (info, path);
219 gp_port_info_set_name (info, _("USB Mass Storage direct IO"));
220 ret = gp_port_info_list_append (list, info);
221 if (ret < GP_OK)
222 break;
223 }
224 gp_system_closedir (dir);
225 return GP_OK;
226 }
227
228 static int
229 gp_port_usbdiskdirect_init (GPPort *port)
230 {
231 C_MEM (port->pl = calloc (1, sizeof (GPPortPrivateLibrary)));
232
233 port->pl->fd = -1;
234
235 return GP_OK;
236 }
237
238 static int
239 gp_port_usbdiskdirect_exit (GPPort *port)
240 {
241 C_PARAMS (port);
242
243 free (port->pl);
244 port->pl = NULL;
245
246 return GP_OK;
247 }
248
249 static int
250 gp_port_usbdiskdirect_open (GPPort *port)
251 {
252 int result, i;
253 const int max_tries = 5;
254 const char *path = port->settings.usbdiskdirect.path;
255
256 result = gp_port_usbdiskdirect_lock (port, path);
257 if (result != GP_OK) {
258 for (i = 0; i < max_tries; i++) {
259 result = gp_port_usbdiskdirect_lock (port, path);
260 if (result == GP_OK)
261 break;
262 GP_LOG_D ("Failed to get a lock, trying again...");
263 sleep (1);
264 }
265 CHECK (result)
266 }
267 port->pl->fd = open (path, O_RDWR|O_DIRECT|O_SYNC);
268 if (port->pl->fd == -1) {
269 gp_port_usbdiskdirect_unlock (port, path);
270 gp_port_set_error (port, _("Failed to open '%s' (%m)."), path);
271 return GP_ERROR_IO;
272 }
273
274 return GP_OK;
275 }
276
277 static int
278 gp_port_usbdiskdirect_close (GPPort *port)
279 {
280 if (!port || port->pl->fd == -1)
281 return GP_OK;
282
283 if (close (port->pl->fd) == -1) {
284 gp_port_set_error (port, _("Could not close "
285 "'%s' (%m)."), port->settings.usbdiskdirect.path);
286 return GP_ERROR_IO;
287 }
288 port->pl->fd = -1;
289
290 CHECK (gp_port_usbdiskdirect_unlock (port,
291 port->settings.usbdiskdirect.path))
292
293 return GP_OK;
294 }
295
296 static int gp_port_usbdiskdirect_seek (GPPort *port, int offset, int whence)
297 {
298 off_t ret;
299
300 C_PARAMS (port);
301
302 /* The device needs to be opened for that operation */
303 if (port->pl->fd == -1)
304 CHECK (gp_port_usbdiskdirect_open (port))
305
306 ret = lseek (port->pl->fd, offset, whence);
307 if (ret == -1) {
308 gp_port_set_error (port, _("Could not seek to offset: %x on "
309 "'%s' (%m)."), offset,
310 port->settings.usbdiskdirect.path);
311 return GP_ERROR_IO;
312 }
313
314 return ret;
315 }
316
317 static int
318 gp_port_usbdiskdirect_write (GPPort *port, const char *bytes, int size)
319 {
320 int ret;
321
322 C_PARAMS (port);
323
324 /* The device needs to be opened for that operation */
325 if (port->pl->fd == -1)
326 CHECK (gp_port_usbdiskdirect_open (port))
327
328 ret = write (port->pl->fd, bytes, size);
329 if (ret < 0) {
330 gp_port_set_error (port, _("Could not write to '%s' (%m)."),
331 port->settings.usbdiskdirect.path);
332 return GP_ERROR_IO;
333 }
334
335 return ret;
336 }
337
338 static int
339 gp_port_usbdiskdirect_read (GPPort *port, char *bytes, int size)
340 {
341 int ret;
342
343 C_PARAMS (port);
344
345 /* The device needs to be opened for that operation */
346 if (port->pl->fd == -1)
347 CHECK (gp_port_usbdiskdirect_open (port))
348
349 ret = read (port->pl->fd, bytes, size);
350 if (ret < 0) {
351 gp_port_set_error (port, _("Could not read from '%s' (%m)."),
352 port->settings.usbdiskdirect.path);
353 return GP_ERROR_IO;
354 }
355
356 return ret;
357 }
358
359 static int
360 gp_port_usbdiskdirect_update (GPPort *port)
361 {
362 C_PARAMS (port);
363
364 memcpy (&port->settings, &port->settings_pending,
365 sizeof (port->settings));
366
367 return GP_OK;
368 }
369
370 static int
371 gp_port_usbdiskdirect_find_device(GPPort *port, int idvendor, int idproduct)
372 {
373 unsigned short vendor_id, product_id;
374 const char *disk;
375
376 C_PARAMS (port);
377
378 disk = strrchr (port->settings.usbdiskdirect.path, '/');
379 C_PARAMS (disk);
380 disk++;
381
382 CHECK (gp_port_usbdiskdirect_get_usb_id (disk,
383 &vendor_id, &product_id))
384 if (vendor_id != idvendor || product_id != idproduct)
385 return GP_ERROR_IO_USB_FIND;
386
387 return GP_OK;
388 }
389
390 GPPortOperations *
391 gp_port_library_operations ()
392 {
393 GPPortOperations *ops;
394
395 ops = calloc (1, sizeof (GPPortOperations));
396 if (!ops)
397 return (NULL);
398
399 ops->init = gp_port_usbdiskdirect_init;
400 ops->exit = gp_port_usbdiskdirect_exit;
401 ops->open = gp_port_usbdiskdirect_open;
402 ops->close = gp_port_usbdiskdirect_close;
403 ops->seek = gp_port_usbdiskdirect_seek;
404 ops->read = gp_port_usbdiskdirect_read;
405 ops->write = gp_port_usbdiskdirect_write;
406 ops->update = gp_port_usbdiskdirect_update;
407 ops->find_device = gp_port_usbdiskdirect_find_device;
408
409 return (ops);
410 }