Hi All,<br><br>I have made it worked after seteuid in the program and export DBUS_SESSION_BUS_ADDRESS as the plain user's.<br><br>I don't know why but it did work, thanks all of you a lot.<br><br><div>--<br>
<div> Best Regards,<br>
<br></div>
</div><br>在2010-05-13 10:36:51,danny <tornadory@163.com> 写道:<br> <blockquote id="isReplyContent" style="border-left: 1px solid rgb(204, 204, 204); margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">Hi All,<br><br>I test, but unfortunately it failed.<br><br>Below is my test program, I thought the g_volume_monitor_get() need user's info, was it wrong?<br><br>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.<br><br>But when run it as root, it will show nothing.<br><br>/*<br>* gcc -o test test.c `pkg-config --libs --cflags gtk+-2.0`<br>* Usage: ./test<br>*<br>*/<br><br>#include <gio/gio.h><br>#include <gtk/gtk.h><br>#include <string.h><br>#include <unistd.h><br>#include <errno.h><br><br>static void print_drive_info(GDrive *drive)<br>{<br> gchar *name;<br><br> name = g_drive_get_name(drive);<br> <br> g_print("\tname: %s\n",<br> name ? name : "(null)");<br><br> g_free(name);<br>}<br><br>static void on_drive_changed(GVolumeMonitor *volume_monitor, GDrive *drive)<br>{<br> g_print("Drive changed:\n");<br> print_drive_info(drive);<br>}<br><br>static void on_drive_connected(GVolumeMonitor *volume_monitor, GDrive *drive)<br>{<br> g_print("Drive connected:\n");<br> print_drive_info(drive);<br>}<br><br>static void on_drive_disconnected(GVolumeMonitor *volume_monitor, GDrive *drive)<br>{<br> g_print("Drive disconnected:\n");<br> print_drive_info(drive);<br>}<br><br>static void on_refresh_clicked(GtkButton *button, Context ctx)<br>{<br> refresh_drive_list(ctx);<br>}<br><br>int main(int argc, char *argv[])<br>{<br> GVolumeMonitor *monitor;<br><br> gtk_init(&argc, &argv);<br><br> /*Just for test , I set the uid euid directly to 500 which is my own user id */<br> int old_uid = getuid();<br> int old_euid = geteuid();<br> printf("uid is %d \n euid is %d \n",getuid(), geteuid() );<br> if (setuid(500) == -1)<br> //if (seteuid(500) == -1)<br> {<br> printf ("seteuid : %s \n", strerror(errno));<br> return -1;<br> }<br> printf("uid is %d \n euid is %d \n",getuid(), geteuid() );<br><br> monitor = g_volume_monitor_get(); //the main entry I get information<br> g_signal_connect(monitor, "drive-changed", G_CALLBACK(on_drive_changed), NULL);<br> g_signal_connect(monitor, "drive-connected", G_CALLBACK(on_drive_connected), NULL);<br> g_signal_connect(monitor, "drive-disconnected", G_CALLBACK(on_drive_disconnected), NULL);<br> <br> gtk_main();<br><br> return 0;<br>}<br><br><div>--<br>
<div> Best Regards,<br>
<br></div>
</div><br>在2010-05-13 09:48:16,danny <<a href="mailto:tornadory@163.com">tornadory@163.com</a>> 写道:<br> <blockquote id="isReplyContent" style="border-left: 1px solid rgb(204, 204, 204); margin: 0px 0px 0px 0.8ex; padding-left: 1ex;">Thanks all of you, I'll try it now.<br><br>Thanks again.<br><br><br><div>--<br>
<div> Best Regards,<br>
<br></div>
</div><br><pre><br>在2010-05-13 00:26:45,"Thiago Macieira" <<a href="mailto:thiago@kde.org">thiago@kde.org</a>> 写道:<br>>Em Quarta-feira 12 Maio 2010, às 18:02:10, Rémi Denis-Courmont escreveu:<br>>> Le mercredi 12 mai 2010 14:04:40 Thiago Macieira, vous avez écrit :<br>>> > Em Quarta-feira 12. Maio 2010, às 12.13.06, danny escreveu:<br>>> > > Dear All,<br>>> > > <br>>> > > I have a program run as root, but I need get information from GVFS<br>>> > > which is on the session bus, and it shows "failed to connect the<br>>> > > session bus" while I run the test program, and it could not get any<br>>> > > information from the GVFS.<br>>> > > <br>>> > > Could I access the session bus in my "root" program?<br>>> > <br>>> > No.<br>>> > <br>>> > The user's session bus requires the user's UID. Your program needs to<br>>> > drop privileges before connecting.<br>>> <br>>> I think, in principles, the root process could just set its effective UID<br>>> to the user, while establishing the DBus connection, then back to root. It<br>>> does not need to drop the root privileges.<br>><br>>Well, setting the EUID is dropping the privileges, even if you can still take <br>>them back later :-)<br>><br>>-- <br>>Thiago Macieira - thiago (AT) macieira.info - thiago (AT) kde.org<br>> Senior Product Manager - Nokia, Qt Development Frameworks<br>> PGP/GPG: 0x6EF45358; fingerprint:<br>> E067 918B B660 DBD1 105C 966C 33F5 F005 6EF4 5358<br></pre><br><br><span title="neteasefooter"><hr>
<a href="http://ym.163.com/?from=od3" target="_blank">网易为中小企业免费提供企业邮箱(自主域名)</a>
</span></blockquote><br><br><span title="neteasefooter"></span></blockquote><br><br><span title="neteasefooter"/></span>