Could I connect user's session bus from root?

danny tornadory at 163.com
Wed May 12 19:36:51 PDT 2010


Hi All,

I test, but unfortunately it failed.

Below is my test program,  I thought the g_volume_monitor_get() need user's info, was it wrong?

Normally while I run "./test" , it will monitor the removable device and show the drive's name while some devices such as USB type connected or disconnected.

But when run it as root, it will show nothing.

/*
*   gcc -o test test.c `pkg-config --libs --cflags gtk+-2.0`
*   Usage:  ./test
*
*/

#include <gio/gio.h>
#include <gtk/gtk.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

static void print_drive_info(GDrive *drive)
{
    gchar *name;

    name = g_drive_get_name(drive);
  
    g_print("\tname: %s\n",
        name ? name : "(null)");

    g_free(name);
}

static void on_drive_changed(GVolumeMonitor *volume_monitor, GDrive *drive)
{
    g_print("Drive changed:\n");
    print_drive_info(drive);
}

static void on_drive_connected(GVolumeMonitor *volume_monitor, GDrive *drive)
{
    g_print("Drive connected:\n");
    print_drive_info(drive);
}

static void on_drive_disconnected(GVolumeMonitor *volume_monitor, GDrive *drive)
{
    g_print("Drive disconnected:\n");
    print_drive_info(drive);
}

static void on_refresh_clicked(GtkButton *button, Context ctx)
{
    refresh_drive_list(ctx);
}

int main(int argc, char *argv[])
{
    GVolumeMonitor *monitor;

    gtk_init(&argc, &argv);

    /*Just for test , I set the uid euid directly to 500 which is my own user id */
    int old_uid = getuid();
    int old_euid = geteuid();
    printf("uid is %d \n euid is %d \n",getuid(), geteuid() );
    if (setuid(500) == -1)
    //if (seteuid(500) == -1)
    {
    printf ("seteuid : %s \n", strerror(errno));
    return -1;
    }
    printf("uid is %d \n euid is %d \n",getuid(), geteuid() );

    monitor = g_volume_monitor_get();   //the main entry I get information
    g_signal_connect(monitor, "drive-changed", G_CALLBACK(on_drive_changed), NULL);
    g_signal_connect(monitor, "drive-connected", G_CALLBACK(on_drive_connected), NULL);
    g_signal_connect(monitor, "drive-disconnected", G_CALLBACK(on_drive_disconnected), NULL);
 
    gtk_main();

    return 0;
}


--

 Best Regards,



在2010-05-13 09:48:16,danny <tornadory at 163.com> 写道:
Thanks all of you, I'll try it now.

Thanks again.



--

 Best Regards,





在2010-05-13 00:26:45,"Thiago Macieira" <thiago at kde.org> 写道:
>Em Quarta-feira 12 Maio 2010, às 18:02:10, Rémi Denis-Courmont escreveu:
>> Le mercredi 12 mai 2010 14:04:40 Thiago Macieira, vous avez écrit :
>> > Em Quarta-feira 12. Maio 2010, às 12.13.06, danny escreveu:
>> > > Dear All,
>> > > 
>> > > I have a program run as root, but I need get information from GVFS
>> > > which is on the session bus, and it shows "failed to connect the
>> > > session bus" while I run the test program, and it could not get any
>> > > information from the GVFS.
>> > > 
>> > > Could I access the session bus in my "root" program?
>> > 
>> > No.
>> > 
>> > The user's session bus requires the user's UID. Your program needs to
>> > drop privileges before connecting.
>> 
>> I think, in principles, the root process could just set its effective UID
>> to the user, while establishing the DBus connection, then back to root. It
>> does not need to drop the root privileges.
>
>Well, setting the EUID is dropping the privileges, even if you can still take 
>them back later :-)
>
>-- 
>Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org
>  Senior Product Manager - Nokia, Qt Development Frameworks
>      PGP/GPG: 0x6EF45358; fingerprint:
>      E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358




网易为中小企业免费提供企业邮箱(自主域名)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freedesktop.org/archives/dbus/attachments/20100513/9b78955c/attachment.htm>


More information about the dbus mailing list