1) C 语言实现:
#include <gio/gio.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <fcntl.h>
#ifdef G_OS_UNIX
//#include <gio/gunixfdlist.h>
/* For STDOUT_FILENO */
#include <unistd.h>
#endif
GDBusProxy *dbus_proxy;
GMainLoop * g_main_loop;
char *dbus_name;
#define PROCESS_NAME_LENGTH 255
int sysfs_read(char *proc_file_name, char *cmdline, int len)
{
int fd = open(proc_file_name, O_RDONLY);
int n = read(fd, cmdline, len);
close(fd);
return n;
}
void
get_pid_from_dbus_name_cb(GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GError *error = NULL;
GVariant *result = NULL;
guint pid;
gchar process_name[PROCESS_NAME_LENGTH] = "";
gchar proc_path[64] = "";
int ret;
result = g_dbus_proxy_call_finish (dbus_proxy, res, &error);
if (error) {
printf("get_pid_from_dbus_name_cb failed: %s\n", error->message);
g_error_free(error);
}
else if (result) {
g_variant_get(result, "(u)", &pid);
g_variant_unref(result);
/* safety check */
if (pid != 0) {
sprintf(proc_path, "/proc/%u/cmdline", pid);
ret = sysfs_read(proc_path, process_name, PROCESS_NAME_LENGTH);
if (ret < 0)
{
printf("error reading process name from %s: %d",
proc_path, ret);
strcpy(process_name, "UNKNOWN");
}
//g_debug("PID: %u, Process Name: %s", pid, process_name);
printf("PID: %u, Process Name: %s\n", pid, process_name);
}
else {
/* not sure this can happen */
printf("unable to get pid info\n");
}
}
g_main_loop_quit(g_main_loop);
exit(0);
}
void
dbus_proxy_connect_cb(GObject *source_object,
GAsyncResult *res,
gpointer user_data)
{
GError *error = NULL;
dbus_proxy = g_dbus_proxy_new_finish (res, &error);
if (error) {
g_warning("dbus_proxy_connect_cb failed: %s", error->message);
g_error_free(error);
dbus_proxy = NULL;
}
else {
g_debug("dbus_proxy_connect_cb succeeded");
g_dbus_proxy_call(dbus_proxy,
"GetConnectionUnixProcessID",
g_variant_new("(s)", dbus_name),
G_DBUS_CALL_FLAGS_NONE,
-1,
NULL,
(GAsyncReadyCallback)get_pid_from_dbus_name_cb,
NULL);
}
}
int main (int argc, char **argv)
{
GDBusConnection *bcon;
GMainLoop *loop;
if(argc != 2)
{
return -1;
}
dbus_name = argv[1];
g_type_init ();
// bcon = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
NULL,
"org.freedesktop.DBus",
"/org/freedesktop/DBus",
"org.freedesktop.DBus",
NULL,
(GAsyncReadyCallback)dbus_proxy_connect_cb,
NULL);
//g_dbus_proxy_call (bproxy, "HelloWorld", g_variant_new ("(s)", "Wow"), G_DBUS_CALL_FLAGS_NONE, -1, NULL, (GAsyncReadyCallback) mycallback, NULL);
g_main_loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(g_main_loop);
return 0;
}
编译:
gcc gdbus-get-pid.c `pkg-config --cflags --libs gio-2.0 gio-unix-2.0` -g -o gdbus-get-pid
运行方式:
$ ./gdbus-get-pid :1.6
PID: 953, Process Name: /lib/systemd/systemd-logind
./gdbus-get-pid org.freedesktop.UDisks2
PID: 2887, Process Name: /usr/lib/udisks2/udisksd
输入可以是 DBUS连接的 unique name,或者是 well-known name.
2. 使用 dbus-send 命令:
e$ dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionUnixProcessID string::1.6
method return sender=org.freedesktop.DBus -> dest=:1.166 reply_serial=2
uint32 2517
$ dbus-send --system --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionUnixProcessID string::1.6
method return sender=org.freedesktop.DBus -> dest=:1.113 reply_serial=2
uint32 953
$ dbus-send --system --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetConnectionUnixProcessID string:org.freedesktop.UDisks2
method return sender=org.freedesktop.DBus -> dest=:1.114 reply_serial=2
uint32 2887
3. python代码:
#!/usr/bin/python
import dbus
import sys
bus=dbus.SystemBus().get_object('org.freedesktop.DBus', '/org/freedesktop/DBus');
print dbus.Interface(bus, 'org.freedesktop.DBus').GetConnectionUnixProcessID(sys.argv[1])
References:
1. http://www.mattfischer.com/blog/?p=494