Home | History | Annotate | Line # | Download | only in mount_sysctlfs
sysctlfs.c revision 1.3
      1 /*	$NetBSD: sysctlfs.c,v 1.3 2007/08/10 08:13:11 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006, 2007  Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 /*
     29  * sysctlfs: mount sysctls as a file system tree.  Supports query and
     30  * modify of nodes in the sysctl namespace in addition to namespace
     31  * traversal.
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 #ifndef lint
     36 __RCSID("$NetBSD: sysctlfs.c,v 1.3 2007/08/10 08:13:11 pooka Exp $");
     37 #endif /* !lint */
     38 
     39 #include <sys/types.h>
     40 #include <sys/sysctl.h>
     41 
     42 #include <assert.h>
     43 #include <err.h>
     44 #include <errno.h>
     45 #include <mntopts.h>
     46 #include <paths.h>
     47 #include <puffs.h>
     48 #include <stdlib.h>
     49 #include <string.h>
     50 #include <unistd.h>
     51 #include <util.h>
     52 
     53 PUFFSOP_PROTOS(sysctlfs)
     54 
     55 struct sfsnode {
     56 	int sysctl_flags;
     57 	ino_t myid;
     58 };
     59 
     60 #define SFSPATH_DOTDOT 0
     61 #define SFSPATH_NORMAL 1
     62 
     63 #define N_HIERARCHY 10
     64 typedef int SfsName[N_HIERARCHY];
     65 
     66 struct sfsfid {
     67 	int len;
     68 	SfsName path;
     69 };
     70 
     71 static struct sfsnode rn;
     72 static SfsName sname_root;
     73 static struct timespec fstime;
     74 
     75 static ino_t nextid = 3;
     76 static mode_t fileperms;
     77 static uid_t fileuid;
     78 static gid_t filegid;
     79 
     80 #define ISADIR(a) ((SYSCTL_TYPE(a->sysctl_flags) == CTLTYPE_NODE))
     81 #define SFS_MAXFILE 8192
     82 #define SFS_NODEPERDIR 128
     83 
     84 static int sysctlfs_domount(struct puffs_usermount *);
     85 
     86 /*
     87  * build paths.  doesn't support rename (but neither does the fs)
     88  */
     89 static int
     90 sysctlfs_pathbuild(struct puffs_usermount *pu,
     91 	const struct puffs_pathobj *parent, const struct puffs_pathobj *comp,
     92 	size_t offset, struct puffs_pathobj *res)
     93 {
     94 	SfsName *sname;
     95 	size_t clen;
     96 
     97 	assert(parent->po_len < N_HIERARCHY); /* code uses +1 */
     98 
     99 	sname = malloc(sizeof(SfsName));
    100 	assert(sname != NULL);
    101 
    102 	clen = parent->po_len;
    103 	if (comp->po_len == SFSPATH_DOTDOT) {
    104 		assert(clen != 0);
    105 		clen--;
    106 	}
    107 
    108 	memcpy(sname, parent->po_path, clen * sizeof(int));
    109 
    110 	res->po_path = sname;
    111 	res->po_len = clen;
    112 
    113 	return 0;
    114 }
    115 
    116 static int
    117 sysctlfs_pathtransform(struct puffs_usermount *pu,
    118 	const struct puffs_pathobj *p, const const struct puffs_cn *pcn,
    119 	struct puffs_pathobj *res)
    120 {
    121 
    122 	res->po_path = NULL;
    123 	/*
    124 	 * XXX: overload.  prevents us from doing rename, but the fs
    125 	 * (and sysctl(3)) doesn't support it, so no biggie
    126 	 */
    127 	if (PCNISDOTDOT(pcn)) {
    128 		res->po_len = SFSPATH_DOTDOT;
    129 	}else {
    130 		res->po_len = SFSPATH_NORMAL;
    131 	}
    132 
    133 	return 0;
    134 }
    135 
    136 static int
    137 sysctlfs_pathcmp(struct puffs_usermount *pu, struct puffs_pathobj *po1,
    138 	struct puffs_pathobj *po2, size_t clen, int checkprefix)
    139 {
    140 
    141 	if (memcmp(po1->po_path, po2->po_path, clen * sizeof(int)) == 0)
    142 		return 0;
    143 	return 1;
    144 }
    145 
    146 static void
    147 sysctlfs_pathfree(struct puffs_usermount *pu, struct puffs_pathobj *po)
    148 {
    149 
    150 	free(po->po_path);
    151 }
    152 
    153 static struct puffs_node *
    154 getnode(struct puffs_usermount *pu, struct puffs_pathobj *po, int nodetype)
    155 {
    156 	struct sysctlnode sn[SFS_NODEPERDIR];
    157 	struct sysctlnode qnode;
    158 	struct puffs_node *pn;
    159 	struct sfsnode *sfs;
    160 	SfsName myname, *sname;
    161 	size_t sl;
    162 	int i;
    163 
    164 	/*
    165 	 * Check if we need to create a new in-memory node or if we
    166 	 * already have one for this path.  Shortcut for the rootnode.
    167 	 * Also, memcmp against zero-length would be quite true always.
    168 	 */
    169 	if (po->po_len == 0)
    170 		pn = puffs_getroot(pu);
    171 	else
    172 		pn = puffs_pn_nodewalk(pu, puffs_path_walkcmp, po);
    173 
    174 	if (pn == NULL) {
    175 		/*
    176 		 * don't know nodetype?  query...
    177 		 *
    178 		 * XXX1: nothing really guarantees 0 is an invalid nodetype
    179 		 * XXX2: is there really no easier way of doing this?  we
    180 		 *       know the whole mib path
    181 		 */
    182 		if (!nodetype) {
    183 			sname = po->po_path;
    184 			memcpy(myname, po->po_path, po->po_len * sizeof(int));
    185 
    186 			memset(&qnode, 0, sizeof(qnode));
    187 			qnode.sysctl_flags = SYSCTL_VERSION;
    188 			myname[po->po_len-1] = CTL_QUERY;
    189 
    190 			sl = sizeof(sn);
    191 			if (sysctl(myname, po->po_len, sn, &sl,
    192 			    &qnode, sizeof(qnode)) == -1)
    193 				abort();
    194 
    195 			for (i = 0; i < sl / sizeof(struct sysctlnode); i++) {
    196 				 if (sn[i].sysctl_num==(*sname)[po->po_len-1]) {
    197 					nodetype = sn[i].sysctl_flags;
    198 					break;
    199 				}
    200 			}
    201 			if (!nodetype)
    202 				return NULL;
    203 		}
    204 
    205 		sfs = emalloc(sizeof(struct sfsnode));
    206 		sfs->sysctl_flags = nodetype;
    207 		sfs->myid = nextid++;
    208 
    209 		pn = puffs_pn_new(pu, sfs);
    210 		assert(pn);
    211 	}
    212 
    213 	return pn;
    214 }
    215 
    216 int
    217 main(int argc, char *argv[])
    218 {
    219 	struct puffs_usermount *pu;
    220 	struct puffs_ops *pops;
    221 	mntoptparse_t mp;
    222 	int mntflags, pflags, lflags;
    223 	int ch;
    224 
    225 	setprogname(argv[0]);
    226 
    227 	if (argc < 2)
    228 		errx(1, "usage: %s sysctlfs [-o mntopts]mountpath",
    229 		    getprogname());
    230 
    231 	mntflags = pflags = lflags = 0;
    232 	while ((ch = getopt(argc, argv, "o:s")) != -1) {
    233 		switch (ch) {
    234 		case 'o':
    235 			mp = getmntopts(optarg, puffsmopts, &mntflags, &pflags);
    236 			if (mp == NULL)
    237 				err(1, "getmntopts");
    238 			freemntopts(mp);
    239 			break;
    240 		case 's':
    241 			lflags = PUFFSLOOP_NODAEMON;
    242 			break;
    243 		}
    244 	}
    245 	argv += optind;
    246 	argc -= optind;
    247 	pflags |= PUFFS_FLAG_BUILDPATH | PUFFS_KFLAG_NOCACHE;
    248 
    249 	if (pflags & PUFFS_FLAG_OPDUMP)
    250 		lflags |= PUFFSLOOP_NODAEMON;
    251 
    252 	if (argc != 2)
    253 		errx(1, "usage: %s [-o mntopts]mountpath", getprogname());
    254 
    255 	PUFFSOP_INIT(pops);
    256 
    257 	PUFFSOP_SETFSNOP(pops, unmount);
    258 	PUFFSOP_SETFSNOP(pops, sync);
    259 	PUFFSOP_SETFSNOP(pops, statvfs);
    260 	PUFFSOP_SET(pops, sysctlfs, fs, nodetofh);
    261 	PUFFSOP_SET(pops, sysctlfs, fs, fhtonode);
    262 
    263 	PUFFSOP_SET(pops, sysctlfs, node, lookup);
    264 	PUFFSOP_SET(pops, sysctlfs, node, getattr);
    265 	PUFFSOP_SET(pops, sysctlfs, node, setattr);
    266 	PUFFSOP_SET(pops, sysctlfs, node, readdir);
    267 	PUFFSOP_SET(pops, sysctlfs, node, read);
    268 	PUFFSOP_SET(pops, sysctlfs, node, write);
    269 	PUFFSOP_SET(pops, puffs_genfs, node, reclaim);
    270 
    271 	pu = puffs_init(pops, _PATH_PUFFS, "sysctlfs", NULL, pflags);
    272 	if (pu == NULL)
    273 		err(1, "puffs_init");
    274 
    275 	puffs_set_pathbuild(pu, sysctlfs_pathbuild);
    276 	puffs_set_pathtransform(pu, sysctlfs_pathtransform);
    277 	puffs_set_pathcmp(pu, sysctlfs_pathcmp);
    278 	puffs_set_pathfree(pu, sysctlfs_pathfree);
    279 
    280 	puffs_setfhsize(pu, sizeof(struct sfsfid), PUFFS_FHFLAG_NFSV3);
    281 
    282 	if (sysctlfs_domount(pu) != 0)
    283 		errx(1, "domount");
    284 
    285 	if (puffs_mount(pu, argv[1], mntflags, puffs_getroot(pu)) == -1)
    286 		err(1, "puffs_mount");
    287 
    288 	if (puffs_mainloop(pu, lflags) == -1)
    289 		err(1, "mainloop");
    290 
    291 	return 0;
    292 }
    293 
    294 static int
    295 sysctlfs_domount(struct puffs_usermount *pu)
    296 {
    297 	struct puffs_pathobj *po_root;
    298 	struct puffs_node *pn_root;
    299 	struct timeval tv_now;
    300 
    301 	rn.myid = 2;
    302 	rn.sysctl_flags = CTLTYPE_NODE;
    303 
    304 	gettimeofday(&tv_now, NULL);
    305 	TIMEVAL_TO_TIMESPEC(&tv_now, &fstime);
    306 
    307 	pn_root = puffs_pn_new(pu, &rn);
    308 	assert(pn_root != NULL);
    309 	puffs_setroot(pu, pn_root);
    310 
    311 	po_root = puffs_getrootpathobj(pu);
    312 	po_root->po_path = &sname_root;
    313 	po_root->po_len = 0;
    314 
    315 	fileuid = geteuid();
    316 	filegid = getegid();
    317 
    318 	if (fileuid == 0)
    319 		fileperms = 0755;
    320 	else
    321 		fileperms = 0555;
    322 
    323 	return 0;
    324 }
    325 
    326 int
    327 sysctlfs_fs_fhtonode(struct puffs_cc *pcc, void *fid, size_t fidsize,
    328 	struct puffs_newinfo *pni)
    329 {
    330 	struct puffs_pathobj po;
    331 	struct puffs_node *pn;
    332 	struct sfsnode *sfs;
    333 	struct sfsfid *sfid;
    334 
    335 	sfid = fid;
    336 
    337 	po.po_len = sfid->len;
    338 	po.po_path = &sfid->path;
    339 
    340 	pn = getnode(puffs_cc_getusermount(pcc), &po, 0);
    341 	if (pn == NULL)
    342 		return EINVAL;
    343 	sfs = pn->pn_data;
    344 
    345 	puffs_newinfo_setcookie(pni, pn);
    346 	if (ISADIR(sfs))
    347 		puffs_newinfo_setvtype(pni, VDIR);
    348 	else
    349 		puffs_newinfo_setvtype(pni, VREG);
    350 
    351 	return 0;
    352 }
    353 
    354 int
    355 sysctlfs_fs_nodetofh(struct puffs_cc *pcc, void *cookie,
    356 	void *fid, size_t *fidsize)
    357 {
    358 	struct puffs_node *pn = cookie;
    359 	struct sfsfid *sfid;
    360 
    361 	sfid = fid;
    362 	sfid->len = PNPLEN(pn);
    363 	memcpy(&sfid->path, PNPATH(pn), sfid->len * sizeof(int));
    364 
    365 	return 0;
    366 }
    367 
    368 static void
    369 doprint(struct sfsnode *sfs, struct puffs_pathobj *po,
    370 	char *buf, size_t bufsize)
    371 {
    372 	size_t sz;
    373 
    374 	assert(!ISADIR(sfs));
    375 
    376 	memset(buf, 0, bufsize);
    377 	switch (SYSCTL_TYPE(sfs->sysctl_flags)) {
    378 	case CTLTYPE_INT: {
    379 		int i;
    380 		sz = sizeof(int);
    381 		if (sysctl(po->po_path, po->po_len, &i, &sz, NULL, 0) == -1)
    382 			break;
    383 		snprintf(buf, bufsize, "%d", i);
    384 		break;
    385 	}
    386 	case CTLTYPE_QUAD: {
    387 		quad_t q;
    388 		sz = sizeof(q);
    389 		if (sysctl(po->po_path, po->po_len, &q, &sz, NULL, 0) == -1)
    390 			break;
    391 		snprintf(buf, bufsize, "%" PRId64, q);
    392 		break;
    393 	}
    394 	case CTLTYPE_STRUCT:
    395 		snprintf(buf, bufsize, "CTLTYPE_STRUCT: implement me and "
    396 		    "score a cookie");
    397 		break;
    398 	case CTLTYPE_STRING: {
    399 		sz = bufsize;
    400 		if (sysctl(po->po_path, po->po_len, buf, &sz, NULL, 0) == -1)
    401 			break;
    402 		break;
    403 	}
    404 	default:
    405 		snprintf(buf, bufsize, "invalid sysctl CTLTYPE");
    406 		break;
    407 	}
    408 }
    409 
    410 static int
    411 getlinks(struct sfsnode *sfs, struct puffs_pathobj *po)
    412 {
    413 	struct sysctlnode sn[SFS_NODEPERDIR];
    414 	struct sysctlnode qnode;
    415 	SfsName *sname;
    416 	size_t sl;
    417 
    418 	if (!ISADIR(sfs))
    419 		return 1;
    420 
    421 	memset(&qnode, 0, sizeof(qnode));
    422 	sl = sizeof(sn);
    423 	qnode.sysctl_flags = SYSCTL_VERSION;
    424 	sname = po->po_path;
    425 	(*sname)[po->po_len] = CTL_QUERY;
    426 
    427 	if (sysctl(*sname, po->po_len + 1, sn, &sl,
    428 	    &qnode, sizeof(qnode)) == -1)
    429 		return 0;
    430 
    431 	return (sl / sizeof(sn[0])) + 2;
    432 }
    433 
    434 static int
    435 getsize(struct sfsnode *sfs, struct puffs_pathobj *po)
    436 {
    437 	char buf[SFS_MAXFILE];
    438 
    439 	if (ISADIR(sfs))
    440 		return getlinks(sfs, po) * 16; /* totally arbitrary */
    441 
    442 	doprint(sfs, po, buf, sizeof(buf));
    443 	return strlen(buf) + 1;
    444 }
    445 
    446 int
    447 sysctlfs_node_lookup(struct puffs_cc *pcc, void *opc, struct puffs_newinfo *pni,
    448 	const struct puffs_cn *pcn)
    449 {
    450 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    451 	struct puffs_cn *p2cn = __UNCONST(pcn); /* XXX: fix the interface */
    452 	struct sysctlnode sn[SFS_NODEPERDIR];
    453 	struct sysctlnode qnode;
    454 	struct puffs_node *pn_dir = opc;
    455 	struct puffs_node *pn_new;
    456 	struct sfsnode *sfs_dir = pn_dir->pn_data, *sfs_new;
    457 	SfsName *sname = PCNPATH(pcn);
    458 	size_t sl;
    459 	int i, nodetype;
    460 
    461 	assert(ISADIR(sfs_dir));
    462 
    463 	/*
    464 	 * If we're looking for dotdot, we already have the entire pathname
    465 	 * in sname, courtesy of pathbuild, so we can skip this step.
    466 	 */
    467 	if (!PCNISDOTDOT(pcn)) {
    468 		memset(&qnode, 0, sizeof(qnode));
    469 		sl = SFS_NODEPERDIR * sizeof(struct sysctlnode);
    470 		qnode.sysctl_flags = SYSCTL_VERSION;
    471 		(*sname)[PCNPLEN(pcn)] = CTL_QUERY;
    472 
    473 		if (sysctl(*sname, PCNPLEN(pcn) + 1, sn, &sl,
    474 		    &qnode, sizeof(qnode)) == -1)
    475 			return ENOENT;
    476 
    477 		for (i = 0; i < sl / sizeof(struct sysctlnode); i++)
    478 			if (strcmp(sn[i].sysctl_name, pcn->pcn_name) == 0)
    479 				break;
    480 		if (i == sl / sizeof(struct sysctlnode))
    481 			return ENOENT;
    482 
    483 		(*sname)[PCNPLEN(pcn)] = sn[i].sysctl_num;
    484 		p2cn->pcn_po_full.po_len++;
    485 		nodetype = sn[i].sysctl_flags;
    486 	} else
    487 		nodetype = CTLTYPE_NODE;
    488 
    489 	pn_new = getnode(pu, &p2cn->pcn_po_full, nodetype);
    490 	sfs_new = pn_new->pn_data;
    491 
    492 	puffs_newinfo_setcookie(pni, pn_new);
    493 	if (ISADIR(sfs_new))
    494 		puffs_newinfo_setvtype(pni, VDIR);
    495 	else
    496 		puffs_newinfo_setvtype(pni, VREG);
    497 
    498 	return 0;
    499 }
    500 
    501 int
    502 sysctlfs_node_getattr(struct puffs_cc *pcc, void *opc, struct vattr *va,
    503 	const struct puffs_cred *pcr, const struct puffs_cid *pcid)
    504 {
    505 	struct puffs_node *pn = opc;
    506 	struct sfsnode *sfs = pn->pn_data;
    507 
    508 	memset(va, 0, sizeof(struct vattr));
    509 
    510 	if (ISADIR(sfs)) {
    511 		va->va_type = VDIR;
    512 		va->va_mode = 0555;
    513 	} else {
    514 		va->va_type = VREG;
    515 		va->va_mode = fileperms;
    516 	}
    517 	va->va_uid = fileuid;
    518 	va->va_gid = filegid;
    519 	va->va_nlink = getlinks(sfs, &pn->pn_po);
    520 	va->va_fileid = sfs->myid;
    521 	va->va_size = getsize(sfs, &pn->pn_po);
    522 	va->va_gen = 1;
    523 	va->va_rdev = PUFFS_VNOVAL;
    524 	va->va_blocksize = 512;
    525 	va->va_filerev = 1;
    526 
    527 	va->va_atime = va->va_mtime = va->va_ctime = va->va_birthtime = fstime;
    528 
    529 	return 0;
    530 }
    531 
    532 int
    533 sysctlfs_node_setattr(struct puffs_cc *pcc, void *opc,
    534 	const struct vattr *va, const struct puffs_cred *pcr,
    535 	const struct puffs_cid *pcid)
    536 {
    537 
    538 	/* dummy, but required for write */
    539 	/* XXX: we could return EOPNOTSUPP or something */
    540 	return 0;
    541 }
    542 
    543 int
    544 sysctlfs_node_readdir(struct puffs_cc *pcc, void *opc, struct dirent *dent,
    545 	off_t *readoff, size_t *reslen, const struct puffs_cred *pcr,
    546 	int *eofflag, off_t *cookies, size_t *ncookies)
    547 {
    548 	struct puffs_usermount *pu = puffs_cc_getusermount(pcc);
    549 	struct sysctlnode sn[SFS_NODEPERDIR];
    550 	struct sysctlnode qnode;
    551 	struct puffs_node *pn_dir = opc;
    552 	struct puffs_node *pn_res;
    553 	struct puffs_pathobj po;
    554 	struct sfsnode *sfs_dir = pn_dir->pn_data, *sfs_ent;
    555 	SfsName *sname;
    556 	size_t sl;
    557 	enum vtype vt;
    558 	ino_t id;
    559 	int i;
    560 
    561 	*ncookies = 0;
    562 
    563  again:
    564 	if (*readoff == DENT_DOT || *readoff == DENT_DOTDOT) {
    565 		puffs_gendotdent(&dent, sfs_dir->myid, *readoff, reslen);
    566 		(*readoff)++;
    567 		PUFFS_STORE_DCOOKIE(cookies, ncookies, *readoff);
    568 		goto again;
    569 	}
    570 
    571 	memset(&qnode, 0, sizeof(qnode));
    572 	sl = SFS_NODEPERDIR * sizeof(struct sysctlnode);
    573 	qnode.sysctl_flags = SYSCTL_VERSION;
    574 	sname = PNPATH(pn_dir);
    575 	(*sname)[PNPLEN(pn_dir)] = CTL_QUERY;
    576 
    577 	if (sysctl(*sname, PNPLEN(pn_dir) + 1, sn, &sl,
    578 	    &qnode, sizeof(qnode)) == -1)
    579 		return ENOENT;
    580 
    581 	po.po_path = sname;
    582 	po.po_len = PNPLEN(pn_dir)+1;
    583 
    584 	for (i = DENT_ADJ(*readoff); i < sl / sizeof(struct sysctlnode); i++) {
    585 		if (SYSCTL_TYPE(sn[i].sysctl_flags) == CTLTYPE_NODE)
    586 			vt = VDIR;
    587 		else
    588 			vt = VREG;
    589 
    590 		/*
    591 		 * check if the node exists.  if so, give it the real
    592 		 * inode number.  otherwise just fake it.
    593 		 */
    594 		(*sname)[PNPLEN(pn_dir)] = sn[i].sysctl_num;
    595 		pn_res = puffs_pn_nodewalk(pu, puffs_path_walkcmp, &po);
    596 		if (pn_res) {
    597 			sfs_ent = pn_res->pn_data;
    598 			id = sfs_ent->myid;
    599 		} else {
    600 			id = nextid++;
    601 		}
    602 
    603 		if (!puffs_nextdent(&dent, sn[i].sysctl_name, id,
    604 		    puffs_vtype2dt(vt), reslen))
    605 			return 0;
    606 
    607 		(*readoff)++;
    608 		PUFFS_STORE_DCOOKIE(cookies, ncookies, *readoff);
    609 	}
    610 
    611 	*eofflag = 1;
    612 	return 0;
    613 }
    614 
    615 int
    616 sysctlfs_node_read(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    617 	off_t offset, size_t *resid, const struct puffs_cred *pcr,
    618 	int ioflag)
    619 {
    620 	char localbuf[SFS_MAXFILE];
    621 	struct puffs_node *pn = opc;
    622 	struct sfsnode *sfs = pn->pn_data;
    623 	int xfer;
    624 
    625 	if (ISADIR(sfs))
    626 		return EISDIR;
    627 
    628 	doprint(sfs, &pn->pn_po, localbuf, sizeof(localbuf));
    629 	xfer = MIN(*resid, strlen(localbuf) - offset);
    630 
    631 	if (xfer <= 0)
    632 		return 0;
    633 
    634 	memcpy(buf, localbuf + offset, xfer);
    635 	*resid -= xfer;
    636 
    637 	if (*resid) {
    638 		buf[xfer] = '\n';
    639 		(*resid)--;
    640 	}
    641 
    642 	return 0;
    643 }
    644 
    645 int
    646 sysctlfs_node_write(struct puffs_cc *pcc, void *opc, uint8_t *buf,
    647 	off_t offset, size_t *resid, const struct puffs_cred *cred,
    648 	int ioflag)
    649 {
    650 	struct puffs_node *pn = opc;
    651 	struct sfsnode *sfs = pn->pn_data;
    652 	long long ll;
    653 	int i, rv;
    654 
    655 	if (puffs_cred_isjuggernaut(cred) == 0)
    656 		return EACCES;
    657 
    658 	if (ISADIR(sfs))
    659 		return EISDIR;
    660 
    661 	if (offset != 0)
    662 		return EINVAL;
    663 
    664 	if (ioflag & PUFFS_IO_APPEND)
    665 		return EINVAL;
    666 
    667 	switch (SYSCTL_TYPE(sfs->sysctl_flags)) {
    668 	case CTLTYPE_INT:
    669 		if (sscanf((const char *)buf, "%d", &i) != 1)
    670 			return EINVAL;
    671 		rv = sysctl(PNPATH(pn), PNPLEN(pn), NULL, NULL,
    672 		    &i, sizeof(int));
    673 		break;
    674 	case CTLTYPE_QUAD:
    675 		if (sscanf((const char *)buf, "%lld", &ll) != 1)
    676 			return EINVAL;
    677 		rv =  sysctl(PNPATH(pn), PNPLEN(pn), NULL, NULL,
    678 		    &ll, sizeof(long long));
    679 		break;
    680 	case CTLTYPE_STRING:
    681 		rv = sysctl(PNPATH(pn), PNPLEN(pn), NULL, NULL, buf, *resid);
    682 		break;
    683 	default:
    684 		rv = EINVAL;
    685 		break;
    686 	}
    687 
    688 	if (rv)
    689 		return rv;
    690 
    691 	*resid = 0;
    692 	return 0;
    693 }
    694