mirror of
https://github.com/ebrandi/FDD-book.git
synced 2026-12-06 08:33:30 +00:00
162 lines
3.4 KiB
C
162 lines
3.4 KiB
C
|
|
/*-
|
||
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
||
|
|
*
|
||
|
|
* Copyright (c) 2025 Your Name
|
||
|
|
* All rights reserved.
|
||
|
|
*
|
||
|
|
* Lab 7.4, variant B.
|
||
|
|
*
|
||
|
|
* Same code as stage2-final/myfirst.c, but attach is rigged to fail
|
||
|
|
* right after make_dev_s succeeds, before sysctl setup. The failure
|
||
|
|
* jumps to fail_dev, exercising the destroy_dev() leg of the unwind.
|
||
|
|
* Loading should fail with ENOMEM, no /dev/myfirst0 should be left
|
||
|
|
* behind, and no leaked mutex.
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include <sys/param.h>
|
||
|
|
#include <sys/module.h>
|
||
|
|
#include <sys/kernel.h>
|
||
|
|
#include <sys/systm.h>
|
||
|
|
#include <sys/bus.h>
|
||
|
|
#include <sys/conf.h>
|
||
|
|
#include <sys/uio.h>
|
||
|
|
#include <sys/lock.h>
|
||
|
|
#include <sys/mutex.h>
|
||
|
|
#include <sys/sysctl.h>
|
||
|
|
|
||
|
|
struct myfirst_softc {
|
||
|
|
device_t dev;
|
||
|
|
int unit;
|
||
|
|
struct mtx mtx;
|
||
|
|
uint64_t attach_ticks;
|
||
|
|
uint64_t open_count;
|
||
|
|
uint64_t bytes_read;
|
||
|
|
int is_attached;
|
||
|
|
int is_open;
|
||
|
|
struct cdev *cdev;
|
||
|
|
struct sysctl_ctx_list sysctl_ctx;
|
||
|
|
struct sysctl_oid *sysctl_tree;
|
||
|
|
};
|
||
|
|
|
||
|
|
static d_open_t myfirst_open;
|
||
|
|
static d_close_t myfirst_close;
|
||
|
|
static d_read_t myfirst_read;
|
||
|
|
static d_write_t myfirst_write;
|
||
|
|
|
||
|
|
static struct cdevsw myfirst_cdevsw = {
|
||
|
|
.d_version = D_VERSION,
|
||
|
|
.d_open = myfirst_open,
|
||
|
|
.d_close = myfirst_close,
|
||
|
|
.d_read = myfirst_read,
|
||
|
|
.d_write = myfirst_write,
|
||
|
|
.d_name = "myfirst",
|
||
|
|
};
|
||
|
|
|
||
|
|
static int
|
||
|
|
myfirst_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
|
||
|
|
{
|
||
|
|
return (0);
|
||
|
|
}
|
||
|
|
|
||
|
|
static int
|
||
|
|
myfirst_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
|
||
|
|
{
|
||
|
|
return (0);
|
||
|
|
}
|
||
|
|
|
||
|
|
static int
|
||
|
|
myfirst_read(struct cdev *dev, struct uio *uio, int ioflag)
|
||
|
|
{
|
||
|
|
return (0);
|
||
|
|
}
|
||
|
|
|
||
|
|
static int
|
||
|
|
myfirst_write(struct cdev *dev, struct uio *uio, int ioflag)
|
||
|
|
{
|
||
|
|
uio->uio_resid = 0;
|
||
|
|
return (0);
|
||
|
|
}
|
||
|
|
|
||
|
|
static void
|
||
|
|
myfirst_identify(driver_t *driver, device_t parent)
|
||
|
|
{
|
||
|
|
if (device_find_child(parent, driver->name, -1) != NULL)
|
||
|
|
return;
|
||
|
|
if (BUS_ADD_CHILD(parent, 0, driver->name, -1) == NULL)
|
||
|
|
device_printf(parent, "myfirst: BUS_ADD_CHILD failed\n");
|
||
|
|
}
|
||
|
|
|
||
|
|
static int
|
||
|
|
myfirst_probe(device_t dev)
|
||
|
|
{
|
||
|
|
device_set_desc(dev, "My First FreeBSD Driver");
|
||
|
|
return (BUS_PROBE_DEFAULT);
|
||
|
|
}
|
||
|
|
|
||
|
|
static int
|
||
|
|
myfirst_attach(device_t dev)
|
||
|
|
{
|
||
|
|
struct myfirst_softc *sc;
|
||
|
|
struct make_dev_args args;
|
||
|
|
int error;
|
||
|
|
|
||
|
|
sc = device_get_softc(dev);
|
||
|
|
sc->dev = dev;
|
||
|
|
sc->unit = device_get_unit(dev);
|
||
|
|
|
||
|
|
mtx_init(&sc->mtx, device_get_nameunit(dev), "myfirst", MTX_DEF);
|
||
|
|
|
||
|
|
make_dev_args_init(&args);
|
||
|
|
args.mda_devsw = &myfirst_cdevsw;
|
||
|
|
args.mda_uid = UID_ROOT;
|
||
|
|
args.mda_gid = GID_WHEEL;
|
||
|
|
args.mda_mode = 0600;
|
||
|
|
args.mda_si_drv1 = sc;
|
||
|
|
|
||
|
|
error = make_dev_s(&args, &sc->cdev, "myfirst%d", sc->unit);
|
||
|
|
if (error != 0) {
|
||
|
|
device_printf(dev, "Failed to create device node: %d\n",
|
||
|
|
error);
|
||
|
|
goto fail_mtx;
|
||
|
|
}
|
||
|
|
|
||
|
|
/* SIMULATED FAILURE FOR LAB 7.4 (variant B) */
|
||
|
|
device_printf(dev, "Simulating failure after dev node creation\n");
|
||
|
|
error = ENOMEM;
|
||
|
|
goto fail_dev;
|
||
|
|
|
||
|
|
/* unreachable in this variant */
|
||
|
|
return (0);
|
||
|
|
|
||
|
|
fail_dev:
|
||
|
|
destroy_dev(sc->cdev);
|
||
|
|
sc->cdev = NULL;
|
||
|
|
fail_mtx:
|
||
|
|
mtx_destroy(&sc->mtx);
|
||
|
|
sc->is_attached = 0;
|
||
|
|
return (error);
|
||
|
|
}
|
||
|
|
|
||
|
|
static int
|
||
|
|
myfirst_detach(device_t dev)
|
||
|
|
{
|
||
|
|
return (0);
|
||
|
|
}
|
||
|
|
|
||
|
|
static device_method_t myfirst_methods[] = {
|
||
|
|
DEVMETHOD(device_identify, myfirst_identify),
|
||
|
|
DEVMETHOD(device_probe, myfirst_probe),
|
||
|
|
DEVMETHOD(device_attach, myfirst_attach),
|
||
|
|
DEVMETHOD(device_detach, myfirst_detach),
|
||
|
|
DEVMETHOD_END
|
||
|
|
};
|
||
|
|
|
||
|
|
static driver_t myfirst_driver = {
|
||
|
|
"myfirst",
|
||
|
|
myfirst_methods,
|
||
|
|
sizeof(struct myfirst_softc)
|
||
|
|
};
|
||
|
|
|
||
|
|
DRIVER_MODULE(myfirst, nexus, myfirst_driver, 0, 0);
|
||
|
|
MODULE_VERSION(myfirst, 1);
|