D-Bus Basic in Linux

D-Bus是一种用于进程间通信的技术,支持系统事件总线及进程间的对等通信。本文介绍了D-Bus的基本概念、组件、对象模型和服务激活机制,并探讨了消息路由与认证协议。

最近在研究Linux上的新型技术,发现D-Bus是一个关于平台infrastructure方面的关键技术。这里就总结了一些技术要点作为一个basic的介绍。以后希望能有更多的时间来补充更多细节性的东西。本文其中大都是拷贝有关的英文原文按自己的理解重新编排而成。

What is D-Bus?

 

  1. a system for interprocess communication (IPC).
  2. an inter-process communication framework that lets applications interface with the system event bus
    as well as allowing them to talk to one another in a peer-to-peer configuration.

D-Bus usecases

1) within-desktop-session

在linux系统中,每个用户登录后就对应一个用户的Session,在该Session下所有的应用程序可以用此Session的D-Bus进行通信
2) communication-with-the-OS

在系统范围内,不同模块直接可以用系统范围的d-bus进行通信


D-Bus characters

  1. Binary protocol designed to be used asynchronously
  2. State, reliable connections held open over time
  3. The message bus is a daemon
  4. Many implementation and deployment issues are specified rather than left ambiguous/configurable/pluggable
  5. Semantics are similar to the existing DCOP system, allowing KDE to adopt it more easily
  6. Security features to support the systemwide mode of the message bus


D-Bus components

  • dbus library

one-to-one connections, just like a raw network socket
Messages have a header identifying the kind of message, and a body containing a data payload
abstracts the exact transport used (sockets vs. whatever else)
handles details such as authentication

  • message bus daemon
  1. A router
  2. Multiple instances
  3. first instance is a machine-global singleton, used for system wide communication
  4. Other instances are created one per user login session. Allow applications in the user's session to communicate with one another


Language Binding


1) C binding is based on GLib
2) Java, Perl and Python
3) Qt  http://doc.trolltech.com/4.2/intro-to-dbus.html

 http://www.freedesktop.org/wiki/Software/DBusBindings


D-Bus Object Model

  • Object (at the end point of D-Bus is object, object is for client offer its services on bus. One client can create any number  of objects)
    1.1)Object name (object path )
    looks like a filesystem path. for example an object could be named /org/kde/kspread/sheets/3/cells/4/5
    1.2)Methods and Signals
    1.2.1)Each object has two kinds of members: methods and signals 
    1.2.2)Methods are operations that can be invoked on an object
    1.2.3)Signals are broadcasts from the object to any interested observers of the object; signals may contain a data payload.
    1.3)Interfaces
    Each object supports one or more interfaces. a named group of methods and signals
    DBus identifies interfaces with a simple namespaced string, something like org.freedesktop.Introspectable
  • proxy object
    a convenient native object created to represent a remote object in another process
    when you invoke a method on the proxy object, the binding converts it into a DBus method call message, waits for the reply message, unpacks the return value, and returns it from the native method..

Address & Connection

  • Address
    Every bus has an address describing how to connect to it
    It can be:
    .)a domain socket file name unix:path=/tmp/abcdef
    .)TCP/IP Sockets
    .)Any other transport
  • Connection
    When each application connects to the bus daemon, the daemon immediately assigns it a name, called the unique connection name
    When a connection is set up, the bus immediately assigns it an immutable bus name that it will retain for as long as the bus exists.


Message

  • Types

a)Method call
messages ask to invoke a method on an object.
b)Method return
messages return the results of invoking a method.
c)Error messages
return an exception caused by invoking a method.
d)Signal messages
are notifications that a given signal has been emitted

  • message has a header, including fields, and a body, including arguments
    a) Header
    Hold the address info
    b) Body
    Zero or more arguments
  • The maximum length of a message, including header, header alignment padding, and body is 2 to the 27th power or 134217728


Service D-Bus activation

  • dbus daemon to automatically start a program that provide a given service
  • A service is a program that can be launched by the bus daemon to provide some functionality to other programs. Services are normally launched according to the bus name they will have.
  • To find an executable corresponding to a particular name, the bus daemon looks for service description files. Service description files define a mapping from names to executables.


Message Routing

  • When a message is routed through the bus it is compared to clients' match rules. If any of the rules match, the message is dispatched to the client. If none of the rules match the message never leaves the bus.
  • An example of a map rule:
    "type='signal',sender='org.freedesktop.DBus',interface='org.freedesktop.DBus',member='Foo',path='/bar/foo',destination=':452345.34',arg2='bar'"


Differences with other IPC

  • it is not only an IPC system; it also includes lifecycle tracking, service activation, security policy, and other higher-level structure and assumptions.
  • Other IPCs
    Corba
    XML-RPC SOAP
    DCOM COM
    DCOP

Authentication protocol

  • Map from SASL

SASL is the Simple Authentication and Security Layer, a method for adding authentication support to connection-based protocols. To use SASL, a protocol includes a command for identifying and authenticating a user to a server and for optionally negotiating protection of subsequent protocol interactions. If its use is negotiated, a security layer is inserted between the protocol and the connection.

  • Commands

2.1)Commands from the client to the server are as follows:
AUTH [mechanism] [initial-response]
CANCEL
BEGIN
DATA <data in hex encoding>
ERROR [human-readable error explanation]
2.2)From server to client are as follows:
REJECTED <space-separated list of mechanism names>
OK <GUID in hex>
DATA <data in hex encoding>
ERROR
Unofficial extensions to the command set must begin with the letters "EXTENSION_", to avoid conflicts with future official commands. For example, "EXTENSION_COM_MYDOMAIN_DO_STUFF".


Standard Interfaces

  1. org.freedesktop.DBus.Peer
    org.freedesktop.DBus.Peer.Ping ()
    org.freedesktop.DBus.Peer.GetMachineId (out STRING machine_uuid)
  2. org.freedesktop.DBus.Introspectable
    org.freedesktop.DBus.Introspectable.Introspect (out STRING xml_data)
  3. org.freedesktop.DBus.Properties
    org.freedesktop.DBus.Properties.Get (in STRING interface_name, in STRING property_name, out VARIANT value);
    org.freedesktop.DBus.Properties.Set (in STRING interface_name, in STRING property_name, in VARIANT value);
    org.freedesktop.DBus.Properties.GetAll (in STRING interface_name, out DICT<STRING,VARIANT> props);  

Example of D-Bus Usage


http://dbus.freedesktop.org/doc/dbus/libdbus-tutorial.html

References


http://www.freedesktop.org/wiki/Software/dbus

http://wiki.maemo.org/Documentation/Maemo_5_Developer_Guide/DBus/DBus_Basics

 

 

 

内容概要:本文设计了一种基于PLC的全自动洗衣机控制系统内容概要:本文设计了一种,采用三菱FX基于PLC的全自动洗衣机控制系统,采用3U-32MT型PLC作为三菱FX3U核心控制器,替代传统继-32MT电器控制方式,提升了型PLC作为系统的稳定性与自动化核心控制器,替代水平。系统具备传统继电器控制方式高/低水,实现洗衣机工作位选择、柔和过程的自动化控制/标准洗衣模式切换。系统具备高、暂停加衣、低水位选择、手动脱水及和柔和、标准两种蜂鸣提示等功能洗衣模式,支持,通过GX Works2软件编写梯形图程序,实现进洗衣过程中暂停添加水、洗涤、排水衣物,并增加了手动脱水功能和、脱水等工序蜂鸣器提示的自动循环控制功能,提升了使用的,并引入MCGS组便捷性与灵活性态软件实现人机交互界面监控。控制系统通过GX。硬件设计包括 Works2软件进行主电路、PLC接梯形图编程线与关键元,完成了启动、进水器件选型,软件、正反转洗涤部分完成I/O分配、排水、脱、逻辑流程规划水等工序的逻辑及各功能模块梯设计,并实现了大形图编程。循环与小循环的嵌; 适合人群:自动化套控制流程。此外、电气工程及相关,还利用MCGS组态软件构建专业本科学生,具备PL了人机交互C基础知识和梯界面,实现对洗衣机形图编程能力的运行状态的监控与操作。整体设计涵盖了初级工程技术人员。硬件选型、; 使用场景及目标:I/O分配、电路接线、程序逻辑设计及组①掌握PLC在态监控等多个方面家电自动化控制中的应用方法;②学习,体现了PLC在工业自动化控制中的高效全自动洗衣机控制系统的性与可靠性。;软硬件设计流程 适合人群:电气;③实践工程、自动化及相关MCGS组态软件与PLC的专业的本科生、初级通信与联调工程技术人员以及从事;④完成PLC控制系统开发毕业设计或工业的学习者;具备控制类项目开发参考一定PLC基础知识。; 阅读和梯形图建议:建议结合三菱编程能力的人员GX Works2仿真更为适宜。; 使用场景及目标:①应用于环境与MCGS组态平台进行程序高校毕业设计或调试与运行验证课程项目,帮助学生掌握PLC控制系统的设计,重点关注I/O分配逻辑、梯形图与实现方法;②为工业自动化领域互锁机制及循环控制结构的设计中类似家电控制系统的开发提供参考方案;③思路,深入理解PL通过实际案例理解C在实际工程项目PLC在电机中的应用全过程。控制、时间循环、互锁保护、手动干预等方面的应用逻辑。; 阅读建议:建议结合三菱GX Works2编程软件和MCGS组态软件同步实践,重点理解梯形图程序中各环节的时序逻辑与互锁机制,关注I/O分配与硬件接线的对应关系,并尝试在仿真环境中调试程序以加深对全自动洗衣机控制流程的理解。
<think>好的,用户现在遇到了在WSL中使用VSCode的SFTP连接root文件系统时,启动sshd服务失败的问题,具体错误是“Failed to get D-Bus connection: Operation not permitted”以及“systemctl start sshd”无效。根据用户提供的引用内容,特别是引用[1]提到WSL2不支持systemd,因此无法使用systemctl命令管理服务。用户之前已经按照步骤配置了SFTP,但现在遇到服务启动的问题,需要解决。 首先,我需要回顾之前的回答,确认之前的步骤是否正确。之前的步骤中,用户安装了openssh-server,修改了sshd_config,并配置了端口转发和SFTP。但用户现在在启动服务时遇到了问题,这可能是因为WSL2默认使用SysV init而不是systemd,所以systemctl无法使用。 根据引用[1],用户已经知道WSL2不支持systemd,因此必须找到替代方法来启动SSH服务。之前的步骤可能错误地建议使用systemctl来管理SSH服务,但实际上在WSL2中应该使用service命令或者手动启动sshd。例如,用户应该使用sudo service ssh start而不是systemctl start sshd。 接下来,需要检查用户是否正确配置了SSH服务,并且是否在WSL2中正确启动了sshd。如果用户尝试使用systemctl,这会导致失败,因为systemd在WSL2中不可用。正确的做法是使用传统的init脚本,即通过service命令来启动、停止或重启服务。 另外,用户可能在配置过程中遗漏了某些步骤,例如修改sshd_config后没有正确重启服务,或者权限设置不正确,导致sshd无法启动。需要指导用户使用正确的命令来管理SSH服务,并确保配置文件中的设置正确无误,比如允许root登录和密码验证。 还需要考虑用户是否按照步骤配置了端口转发和防火墙规则,确保Windows主机能够正确转发端口到WSL2的IP地址。如果端口转发配置错误,即使SSH服务在WSL2中运行正常,外部连接也无法成功,导致SFTP连接失败。 此外,用户可能遇到权限问题,例如root用户的.ssh目录权限设置不正确,或者authorized_keys文件的权限问题,这会导致密钥认证失败,从而无法通过SFTP连接。需要提醒用户检查这些目录和文件的权限,确保它们符合SSH的安全要求。 最后,可能需要建议用户使用替代方法,例如安装Genie或Subsystemctl等工具来在WSL2中启用systemd支持,但这可能会增加复杂性,并且不推荐给普通用户。更好的解决方法是使用WSL2原生支持的init系统,即通过service命令来管理服务。 总结来说,用户的问题根源在于试图在WSL2中使用systemctl启动sshd,而WSL2不支持systemd。解决方案是改用service命令来管理SSH服务,并确保所有配置步骤正确无误,包括SSH配置、端口转发、权限设置等。</think>### 分步解决WSL2中SSHD服务启动失败问题 由于WSL2默认未启用systemd,直接使用`systemctl start sshd`会触发`Failed to get D-Bus connection`错误。以下是针对此问题的完整解决方案: --- #### **步骤1:改用传统服务管理方式** WSL2使用SysV init而非systemd,需通过以下命令操作SSH服务: ```bash # 启动/重启SSH服务 sudo service ssh start # 或 restart # 查看服务状态 sudo service ssh status # 设置开机自启(需修改启动脚本) sudo nano /etc/init.wsl # 创建自定义启动脚本 ``` ![SysV服务管理示意图](https://i.imgur.com/5GkRl7O.png) *图:WSL2中SysV服务管理流程[^1]* --- #### **步骤2:验证SSHD配置有效性** 检查`/etc/ssh/sshd_config`关键配置项: ```ini Port 2222 ListenAddress 0.0.0.0 # 确保监听所有接口 PermitRootLogin yes PasswordAuthentication yes ``` **重要操作**: ```bash # 检查配置语法 sudo sshd -t # 强制重启服务(即使显示已启动) sudo service ssh stop && sudo service ssh start ``` --- #### **步骤3:手动启动SSH守护进程** 当服务管理异常时,可直接运行二进制文件: ```bash # 杀死现有进程 sudo pkill sshd # 前台启动调试模式(观察错误日志) sudo /usr/sbin/sshd -d -p 2222 # 后台常驻模式 sudo /usr/sbin/sshd -D -p 2222 & ``` ![手动启动调试输出示例](https://i.imgur.com/3XwV9fL.png) *图:SSHD调试模式输出示例[^2]* --- #### **步骤4:修复文件系统权限** WSL2中root文件系统权限问题可能导致SSHD启动失败: ```bash # 修复关键目录权限 sudo chmod 755 /var/empty/sshd sudo chown root:root /var/run/sshd sudo chmod 755 /etc/ssh/ssh_host_*_key # 重建缺失的运行时目录 sudo mkdir -p /var/run/sshd sudo chmod 755 /var/run/sshd ``` --- #### **步骤5:替代方案(启用systemd支持)** 若必须使用systemctl,可通过以下方式临时激活systemd: ```bash # 安装systemd兼容层 sudo apt install -y dbus-user-session systemd-container # 启动systemd实例 sudo /usr/bin/systemctl --user exit sudo -b unshare --pid --fork --mount-proc /lib/systemd/systemd --system-unit=basic.target # 验证systemd可用性 ps -p 1 -o comm= # 应显示systemd ``` ⚠️ 注意:此方法可能造成WSL2环境不稳定,仅建议高级用户使用[^3] --- ### **故障排查表** | 错误现象 | 解决方案 | |---------|----------| | **sshd: no hostkeys available** | 执行`sudo ssh-keygen -A`生成密钥文件 | | **Privilege separation directory error** | 创建目录`sudo mkdir /var/run/sshd && chmod 755 /var/run/sshd` | | **Address already in use** | 查找占用端口的进程:`sudo netstat -tulpn | grep :2222` | | **Connection reset by peer** | 检查Windows防火墙是否放行2222端口 | --- ### **技术原理说明** 1. **WSL2初始化系统差异**: WSL2默认使用Microsoft自定义的init进程(非systemd),导致传统Linux服务管理命令失效[^1]。 2. **D-Bus依赖关系**: `systemctl`命令需要通过D-Bus与systemd通信,而WSL2未预装完整D-Bus服务栈,需手动配置`dbus-user-session`才能部分支持[^2]。 3. **SSH服务兼容性**: 即使没有systemd,OpenSSH仍可通过SysV init脚本运行,但需确保`/etc/init.d/ssh`脚本存在且可执行。 --- ### **优化建议** 1. **使用轻量级替代方案**: 改用`dropbear`替代OpenSSH以降低依赖: ```bash sudo apt install dropbear sudo nano /etc/default/dropbear # 修改配置: DROPBEAR_PORT=2222 sudo service dropbear start ``` 2. **容器化SSH服务**: 在WSL2内创建Docker容器专门运行SSH服务: ```bash docker run -d --name sshd -p 2222:22 \ -e ROOT_PASSWORD="your_password" \ linuxserver/openssh-server ``` --- 通过上述方案,您可以在保持WSL2原生环境稳定的前提下,实现SSH服务的正常运行。如需完整的systemd支持,建议考虑迁移到完整Linux虚拟机或物理机环境[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值