diff options
author | des <des@FreeBSD.org> | 2009-01-30 13:54:03 +0000 |
---|---|---|
committer | des <des@FreeBSD.org> | 2009-01-30 13:54:03 +0000 |
commit | 604db9f61a0640dd95d3f93b517db0e54b17f090 (patch) | |
tree | f6bcaa578ebaec30009182705f8c54ba76d1e4c0 /lib | |
parent | c5677e5d7ad2e7b7122f55bc89be13fcb3f7128d (diff) | |
download | FreeBSD-src-604db9f61a0640dd95d3f93b517db0e54b17f090.zip FreeBSD-src-604db9f61a0640dd95d3f93b517db0e54b17f090.tar.gz |
WIP
Diffstat (limited to 'lib')
-rw-r--r-- | lib/libutil/Makefile | 10 | ||||
-rw-r--r-- | lib/libutil/libutil.h | 9 | ||||
-rw-r--r-- | lib/libutil/quotafile.3 | 69 | ||||
-rw-r--r-- | lib/libutil/quotafile.c | 272 |
4 files changed, 357 insertions, 3 deletions
diff --git a/lib/libutil/Makefile b/lib/libutil/Makefile index 1ea4c73..178948d 100644 --- a/lib/libutil/Makefile +++ b/lib/libutil/Makefile @@ -12,8 +12,8 @@ SRCS= _secure_path.c auth.c expand_number.c flopen.c fparseln.c gr_util.c \ hexdump.c humanize_number.c kinfo_getfile.c kinfo_getvmmap.c kld.c \ login.c login_auth.c login_cap.c \ login_class.c login_crypt.c login_ok.c login_times.c login_tty.c \ - logout.c logwtmp.c pidfile.c property.c pty.c pw_util.c realhostname.c \ - stub.c trimdomain.c uucplock.c + logout.c logwtmp.c pidfile.c property.c pty.c pw_util.c quotafile.c \ + realhostname.c stub.c trimdomain.c uucplock.c INCS= libutil.h login_cap.h WARNS?= 6 @@ -30,7 +30,7 @@ MAN+= kld.3 login.3 login_auth.3 login_tty.3 logout.3 logwtmp.3 pty.3 \ login_cap.3 login_class.3 login_times.3 login_ok.3 \ _secure_path.3 uucplock.3 property.3 auth.3 realhostname.3 \ realhostname_sa.3 trimdomain.3 fparseln.3 humanize_number.3 \ - pidfile.3 flopen.3 expand_number.3 + pidfile.3 flopen.3 expand_number.3 quotafile.3 MAN+= login.conf.5 auth.conf.5 MLINKS+= kld.3 kld_isloaded.3 kld.3 kld_load.3 MLINKS+= property.3 properties_read.3 property.3 properties_free.3 @@ -58,5 +58,9 @@ MLINKS+=pidfile.3 pidfile_open.3 \ pidfile.3 pidfile_write.3 \ pidfile.3 pidfile_close.3 \ pidfile.3 pidfile_remove.3 +MLINKS+=quotafile.3 quota_open.3 \ + quotafile.3 quota_read.3 \ + quotafile.3 quota_write.3 \ + quotafile.3 quota_close.3 .include <bsd.lib.mk> diff --git a/lib/libutil/libutil.h b/lib/libutil/libutil.h index 3187fb3..48ab218 100644 --- a/lib/libutil/libutil.h +++ b/lib/libutil/libutil.h @@ -140,6 +140,15 @@ int pidfile_close(struct pidfh *pfh); int pidfile_remove(struct pidfh *pfh); #endif +#ifdef _UFS_UFS_QUOTA_H_ +struct quotafile; +struct quotafile *quota_open(const char *); +struct quotafile *quota_create(const char *); +void quota_close(struct quotafile *); +int quota_read(struct quotafile *, struct dqblk *, int); +int quota_write(struct quotafile *, const struct dqblk *, int); +#endif + __END_DECLS #define UU_LOCK_INUSE (1) diff --git a/lib/libutil/quotafile.3 b/lib/libutil/quotafile.3 new file mode 100644 index 0000000..a414a99 --- /dev/null +++ b/lib/libutil/quotafile.3 @@ -0,0 +1,69 @@ +.\"- +.\" Copyright (c) 2008 Dag-Erling Coïdan Smørgrav +.\" All rights reserved. +.\" +.\" Redistribution and use in source and binary forms, with or without +.\" modification, are permitted provided that the following conditions +.\" are met: +.\" 1. Redistributions of source code must retain the above copyright +.\" notice, this list of conditions and the following disclaimer. +.\" 2. Redistributions in binary form must reproduce the above copyright +.\" notice, this list of conditions and the following disclaimer in the +.\" documentation and/or other materials provided with the distribution. +.\" +.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND +.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +.\" ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE +.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS +.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) +.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY +.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF +.\" SUCH DAMAGE. +.\" +.\" $FreeBSD$ +.\" +.Dd November 4, 2008 +.Dt QUOTAFILE 3 +.Os +.Sh NAME +.Nm quota_open +.Nm quota_create +.Nm quota_read +.Nm quota_write +.Nm quota_close +.Nd "Manipulate quota files" +.Sh LIBRARY +.Lb libutil +.Sh SYNOPSIS +.In ufs/ufs/quota.h +.In libutil.h +.Ft "struct quotafile *" +.Fn quota_open "const char *path" +.Ft "struct quotafile *" +.Fn quota_create "const char *path" +.Ft int +.Fn quota_read "struct quotafile *qf" "struct dqblk *dqb" "int type" +.Ft int +.Fn quota_write "struct quotafile *qf" "const struct dqblk *dqb" "int type" +.Ft int +.Fn quota_close "struct quotafile *qf" +.Sh DESCRIPTION +.Sh RETURN VALUES +.Sh SEE ALSO +.Xr quotactl 2 , +.Xr quota.user 5 , +.Xr quota.group 5 +.Sh HISTORY +The +.Nm +functions first appeared in +.Fx 8.0 . +.Sh AUTHORS +.An -nosplit +The +.Nm +functions and this manual page were written by +.An Dag-Erling Sm\(/orgrav Aq des@FreeBSD.org . diff --git a/lib/libutil/quotafile.c b/lib/libutil/quotafile.c new file mode 100644 index 0000000..a4258b1 --- /dev/null +++ b/lib/libutil/quotafile.c @@ -0,0 +1,272 @@ +/*- + * Copyright (c) 2008 Dag-Erling Coïdan Smørgrav + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer + * in this position and unchanged. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * $FreeBSD$ + */ + +#include <sys/types.h> +#include <sys/endian.h> +#include <sys/stat.h> + +#include <ufs/ufs/quota.h> + +#include <errno.h> +#include <fcntl.h> +#include <grp.h> +#include <pwd.h> +#include <libutil.h> +#include <stdint.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +struct quotafile { + int fd; + int type; /* 32 or 64 */ +}; + +struct quotafile * +quota_open(const char *fn) +{ + struct quotafile *qf; + struct dqhdr64 dqh; + int serrno; + + if ((qf = calloc(1, sizeof(*qf))) == NULL) + return (NULL); + if ((qf->fd = open(fn, O_RDWR)) < 0) { + serrno = errno; + free(qf); + errno = serrno; + return (NULL); + } + qf->type = 32; + switch (read(qf->fd, &dqh, sizeof(dqh))) { + case -1: + serrno = errno; + close(qf->fd); + free(qf); + errno = serrno; + return (NULL); + case sizeof(dqh): + if (strcmp(dqh.dqh_magic, Q_DQHDR64_MAGIC) != 0) { + /* no magic, assume 32 bits */ + qf->type = 32; + return (qf); + } + if (be32toh(dqh.dqh_version) != Q_DQHDR64_VERSION || + be32toh(dqh.dqh_hdrlen) != sizeof(struct dqhdr64) || + be32toh(dqh.dqh_reclen) != sizeof(struct dqblk64)) { + /* correct magic, wrong version / lengths */ + close(qf->fd); + free(qf); + errno = EINVAL; + return (NULL); + } + qf->type = 64; + return (qf); + default: + qf->type = 32; + return (qf); + } + /* not reached */ +} + +struct quotafile * +quota_create(const char *fn) +{ + struct quotafile *qf; + struct dqhdr64 dqh; + struct group *grp; + int serrno; + + if ((qf = calloc(1, sizeof(*qf))) == NULL) + return (NULL); + if ((qf->fd = open(fn, O_RDWR|O_CREAT|O_TRUNC, 0)) < 0) { + serrno = errno; + free(qf); + errno = serrno; + return (NULL); + } + qf->type = 64; + memset(&dqh, 0, sizeof(dqh)); + memcpy(dqh.dqh_magic, Q_DQHDR64_MAGIC, sizeof(dqh.dqh_magic)); + dqh.dqh_version = htobe32(Q_DQHDR64_VERSION); + dqh.dqh_hdrlen = htobe32(sizeof(struct dqhdr64)); + dqh.dqh_reclen = htobe32(sizeof(struct dqblk64)); + if (write(qf->fd, &dqh, sizeof(dqh)) != sizeof(dqh)) { + serrno = errno; + unlink(fn); + close(qf->fd); + free(qf); + errno = serrno; + return (NULL); + } + grp = getgrnam(QUOTAGROUP); + fchown(qf->fd, 0, grp ? grp->gr_gid : 0); + fchmod(qf->fd, 0640); + return (qf); +} + +void +quota_close(struct quotafile *qf) +{ + + close(qf->fd); + free(qf); +} + +static int +quota_read32(struct quotafile *qf, struct dqblk *dqb, int id) +{ + struct dqblk32 dqb32; + off_t off; + + off = id * sizeof(struct dqblk32); + if (lseek(qf->fd, off, SEEK_SET) == -1) + return (-1); + switch (read(qf->fd, &dqb32, sizeof(dqb32))) { + case 0: + memset(&dqb, 0, sizeof(*dqb)); + return (0); + case sizeof(dqb32): + dqb->dqb_bhardlimit = dqb32.dqb_bhardlimit; + dqb->dqb_bsoftlimit = dqb32.dqb_bsoftlimit; + dqb->dqb_curblocks = dqb32.dqb_curblocks; + dqb->dqb_ihardlimit = dqb32.dqb_ihardlimit; + dqb->dqb_isoftlimit = dqb32.dqb_isoftlimit; + dqb->dqb_curinodes = dqb32.dqb_curinodes; + dqb->dqb_btime = dqb32.dqb_btime; + dqb->dqb_itime = dqb32.dqb_itime; + return (0); + default: + return (-1); + } +} + +static int +quota_read64(struct quotafile *qf, struct dqblk *dqb, int id) +{ + struct dqblk64 dqb64; + off_t off; + + off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64); + if (lseek(qf->fd, off, SEEK_SET) == -1) + return (-1); + switch (read(qf->fd, &dqb64, sizeof(dqb64))) { + case 0: + memset(&dqb, 0, sizeof(*dqb)); + return (0); + case sizeof(dqb64): + dqb->dqb_bhardlimit = be64toh(dqb64.dqb_bhardlimit); + dqb->dqb_bsoftlimit = be64toh(dqb64.dqb_bsoftlimit); + dqb->dqb_curblocks = be64toh(dqb64.dqb_curblocks); + dqb->dqb_ihardlimit = be64toh(dqb64.dqb_ihardlimit); + dqb->dqb_isoftlimit = be64toh(dqb64.dqb_isoftlimit); + dqb->dqb_curinodes = be64toh(dqb64.dqb_curinodes); + dqb->dqb_btime = be64toh(dqb64.dqb_btime); + dqb->dqb_itime = be64toh(dqb64.dqb_itime); + return (0); + default: + return (-1); + } +} + +int +quota_read(struct quotafile *qf, struct dqblk *dqb, int id) +{ + + switch (qf->type) { + case 32: + return quota_read32(qf, dqb, id); + case 64: + return quota_read64(qf, dqb, id); + default: + errno = EINVAL; + return (-1); + } + /* not reached */ +} + +#define CLIP32(u64) ((u64) > UINT32_MAX ? UINT32_MAX : (uint32_t)(u64)) + +static int +quota_write32(struct quotafile *qf, const struct dqblk *dqb, int id) +{ + struct dqblk32 dqb32; + off_t off; + + dqb32.dqb_bhardlimit = CLIP32(dqb->dqb_bhardlimit); + dqb32.dqb_bsoftlimit = CLIP32(dqb->dqb_bsoftlimit); + dqb32.dqb_curblocks = CLIP32(dqb->dqb_curblocks); + dqb32.dqb_ihardlimit = CLIP32(dqb->dqb_ihardlimit); + dqb32.dqb_isoftlimit = CLIP32(dqb->dqb_isoftlimit); + dqb32.dqb_curinodes = CLIP32(dqb->dqb_curinodes); + dqb32.dqb_btime = CLIP32(dqb->dqb_btime); + dqb32.dqb_itime = CLIP32(dqb->dqb_itime); + + off = id * sizeof(struct dqblk32); + if (lseek(qf->fd, off, SEEK_SET) == -1) + return (-1); + return (write(qf->fd, &dqb32, sizeof(dqb32))); +} + +static int +quota_write64(struct quotafile *qf, const struct dqblk *dqb, int id) +{ + struct dqblk64 dqb64; + off_t off; + + dqb64.dqb_bhardlimit = htobe64(dqb->dqb_bhardlimit); + dqb64.dqb_bsoftlimit = htobe64(dqb->dqb_bsoftlimit); + dqb64.dqb_curblocks = htobe64(dqb->dqb_curblocks); + dqb64.dqb_ihardlimit = htobe64(dqb->dqb_ihardlimit); + dqb64.dqb_isoftlimit = htobe64(dqb->dqb_isoftlimit); + dqb64.dqb_curinodes = htobe64(dqb->dqb_curinodes); + dqb64.dqb_btime = htobe64(dqb->dqb_btime); + dqb64.dqb_itime = htobe64(dqb->dqb_itime); + + off = sizeof(struct dqhdr64) + id * sizeof(struct dqblk64); + if (lseek(qf->fd, off, SEEK_SET) == -1) + return (-1); + return (write(qf->fd, &dqb64, sizeof(dqb64))); +} + +int +quota_write(struct quotafile *qf, const struct dqblk *dqb, int id) +{ + + switch (qf->type) { + case 32: + return quota_write32(qf, dqb, id); + case 64: + return quota_write64(qf, dqb, id); + default: + errno = EINVAL; + return (-1); + } + /* not reached */ +} |