Port pamusb-agent and pamusb-conf to UDisks.
[pam_usb.git] / src / volume.c
blobb2b16a32739c926aa51e97aa8bd487dc5b797f0d
1 /*
2 * Copyright (c) 2003-2007 Andrea Luzzardi <scox@sig11.org>
4 * This file is part of the pam_usb project. pam_usb is free software;
5 * you can redistribute it and/or modify it under the terms of the GNU General
6 * Public License version 2, as published by the Free Software Foundation.
8 * pam_usb is distributed in the hope that it will be useful, but WITHOUT ANY
9 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 * details.
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
15 * Place, Suite 330, Boston, MA 02111-1307 USA
18 #include <stdio.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <unistd.h>
22 #include <stdlib.h>
23 #include <sys/types.h>
24 #include <sys/mount.h>
25 #include <libhal-storage.h>
26 #include "conf.h"
27 #include "log.h"
28 #include "hal.h"
29 #include "volume.h"
31 static int pusb_volume_mount(t_pusb_options *opts, LibHalVolume **volume,
32 LibHalContext *ctx)
34 char command[1024];
35 char tempname[32];
36 const char *devname;
37 const char *udi;
38 const char *fs;
40 snprintf(tempname, sizeof(tempname), "pam_usb%d", getpid());
41 if (!(devname = libhal_volume_get_device_file(*volume)))
43 log_error("Unable to retrieve device filename\n");
44 return (0);
46 fs = libhal_volume_get_fstype(*volume);
47 log_debug("Attempting to mount device %s with label %s\n",
48 devname, tempname);
49 if (!fs)
50 snprintf(command, sizeof(command), "pmount -A -s %s %s",
51 devname, tempname);
52 else
53 snprintf(command, sizeof(command), "pmount -A -s -t %s %s %s",
54 fs, devname, tempname);
55 log_debug("Executing \"%s\"\n", command);
56 if (system(command) != 0)
58 log_error("Mount failed\n");
59 return (0);
61 udi = libhal_volume_get_udi(*volume);
62 if (!udi)
64 log_error("Unable to retrieve volume UDI\n");
65 return (0);
67 udi = strdup(udi);
68 libhal_volume_free(*volume);
69 *volume = libhal_volume_from_udi(ctx, udi);
70 free((char *)udi);
71 log_debug("Mount succeeded.\n");
72 return (1);
75 static LibHalVolume *pusb_volume_probe(t_pusb_options *opts,
76 LibHalContext *ctx)
78 LibHalVolume *volume = NULL;
79 int maxtries = 0;
80 int i;
82 if (!*(opts->device.volume_uuid))
84 log_debug("No UUID configured for device\n");
85 return (NULL);
87 log_debug("Searching for volume with uuid %s\n", opts->device.volume_uuid);
88 maxtries = ((opts->probe_timeout * 1000000) / 250000);
89 for (i = 0; i < maxtries; ++i)
91 char *udi = NULL;
93 if (i == 1)
94 log_info("Probing volume (this could take a while)...\n");
95 udi = pusb_hal_find_item(ctx,
96 "volume.uuid", opts->device.volume_uuid,
97 NULL);
98 if (!udi)
100 usleep(250000);
101 continue;
103 volume = libhal_volume_from_udi(ctx, udi);
104 libhal_free_string(udi);
105 if (!libhal_volume_should_ignore(volume))
106 return (volume);
107 libhal_volume_free(volume);
108 usleep(250000);
110 return (NULL);
113 LibHalVolume *pusb_volume_get(t_pusb_options *opts, LibHalContext *ctx)
115 LibHalVolume *volume;
117 if (!(volume = pusb_volume_probe(opts, ctx)))
118 return (NULL);
119 log_debug("Found volume %s\n", opts->device.volume_uuid);
120 if (libhal_volume_is_mounted(volume))
122 log_debug("Volume is already mounted.\n");
123 return (volume);
125 if (!pusb_volume_mount(opts, &volume, ctx))
127 libhal_volume_free(volume);
128 return (NULL);
130 return (volume);
133 void pusb_volume_destroy(LibHalVolume *volume)
135 const char *mntpoint;
137 mntpoint = libhal_volume_get_mount_point(volume);
138 if (mntpoint && strstr(mntpoint, "pam_usb"))
140 char command[1024];
142 log_debug("Attempting to umount %s\n",
143 mntpoint);
144 snprintf(command, sizeof(command), "pumount %s", mntpoint);
145 log_debug("Executing \"%s\"\n", command);
146 if (!system(command))
147 log_debug("Umount succeeded.\n");
148 else
149 log_error("Unable to umount %s\n", mntpoint);
151 libhal_volume_free(volume);