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
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
23 #include <sys/types.h>
24 #include <sys/mount.h>
25 #include <libhal-storage.h>
31 static int pusb_volume_mount(t_pusb_options
*opts
, LibHalVolume
**volume
,
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");
46 fs
= libhal_volume_get_fstype(*volume
);
47 log_debug("Attempting to mount device %s with label %s\n",
50 snprintf(command
, sizeof(command
), "pmount -A -s %s %s",
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");
61 udi
= libhal_volume_get_udi(*volume
);
64 log_error("Unable to retrieve volume UDI\n");
68 libhal_volume_free(*volume
);
69 *volume
= libhal_volume_from_udi(ctx
, udi
);
71 log_debug("Mount succeeded.\n");
75 static LibHalVolume
*pusb_volume_probe(t_pusb_options
*opts
,
78 LibHalVolume
*volume
= NULL
;
82 if (!*(opts
->device
.volume_uuid
))
84 log_debug("No UUID configured for device\n");
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
)
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
,
103 volume
= libhal_volume_from_udi(ctx
, udi
);
104 libhal_free_string(udi
);
105 if (!libhal_volume_should_ignore(volume
))
107 libhal_volume_free(volume
);
113 LibHalVolume
*pusb_volume_get(t_pusb_options
*opts
, LibHalContext
*ctx
)
115 LibHalVolume
*volume
;
117 if (!(volume
= pusb_volume_probe(opts
, ctx
)))
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");
125 if (!pusb_volume_mount(opts
, &volume
, ctx
))
127 libhal_volume_free(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"))
142 log_debug("Attempting to umount %s\n",
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");
149 log_error("Unable to umount %s\n", mntpoint
);
151 libhal_volume_free(volume
);