null: merge /dev/zero and add a /dev/full

present on both FreeBSD and Linux.
useful to test a "disk full" condition.

Change-Id: Ibac6b50e8063c90e3e324ec04ac136c34298cc7f
Reviewed-on: https://review.haiku-os.org/c/haiku/+/9617
Reviewed-by: waddlesplash <[email protected]>
Tested-by: Commit checker robot <[email protected]>
This commit is contained in:
Jérôme Duval
2025-08-29 06:57:40 +00:00
committed by Adrien Destugues
parent effc4f4704
commit ad49acb949
5 changed files with 51 additions and 142 deletions
-1
View File
@@ -161,7 +161,6 @@ AddDriversToPackage :
null null
<driver>tty <driver>tty
usb_modeswitch usb_modeswitch
zero
; ;
AddDriversToPackage audio hmulti : AddDriversToPackage audio hmulti :
$(SYSTEM_ADD_ONS_DRIVERS_AUDIO) ; $(SYSTEM_ADD_ONS_DRIVERS_AUDIO) ;
-1
View File
@@ -106,7 +106,6 @@ AddDriversToPackage :
null null
random random
<driver>tty <driver>tty
zero
; ;
AddDriversToPackage audio hmulti AddDriversToPackage audio hmulti
: $(SYSTEM_ADD_ONS_DRIVERS_AUDIO) ; : $(SYSTEM_ADD_ONS_DRIVERS_AUDIO) ;
+1 -5
View File
@@ -7,11 +7,7 @@ KernelAddon dprintf :
; ;
KernelAddon null : KernelAddon null :
null.c null.cpp
;
KernelAddon zero :
zero.c
; ;
KernelAddon console : KernelAddon console :
@@ -8,10 +8,13 @@
#include <Drivers.h> #include <Drivers.h>
#include <KernelExport.h>
#include <string.h> #include <string.h>
#define DEVICE_NAME "null" #define DEVICE_NAME_FULL "full"
#define DEVICE_NAME_NULL "null"
#define DEVICE_NAME_ZERO "zero"
int32 api_version = B_CUR_DRIVER_API_VERSION; int32 api_version = B_CUR_DRIVER_API_VERSION;
@@ -53,6 +56,16 @@ null_read(void *cookie, off_t pos, void *buffer, size_t *_length)
} }
static status_t
zero_read(void *cookie, off_t pos, void *buffer, size_t *_length)
{
if (user_memset(buffer, 0, *_length) < B_OK)
return B_BAD_ADDRESS;
return B_OK;
}
static status_t static status_t
null_write(void *cookie, off_t pos, const void *buffer, size_t *_length) null_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
{ {
@@ -60,6 +73,13 @@ null_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
} }
static status_t
full_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
{
return B_DEVICE_FULL;
}
// #pragma mark - // #pragma mark -
@@ -74,7 +94,9 @@ const char **
publish_devices(void) publish_devices(void)
{ {
static const char *devices[] = { static const char *devices[] = {
DEVICE_NAME, DEVICE_NAME_FULL,
DEVICE_NAME_NULL,
DEVICE_NAME_ZERO,
NULL NULL
}; };
@@ -85,24 +107,40 @@ publish_devices(void)
device_hooks * device_hooks *
find_device(const char *name) find_device(const char *name)
{ {
static device_hooks hooks = { static device_hooks fullHooks = {
&null_open,
&null_close,
&null_freecookie,
&null_ioctl,
&null_read,
&full_write,
};
static device_hooks nullHooks = {
&null_open, &null_open,
&null_close, &null_close,
&null_freecookie, &null_freecookie,
&null_ioctl, &null_ioctl,
&null_read, &null_read,
&null_write, &null_write,
/* Leave select/deselect/readv/writev undefined. The kernel will
* use its own default implementation. The basic hooks above this
* line MUST be defined, however. */
NULL,
NULL,
NULL,
NULL
}; };
if (!strcmp(name, DEVICE_NAME)) static device_hooks zeroHooks = {
return &hooks; &null_open,
&null_close,
&null_freecookie,
&null_ioctl,
&zero_read,
&null_write,
};
if (strcmp(name, DEVICE_NAME_FULL) == 0)
return &fullHooks;
if (strcmp(name, DEVICE_NAME_NULL) == 0)
return &nullHooks;
if (strcmp(name, DEVICE_NAME_ZERO) == 0)
return &zeroHooks;
return NULL; return NULL;
} }
-123
View File
@@ -1,123 +0,0 @@
/*
** Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
** Distributed under the terms of the NewOS License.
*/
#include <Drivers.h>
#include <KernelExport.h>
#include <string.h>
#define DEVICE_NAME "zero"
int32 api_version = B_CUR_DRIVER_API_VERSION;
static status_t
zero_open(const char *name, uint32 flags, void **cookie)
{
*cookie = NULL;
return B_OK;
}
static status_t
zero_close(void *cookie)
{
return B_OK;
}
static status_t
zero_freecookie(void *cookie)
{
return B_OK;
}
static status_t
zero_ioctl(void *cookie, uint32 op, void *buffer, size_t length)
{
return EPERM;
}
static status_t
zero_read(void *cookie, off_t pos, void *buffer, size_t *_length)
{
if (user_memset(buffer, 0, *_length) < B_OK)
return B_BAD_ADDRESS;
return B_OK;
}
static status_t
zero_write(void *cookie, off_t pos, const void *buffer, size_t *_length)
{
return B_OK;
}
// #pragma mark -
status_t
init_hardware(void)
{
return B_OK;
}
const char **
publish_devices(void)
{
static const char *devices[] = {
DEVICE_NAME,
NULL
};
return devices;
}
device_hooks *
find_device(const char *name)
{
static device_hooks hooks = {
&zero_open,
&zero_close,
&zero_freecookie,
&zero_ioctl,
&zero_read,
&zero_write,
/* Leave select/deselect/readv/writev undefined. The kernel will
* use its own default implementation. The basic hooks above this
* line MUST be defined, however. */
NULL,
NULL,
NULL,
NULL
};
if (!strcmp(name, DEVICE_NAME))
return &hooks;
return NULL;
}
status_t
init_driver(void)
{
return B_OK;
}
void
uninit_driver(void)
{
}