After searching the web for a solution and not finding a library, I’m instead trying to write a systemd journal reader using csharp and not having much luck.
There’s an example written in c++ at the bottom of the following link:
https://www.freedesktop.org/software/systemd/man/latest/sd_journal_next.html
This is the code I have so far, which, when I run it, I get
<code>$ ./LogPublisher
success: 0
success: -10
success: -10
Success: -10 Message:
</code>
$ ./LogPublisher
success: 0
success: -10
success: -10
Success: -10 Message:
As far as I can tell, -10
refers to No child processes
as taken from https://en.wikipedia.org/wiki/Errno.h
<code>using System.Runtime.InteropServices;
SD_JOURNAL_LOCAL_ONLY = 1 << 0, // 0000 0001 = 1
SD_JOURNAL_RUNTIME_ONLY = 1 << 1, // 0000 0010 = 2
SD_JOURNAL_SYSTEM = 1 << 2, // 0000 0100 = 4
SD_JOURNAL_CURRENT_USER = 1 << 3, // 0000 1000 = 8
SD_JOURNAL_OS_ROOT = 1 << 4, // 0001 0000 = 16
SD_JOURNAL_ALL_NAMESPACES = 1 << 5, // (32) Show all namespaces, not just the default or specified one */
SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE = 1 << 6, // (64) Show default namespace in addition to specified one */
SD_JOURNAL_TAKE_DIRECTORY_FD = 1 << 7, // (128) sd_journal_open_directory_fd() will take ownership of the provided file descriptor. */
SD_JOURNAL_ASSUME_IMMUTABLE = 1 << 8, // (256) Assume the opened journal files are immutable. Journal entries added later may be ignored. */
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
internal static class Systemd
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_open(ref Journal sd_journal, int flags);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_seek_head(ref Journal sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_next(ref Journal sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_get_data(ref Journal sd_journal, string field, [Out] StringBuilder data, [Out] ulong length);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_seek_head(IntPtr sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern void sd_journal_close(ref Journal sd_journal);
success = Systemd.sd_journal_open(ref j, (int)Flags.SD_JOURNAL_LOCAL_ONLY);
Console.WriteLine($"Success: {success}");
success = Systemd.sd_journal_seek_head(ref j);
Console.WriteLine($"Success: {success}");
// success = Systemd.sd_journal_next(ref j);
// Console.WriteLine($"Success: {success}");
StringBuilder data = new(1024 * 768 * 4);
success = Systemd.sd_journal_get_data(ref j, "MESSAGE", data, length);
Console.WriteLine($"Success: {success} Message: {data}");
Systemd.sd_journal_close(ref j);
<code>using System.Runtime.InteropServices;
using System.Text;
class Program
{
enum Flags
{
SD_JOURNAL_LOCAL_ONLY = 1 << 0, // 0000 0001 = 1
SD_JOURNAL_RUNTIME_ONLY = 1 << 1, // 0000 0010 = 2
SD_JOURNAL_SYSTEM = 1 << 2, // 0000 0100 = 4
SD_JOURNAL_CURRENT_USER = 1 << 3, // 0000 1000 = 8
SD_JOURNAL_OS_ROOT = 1 << 4, // 0001 0000 = 16
SD_JOURNAL_ALL_NAMESPACES = 1 << 5, // (32) Show all namespaces, not just the default or specified one */
SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE = 1 << 6, // (64) Show default namespace in addition to specified one */
SD_JOURNAL_TAKE_DIRECTORY_FD = 1 << 7, // (128) sd_journal_open_directory_fd() will take ownership of the provided file descriptor. */
SD_JOURNAL_ASSUME_IMMUTABLE = 1 << 8, // (256) Assume the opened journal files are immutable. Journal entries added later may be ignored. */
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Journal
{
public int toplevel_fd;
public string path;
public string prefix;
}
internal static class Systemd
{
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_open(ref Journal sd_journal, int flags);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_seek_head(ref Journal sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_next(ref Journal sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_get_data(ref Journal sd_journal, string field, [Out] StringBuilder data, [Out] ulong length);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_seek_head(IntPtr sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern void sd_journal_close(ref Journal sd_journal);
}
static void Main()
{
int success;
Journal j = new();
success = Systemd.sd_journal_open(ref j, (int)Flags.SD_JOURNAL_LOCAL_ONLY);
Console.WriteLine($"Success: {success}");
success = Systemd.sd_journal_seek_head(ref j);
Console.WriteLine($"Success: {success}");
// success = Systemd.sd_journal_next(ref j);
// Console.WriteLine($"Success: {success}");
ulong length = 0;
StringBuilder data = new(1024 * 768 * 4);
success = Systemd.sd_journal_get_data(ref j, "MESSAGE", data, length);
Console.WriteLine($"Success: {success} Message: {data}");
Systemd.sd_journal_close(ref j);
}
}
</code>
using System.Runtime.InteropServices;
using System.Text;
class Program
{
enum Flags
{
SD_JOURNAL_LOCAL_ONLY = 1 << 0, // 0000 0001 = 1
SD_JOURNAL_RUNTIME_ONLY = 1 << 1, // 0000 0010 = 2
SD_JOURNAL_SYSTEM = 1 << 2, // 0000 0100 = 4
SD_JOURNAL_CURRENT_USER = 1 << 3, // 0000 1000 = 8
SD_JOURNAL_OS_ROOT = 1 << 4, // 0001 0000 = 16
SD_JOURNAL_ALL_NAMESPACES = 1 << 5, // (32) Show all namespaces, not just the default or specified one */
SD_JOURNAL_INCLUDE_DEFAULT_NAMESPACE = 1 << 6, // (64) Show default namespace in addition to specified one */
SD_JOURNAL_TAKE_DIRECTORY_FD = 1 << 7, // (128) sd_journal_open_directory_fd() will take ownership of the provided file descriptor. */
SD_JOURNAL_ASSUME_IMMUTABLE = 1 << 8, // (256) Assume the opened journal files are immutable. Journal entries added later may be ignored. */
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Journal
{
public int toplevel_fd;
public string path;
public string prefix;
}
internal static class Systemd
{
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_open(ref Journal sd_journal, int flags);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_seek_head(ref Journal sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_next(ref Journal sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_get_data(ref Journal sd_journal, string field, [Out] StringBuilder data, [Out] ulong length);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern int sd_journal_seek_head(IntPtr sd_journal);
[DllImport("libsystemd.so.0", CharSet = CharSet.Ansi)]
internal static extern void sd_journal_close(ref Journal sd_journal);
}
static void Main()
{
int success;
Journal j = new();
success = Systemd.sd_journal_open(ref j, (int)Flags.SD_JOURNAL_LOCAL_ONLY);
Console.WriteLine($"Success: {success}");
success = Systemd.sd_journal_seek_head(ref j);
Console.WriteLine($"Success: {success}");
// success = Systemd.sd_journal_next(ref j);
// Console.WriteLine($"Success: {success}");
ulong length = 0;
StringBuilder data = new(1024 * 768 * 4);
success = Systemd.sd_journal_get_data(ref j, "MESSAGE", data, length);
Console.WriteLine($"Success: {success} Message: {data}");
Systemd.sd_journal_close(ref j);
}
}
I’ve been trying to reference the source code for journal to replicate but just cant work it out.
https://github.com/systemd/systemd/blob/main/src/systemd/sd-journal.h