Skip to content

Commit

Permalink
Start port of zed
Browse files Browse the repository at this point in the history
zed -d path/to/zed.d -p path/to/pid.txt -s path/to/zed.state -f

Add simple zedlet and support functions

Add all-debug.ps1 example, which uses
zed-function.ps1 to post Notification on
events.

More work needed, in particular to make scripts run
in the background rather than being waited upon.

Signed-off-by: Jorgen Lundman <[email protected]>
  • Loading branch information
lundman committed Oct 29, 2024
1 parent e915a89 commit 801cc56
Show file tree
Hide file tree
Showing 24 changed files with 1,249 additions and 11 deletions.
1 change: 1 addition & 0 deletions cmd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ add_subdirectory(os/windows/kstat)
add_subdirectory(os/windows/zfsinstaller)
add_subdirectory(raidz_test)
add_subdirectory(zinject)
add_subdirectory(zed)
35 changes: 35 additions & 0 deletions cmd/zed/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

use_clang()

um_add_executable(zed
zed.c
zed_conf.c
zed_disk_event.c
zed_event.c
zed_file.c
zed_log.c
zed_strings.c
agents/fmd_api.c
agents/fmd_serd.c
agents/zfs_agents.c
agents/zfs_diagnosis.c
agents/zfs_mod.c
agents/zfs_retire.c
os/windows/zed_exec.c
os/windows/resource.rc
)
target_link_libraries(zed PRIVATE
# libnvpair
# libuutil
libzfs
# libzfs_core
libzpool
)

target_include_directories(zed PRIVATE "${CMAKE_SOURCE_DIR}/cmd/zed" "${CMAKE_SOURCE_DIR}/include")

install(TARGETS zed RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}")
install(FILES $<TARGET_PDB_FILE:zed>
DESTINATION "${CMAKE_INSTALL_BINDIR}"
OPTIONAL
)
10 changes: 5 additions & 5 deletions cmd/zed/agents/fmd_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ fmd_serd_gc(fmd_hdl_t *hdl)
}

/* FMD Timers */

#if 0
static void
_timer_notify(union sigval sv)
{
Expand All @@ -584,7 +584,7 @@ _timer_notify(union sigval sv)
if (ops->fmdo_timeout != NULL)
ops->fmdo_timeout(hdl, ftp, ftp->ft_arg);
}

#endif
/*
* Install a new timer which will fire at least delta nanoseconds after the
* current time. After the timeout has expired, the module's fmdo_timeout
Expand All @@ -608,9 +608,9 @@ fmd_timer_install(fmd_hdl_t *hdl, void *arg, fmd_event_t *ep, hrtime_t delta)
its.it_interval.tv_nsec = its.it_value.tv_nsec;

sev.sigev_notify = SIGEV_THREAD;
sev.sigev_notify_function = _timer_notify;
sev.sigev_notify_attributes = NULL;
sev.sigev_value.sival_ptr = ftp;
// sev.sigev_notify_function = _timer_notify;
// sev.sigev_notify_attributes = NULL;
// sev.sigev_value.sival_ptr = ftp;
sev.sigev_signo = 0;

timer_create(CLOCK_REALTIME, &sev, &ftp->ft_tid);
Expand Down
15 changes: 15 additions & 0 deletions cmd/zed/os/windows/resource.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// {{NO_DEPENDENCIES}}
// Microsoft Visual C++ generated include file.
// Used by resource.rc
//

// Next default values for new objects
//
#ifdef APSTUDIO_INVOKED
#ifndef APSTUDIO_READONLY_SYMBOLS
#define _APS_NEXT_RESOURCE_VALUE 101
#define _APS_NEXT_COMMAND_VALUE 40001
#define _APS_NEXT_CONTROL_VALUE 1000
#define _APS_NEXT_SYMED_VALUE 101
#endif
#endif
110 changes: 110 additions & 0 deletions cmd/zed/os/windows/resource.rc
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
// Microsoft Visual C++ generated resource script.
//
#include <windows.h>
#include "resource.h"

#define APSTUDIO_READONLY_SYMBOLS
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 2 resource.
//
#include "winres.h"

/////////////////////////////////////////////////////////////////////////////
#undef APSTUDIO_READONLY_SYMBOLS

/////////////////////////////////////////////////////////////////////////////
// English (United States) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
#pragma code_page(1252)

#ifdef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// TEXTINCLUDE
//

1 TEXTINCLUDE
BEGIN
"resource.h\0"
END

2 TEXTINCLUDE
BEGIN
"#include ""winres.h""\r\n"
"\0"
END

3 TEXTINCLUDE
BEGIN
"\r\n"
"\0"
END

#endif // APSTUDIO_INVOKED

#endif // English (United States) resources
/////////////////////////////////////////////////////////////////////////////


/////////////////////////////////////////////////////////////////////////////
// English (India) resources

#if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENN)
LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_INDIA
#pragma code_page(1252)

/////////////////////////////////////////////////////////////////////////////
//
// Version
//

VS_VERSION_INFO VERSIONINFO
FILEVERSION 1,0,0,1
PRODUCTVERSION 1,0,0,1
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x40004L
FILETYPE 0x1L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "400904b0"
BEGIN
VALUE "CompanyName", "TODO: <Company name>"
VALUE "FileDescription", "zed.exe"
VALUE "FileVersion", "1.0.0.1"
VALUE "LegalCopyright", "Copyright (C) 2021"
VALUE "OriginalFilename", "resource.rc"
VALUE "ProductName", "zed.exe"
VALUE "ProductVersion", "1.0.0.1"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x4009, 1200
END
END

#endif // English (India) resources
/////////////////////////////////////////////////////////////////////////////



#ifndef APSTUDIO_INVOKED
/////////////////////////////////////////////////////////////////////////////
//
// Generated from the TEXTINCLUDE 3 resource.
//


/////////////////////////////////////////////////////////////////////////////
#endif // not APSTUDIO_INVOKED

30 changes: 30 additions & 0 deletions cmd/zed/os/windows/zed.d/all-debug.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/powershell
# shellcheck disable=SC2154
#
# Log all environment variables to ZED_DEBUG_LOG.
#
# This can be a useful aid when developing/debugging ZEDLETs since it shows the
# environment variables defined for each zevent.

if (Test-Path "$env:ZED_ZEDLET_DIR\zed.rc") {
. "$env:ZED_ZEDLET_DIR\zed.rc"
}
if (Test-Path "$env:ZED_ZEDLET_DIR\zed-functions.ps1") {
. "$env:ZED_ZEDLET_DIR\zed-functions.ps1"
}

#
# all-debug.ps1
#

Write-Host "all-debug.ps1 is running"

$envVar = $env:ZED_ZEDLET_DIR
if ($envVar) {
Write-Host "ZED_ZEDLET_DIR is set to: $envVar"
} else {
Write-Host "ZED_ZEDLET_DIR is not set."
}

Show-ToastNotification -Title "POOL: $env:ZEVENT_POOL" -Message "Event $env:ZEVENT_SUBCLASS has occurred."

107 changes: 107 additions & 0 deletions cmd/zed/os/windows/zed.d/zed-functions.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# zed-functions.ps1
#
# 2024 Jorgen Lundman <[email protected]>
#

# This one doesn't need any extra PS modules, but is
# ugly.
function Show-PlainToastNotification {
param (
[string]$Title = "ZED Notification",
[string]$Message = "Default message"
)

# Load the required assemblies
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Create a form to act as a notification
$form = New-Object System.Windows.Forms.Form
$form.StartPosition = 'Manual'
$form.Location = New-Object System.Drawing.Point(0, 0)
$form.Size = New-Object System.Drawing.Size(300, 100)
$form.FormBorderStyle = 'None'
$form.BackColor = [System.Drawing.Color]::White
$form.TopMost = $true
$form.ShowInTaskbar = $false
$form.Opacity = 0.9

# Create a label for the title
$titleLabel = New-Object System.Windows.Forms.Label
$titleLabel.Text = $Title
$titleLabel.Font = New-Object System.Drawing.Font("Arial", 14, [System.Drawing.FontStyle]::Bold)
$titleLabel.AutoSize = $true
$titleLabel.Location = New-Object System.Drawing.Point(10, 10)

# Create a label for the message
$messageLabel = New-Object System.Windows.Forms.Label
$messageLabel.Text = $Message
$messageLabel.AutoSize = $true
$messageLabel.Location = New-Object System.Drawing.Point(10, 40)

# Add the labels to the form
$form.Controls.Add($titleLabel)
$form.Controls.Add($messageLabel)

# Show the form
$form.Show()

# Timer to close the notification after 5 seconds
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 5000 # milliseconds
$timer.Add_Tick({
$form.Close()
$timer.Stop()
})
$timer.Start()

# Run the form's message loop
[System.Windows.Forms.Application]::Run($form)
}

# For real notifications to work, run in powershell (Admin)
# "Install-Module -Name BurntToast -Force -AllowClobber"
# Failing that, fall back to a regular MessageBox
function Show-ToastNotification {
param (
[string]$Title = "Notification",
[string]$Message = "This is a test notification."
)

# Check if the BurntToast module is available
if (Get-Module -ListAvailable -Name BurntToast) {
try {
# Import the module
Import-Module BurntToast -ErrorAction Stop

# Create a BurntToast notification
New-BurntToastNotification -Text $Title, $Message
}
catch {
# If there's an error, fall back to a basic notification
Write-Host "BurntToast failed to create a notification: $_"
Show-MessageBox "$Message" "$Title"
}
}
else {
# Fallback if BurntToast is not installed
Write-Host "BurntToast module is not installed. Falling back to basic notification."
Show-MessageBox "$Message" "$Title"
}
}

# Function to show a message box
function Show-MessageBox {
param (
[string]$Message,
[string]$Title
)

# Load the necessary assembly
Add-Type -AssemblyName System.Windows.Forms

# Display the message box
[System.Windows.Forms.MessageBox]::Show($Message, $Title)
}


Loading

0 comments on commit 801cc56

Please sign in to comment.