ORG.FREEDESKTOP.LOGCONTROL1(5) org.freedesktop.LogControl1 ORG.FREEDESKTOP.LOGCONTROL1(5)

org.freedesktop.LogControl1 - رابط دی‌باس برای کنترل ثبت رویدادها در سرویس‌های سیستم‌دی

org.freedesktop.LogControl1 یک رابط عمومی است که برای استفاده توسط هر دیمنی در نظر گرفته شده که امکان پرس‌وجو و تنظیم سطح و مقصد ثبت رویداد (لاگ‌گیری) را از طریق D-Bus فراهم می‌کند. این رابط توسط دیمن‌های گوناگونی که بخشی از مجموعه systemd(1) هستند پیاده‌سازی شده است.

فرض بر این است که این تنظیمات برای کل برنامه به‌صورت سراسری هستند، بنابراین از یک مسیر شیء ثابت استفاده می‌شود. این رابط باید همیشه در مسیر /org/freedesktop/LogControl1 در دسترس باشد.

رابط زیر ارائه شده است:

node /org/freedesktop/LogControl1 {
  interface org.freedesktop.LogControl1 {
    properties:
      @org.freedesktop.DBus.Property.EmitsChangedSignal("false")
      @org.freedesktop.systemd1.Privileged("true")
      readwrite s LogLevel = '...';
      @org.freedesktop.DBus.Property.EmitsChangedSignal("false")
      @org.freedesktop.systemd1.Privileged("true")
      readwrite s LogTarget = '...';
      @org.freedesktop.DBus.Property.EmitsChangedSignal("false")
      readonly s SyslogIdentifier = '...';
  };
  interface org.freedesktop.DBus.Peer { ... };
  interface org.freedesktop.DBus.Introspectable { ... };
  interface org.freedesktop.DBus.Properties { ... };
};

ویژگی LogLevel سطح لاگ‌گیری به سبک syslog(3) را توصیف می‌کند، و باید یکی از مقادیر "emerg", "alert", "crit", "err", "warning", "notice", "info", "debug" به ترتیب افزایش میزان جزئیات باشد.

ویژگی LogTarget مقصد (سازوکار) لاگ‌گیری را توصیف می‌کند. این ویژگی باید یکی از موارد زیر باشد: "console" (ثبت لاگ در کنسول یا خروجی استاندارد)، "kmsg" (ثبت لاگ در بافر حلقه‌ای هسته)، "journal" (ثبت لاگ به‌طور مستقیم در ژورنال، ببینید: systemd-journald.service(8))، "syslog" (ثبت لاگ با استفاده از فراخوانی syslog(3)).


دسترسی نوشتن (Write Access)

ویژگی‌های LogLevel و LogTarget باید قابل نوشتن باشند. باید دقت شود تا اطمینان حاصل گردد تنها کلاینت‌هایی با دسترسی و امتیاز مناسب می‌توانند آن‌ها را تغییر دهند.

ویژگی SyslogIdentifier یک ویژگی فقط‌خواندنی است که «شناسه syslog» را نشان می‌دهد. این یک رشته کوتاه است که برنامه مبدأ پیام‌های لاگ ارسالی به فراخوانی syslog(3) را مشخص می‌کند.

گزینه -p/--priority= در journalctl می‌تواند برای فیلتر کردن پیام‌های لاگ بر اساس سطح لاگ استفاده شود، گزینه -t/--identifier= می‌تواند بر اساس شناسه syslog به کار رود، و فیلترهایی مانند "_TRANSPORT=syslog", "_TRANSPORT=journal", و "_TRANSPORT=kernel" می‌توانند برای فیلتر کردن پیام‌ها بر اساس سازوکاری که از طریق آن به systemd-journald رسیده‌اند استفاده شوند.

دستورهای فرعی systemctl log-level و systemctl log-target می‌توانند برای پرس‌وجو و تنظیم ویژگی‌های LogLevel و LogTarget مدیر سرویس به کار روند. همچنین systemctl service-log-level و systemctl service-log-target می‌توانند به طور مشابه برای سرویس‌های منفرد استفاده شوند. (سرویس‌ها باید ویژگی BusName= را تنظیم کرده باشند و رابط شرح داده شده در اینجا را پیاده‌سازی کنند. برای جزئیات درباره BusName= صفحه راهنمای systemd.service(5) را ببینید.)

مثال ۱. ایجاد یک شنونده ساده روی گذرگاه که LogControl1 را پیاده‌سازی می‌کند

/* SPDX-License-Identifier: MIT-0 */
/* Implements the LogControl1 interface as per specification:
 * https://www.freedesktop.org/software/systemd/man/org.freedesktop.LogControl1.html
 *
 * Compile with 'cc logcontrol-example.c $(pkg-config --libs --cflags libsystemd)'
 *
 * To get and set properties via busctl:
 *
 * $ busctl --user get-property org.freedesktop.Example \
 *                              /org/freedesktop/LogControl1 \
 *                              org.freedesktop.LogControl1 \
 *                              SyslogIdentifier
 *   s "example"
 * $ busctl --user get-property org.freedesktop.Example \
 *                              /org/freedesktop/LogControl1 \
 *                              org.freedesktop.LogControl1 \
 *                              LogTarget
 *   s "journal"
 * $ busctl --user get-property org.freedesktop.Example \
 *                              /org/freedesktop/LogControl1 \
 *                              org.freedesktop.LogControl1 \
 *                              LogLevel
 *   s "info"
 * $ busctl --user set-property org.freedesktop.Example \
 *                              /org/freedesktop/LogControl1 \
 *                              org.freedesktop.LogControl1 \
 *                              LogLevel \
 *                              "s" debug
 * $ busctl --user get-property org.freedesktop.Example \
 *                              /org/freedesktop/LogControl1 \
 *                              org.freedesktop.LogControl1 \
 *                              LogLevel
 *   s "debug"
 */
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <syslog.h>
#include <systemd/sd-bus.h>
#include <systemd/sd-journal.h>
#define _cleanup_(f) __attribute__((cleanup(f)))
static int log_error(int log_level, int error, const char *str) {
  sd_journal_print(log_level, "%s failed: %s", str, strerror(-error));
  return error;
}
typedef enum LogTarget {
  LOG_TARGET_JOURNAL,
  LOG_TARGET_KMSG,
  LOG_TARGET_SYSLOG,
  LOG_TARGET_CONSOLE,
  _LOG_TARGET_MAX,
} LogTarget;
static const char* const log_target_table[_LOG_TARGET_MAX] = {
  [LOG_TARGET_JOURNAL] = "journal",
  [LOG_TARGET_KMSG]    = "kmsg",
  [LOG_TARGET_SYSLOG]  = "syslog",
  [LOG_TARGET_CONSOLE] = "console",
};
static const char* const log_level_table[LOG_DEBUG + 1] = {
  [LOG_EMERG]   = "emerg",
  [LOG_ALERT]   = "alert",
  [LOG_CRIT]    = "crit",
  [LOG_ERR]     = "err",
  [LOG_WARNING] = "warning",
  [LOG_NOTICE]  = "notice",
  [LOG_INFO]    = "info",
  [LOG_DEBUG]   = "debug",
};
typedef struct object {
  const char *syslog_identifier;
  LogTarget log_target;
  int log_level;
} object;
static int property_get(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *reply,
                void *userdata,
                sd_bus_error *error) {
  object *o = userdata;
  if (strcmp(property, "LogLevel") == 0)
    return sd_bus_message_append(reply, "s", log_level_table[o->log_level]);
  if (strcmp(property, "LogTarget") == 0)
    return sd_bus_message_append(reply, "s", log_target_table[o->log_target]);
  if (strcmp(property, "SyslogIdentifier") == 0)
    return sd_bus_message_append(reply, "s", o->syslog_identifier);
  return sd_bus_error_setf(error,
                           SD_BUS_ERROR_UNKNOWN_PROPERTY,
                           "Unknown property '%s'",
                           property);
}
static int property_set(
                sd_bus *bus,
                const char *path,
                const char *interface,
                const char *property,
                sd_bus_message *message,
                void *userdata,
                sd_bus_error *error) {
  object *o = userdata;
  const char *value;
  int r;
  r = sd_bus_message_read(message, "s", &value);
  if (r < 0)
    return r;
  if (strcmp(property, "LogLevel") == 0) {
    int i;
    for (i = 0; i < LOG_DEBUG + 1; i++)
      if (strcmp(value, log_level_table[i]) == 0) {
        o->log_level = i;
        setlogmask(LOG_UPTO(i));
        return 0;
      }
    return sd_bus_error_setf(error,
                             SD_BUS_ERROR_INVALID_ARGS,
                             "Invalid value for LogLevel: '%s'",
                             value);
  }
  if (strcmp(property, "LogTarget") == 0) {
    LogTarget i;
    for (i = 0; i < _LOG_TARGET_MAX; i++)
      if (strcmp(value, log_target_table[i]) == 0) {
        o->log_target = i;
        return 0;
      }
    return sd_bus_error_setf(error,
                             SD_BUS_ERROR_INVALID_ARGS,
                             "Invalid value for LogTarget: '%s'",
                             value);
  }
  return sd_bus_error_setf(error,
                           SD_BUS_ERROR_UNKNOWN_PROPERTY,
                           "Unknown property '%s'",
                           property);
}
/* https://www.freedesktop.org/software/systemd/man/sd_bus_add_object.html
 */
static const sd_bus_vtable vtable[] = {
  SD_BUS_VTABLE_START(0),
  SD_BUS_WRITABLE_PROPERTY(
    "LogLevel", "s",
    property_get, property_set,
    0,
    0),
  SD_BUS_WRITABLE_PROPERTY(
    "LogTarget", "s",
    property_get, property_set,
    0,
    0),
  SD_BUS_PROPERTY(
    "SyslogIdentifier", "s",
    property_get,
    0,
    SD_BUS_VTABLE_PROPERTY_CONST),
  SD_BUS_VTABLE_END
};
int main(int argc, char **argv) {
  /* The bus should be relinquished before the program terminates. The cleanup
   * attribute allows us to do it nicely and cleanly whenever we exit the
   * block.
   */
  _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL;
  object o = {
    .log_level = LOG_INFO,
    .log_target = LOG_TARGET_JOURNAL,
    .syslog_identifier = "example",
  };
  int r;
  /* https://man7.org/linux/man-pages/man3/setlogmask.3.html
   * Programs using syslog() instead of sd_journal can use this API to cut logs
   * emission at the source.
   */
  setlogmask(LOG_UPTO(o.log_level));
  /* Acquire a connection to the bus, letting the library work out the details.
   * https://www.freedesktop.org/software/systemd/man/sd_bus_default.html
   */
  r = sd_bus_default(&bus);
  if (r < 0)
    return log_error(o.log_level, r, "sd_bus_default()");
  /* Publish an interface on the bus, specifying our well-known object access
   * path and public interface name.
   * https://www.freedesktop.org/software/systemd/man/sd_bus_add_object.html
   * https://dbus.freedesktop.org/doc/dbus-tutorial.html
   */
  r = sd_bus_add_object_vtable(bus, NULL,
                               "/org/freedesktop/LogControl1",
                               "org.freedesktop.LogControl1",
                               vtable,
                               &o);
  if (r < 0)
    return log_error(o.log_level, r, "sd_bus_add_object_vtable()");
  /* By default, the service is assigned an ephemeral name. Also add a fixed
   * one, so that clients know whom to call.
   * https://www.freedesktop.org/software/systemd/man/sd_bus_request_name.html
   */
  r = sd_bus_request_name(bus, "org.freedesktop.Example", 0);
  if (r < 0)
    return log_error(o.log_level, r, "sd_bus_request_name()");
  for (;;) {
    /* https://www.freedesktop.org/software/systemd/man/sd_bus_wait.html
     */
    r = sd_bus_wait(bus, UINT64_MAX);
    if (r < 0)
      return log_error(o.log_level, r, "sd_bus_wait()");
    /* https://www.freedesktop.org/software/systemd/man/sd_bus_process.html
     */
    r = sd_bus_process(bus, NULL);
    if (r < 0)
      return log_error(o.log_level, r, "sd_bus_process()");
  }
  /* https://www.freedesktop.org/software/systemd/man/sd_bus_release_name.html
   */
  r = sd_bus_release_name(bus, "org.freedesktop.Example");
  if (r < 0)
    return log_error(o.log_level, r, "sd_bus_release_name()");
  return 0;
}

این کد یک سرور ساده روی گذرگاه ایجاد می‌کند. با ارائه ویژگی‌های مورد نیاز و اجازه دادن به تنظیم موارد قابل نوشتن، رابط LogControl1 را پیاده‌سازی می‌نماید. همچنین رویدادها را در سطح لاگ پیکربندی‌شده با استفاده از sd_journal_print(3) ثبت می‌کند.

توجه داشته باشید که هنگام پورت کردن این مثال به سایر کتابخانه‌های D-Bus ممکن است افزودن بررسی‌های دستی امتیاز دسترسی کلاینت لازم باشد، زیرا آن‌ها معمولاً رفتارهای محدودکننده پیش‌فرض sd-bus را ندارند؛ در sd-bus دسترسی بدون امتیاز به ویژگی‌ها از طریق فلگ SD_BUS_VTABLE_UNPRIVILEGED کنترل می‌شود که اختیاری و انتخابی (opt-in) است نه انصرافی (opt-out).

systemd(1), journalctl(1), systemctl(1), systemd.service(5), syslog(3)

systemd 261.2