void-packages/xbps-src/libexec/umount.c
Juan RP e3dc3e3066 Added native utilities to mount/umount/chroot via capabilities(7).
Three new helpers will now be installed into ${libexecdir}:
 - xbps-src-capchroot needs to have set CAP_SYS_CHROOT ep.
 - xbps-src-chroot-cap{,u}mount: needs to have set CAP_SYS_ADMIN ep.

That means that libcap and setcap(8) are now required to install
xbps-src and use it as normal user.

--HG--
extra : convert_revision : 586d6526079e085f86bf3e393459d429f6f0ef99
2010-04-26 14:15:49 +02:00

59 lines
1.2 KiB
C

/*
* Umounts a previously bind mounted filesystem mountpoint,
* by using the CAP_SYS_ADMIN capability set on the file.
*
* Juan RP - 2010/04/26 - Public Domain.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <sys/mount.h>
#include <sys/capability.h>
void
usage(void)
{
fprintf(stderr, "Usage: xbps-src-capbumount <dest>\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char **argv)
{
cap_t cap;
cap_flag_value_t effective, permitted;
int rv;
if (argc != 2)
usage();
cap = cap_get_proc();
if (cap == NULL) {
fprintf(stderr, "cap_get_proc() returned %s!\n",
strerror(errno));
exit(EXIT_FAILURE);
}
cap_get_flag(cap, CAP_SYS_ADMIN, CAP_EFFECTIVE, &effective);
cap_get_flag(cap, CAP_SYS_ADMIN, CAP_PERMITTED, &permitted);
if ((effective != CAP_SET) && (permitted != CAP_SET)) {
fprintf(stderr, "E: missing 'cap_sys_admin' capability!\n"
"Please set it with: setcap cap_sys_admin=ep %s'\n",
argv[0]);
cap_free(cap);
exit(EXIT_FAILURE);
}
cap_free(cap);
if ((rv = umount(argv[1])) != 0) {
fprintf(stderr, "E: cannot umount %s: %s\n", argv[0],
strerror(errno));
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}