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>&nbsp;Best Regards,<br>
<br></div>
</div><br>在2010-05-13 10:36:51,danny&nbsp;&lt;tornadory@163.com&gt; 写道:<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,&nbsp; 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>*&nbsp;&nbsp; gcc -o test test.c `pkg-config --libs --cflags gtk+-2.0`<br>*&nbsp;&nbsp; Usage:&nbsp; ./test<br>*<br>*/<br><br>#include &lt;gio/gio.h&gt;<br>#include &lt;gtk/gtk.h&gt;<br>#include &lt;string.h&gt;<br>#include &lt;unistd.h&gt;<br>#include &lt;errno.h&gt;<br><br>static void print_drive_info(GDrive *drive)<br>{<br>&nbsp;&nbsp;&nbsp; gchar *name;<br><br>&nbsp;&nbsp;&nbsp; name = g_drive_get_name(drive);<br>&nbsp; <br>&nbsp;&nbsp;&nbsp; g_print("\tname: %s\n",<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; name ? name : "(null)");<br><br>&nbsp;&nbsp;&nbsp; g_free(name);<br>}<br><br>static void on_drive_changed(GVolumeMonitor *volume_monitor, GDrive *drive)<br>{<br>&nbsp;&nbsp;&nbsp; g_print("Drive changed:\n");<br>&nbsp;&nbsp;&nbsp; print_drive_info(drive);<br>}<br><br>static void on_drive_connected(GVolumeMonitor *volume_monitor, GDrive *drive)<br>{<br>&nbsp;&nbsp;&nbsp; g_print("Drive connected:\n");<br>&nbsp;&nbsp;&nbsp; print_drive_info(drive);<br>}<br><br>static void on_drive_disconnected(GVolumeMonitor *volume_monitor, GDrive *drive)<br>{<br>&nbsp;&nbsp;&nbsp; g_print("Drive disconnected:\n");<br>&nbsp;&nbsp;&nbsp; print_drive_info(drive);<br>}<br><br>static void on_refresh_clicked(GtkButton *button, Context ctx)<br>{<br>&nbsp;&nbsp;&nbsp; refresh_drive_list(ctx);<br>}<br><br>int main(int argc, char *argv[])<br>{<br>&nbsp;&nbsp;&nbsp; GVolumeMonitor *monitor;<br><br>&nbsp;&nbsp;&nbsp; gtk_init(&amp;argc, &amp;argv);<br><br>&nbsp;&nbsp;&nbsp; /*Just for test , I set the uid euid directly to 500 which is my own user id */<br>&nbsp;&nbsp;&nbsp; int old_uid = getuid();<br>&nbsp;&nbsp;&nbsp; int old_euid = geteuid();<br>&nbsp;&nbsp;&nbsp; printf("uid is %d \n euid is %d \n",getuid(), geteuid() );<br>&nbsp;&nbsp;&nbsp; if (setuid(500) == -1)<br>&nbsp;&nbsp;&nbsp; //if (seteuid(500) == -1)<br>&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp; printf ("seteuid : %s \n", strerror(errno));<br>&nbsp;&nbsp;&nbsp; return -1;<br>&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp;&nbsp; printf("uid is %d \n euid is %d \n",getuid(), geteuid() );<br><br>&nbsp;&nbsp;&nbsp; monitor = g_volume_monitor_get();&nbsp;&nbsp; //the main entry I get information<br>&nbsp;&nbsp;&nbsp; g_signal_connect(monitor, "drive-changed", G_CALLBACK(on_drive_changed), NULL);<br>&nbsp;&nbsp;&nbsp; g_signal_connect(monitor, "drive-connected", G_CALLBACK(on_drive_connected), NULL);<br>&nbsp;&nbsp;&nbsp; g_signal_connect(monitor, "drive-disconnected", G_CALLBACK(on_drive_disconnected), NULL);<br>&nbsp;<br>&nbsp;&nbsp;&nbsp; gtk_main();<br><br>&nbsp;&nbsp;&nbsp; return 0;<br>}<br><br><div>--<br>
<div>&nbsp;Best Regards,<br>
<br></div>
</div><br>在2010-05-13 09:48:16,danny&nbsp;&lt;<a href="mailto:tornadory@163.com">tornadory@163.com</a>&gt; 写道:<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>&nbsp;Best Regards,<br>
<br></div>
</div><br><pre><br>在2010-05-13&nbsp;00:26:45,"Thiago&nbsp;Macieira"&nbsp;&lt;<a href="mailto:thiago@kde.org">thiago@kde.org</a>&gt;&nbsp;写道:<br>&gt;Em&nbsp;Quarta-feira&nbsp;12&nbsp;Maio&nbsp;2010,&nbsp;às&nbsp;18:02:10,&nbsp;Rémi&nbsp;Denis-Courmont&nbsp;escreveu:<br>&gt;&gt;&nbsp;Le&nbsp;mercredi&nbsp;12&nbsp;mai&nbsp;2010&nbsp;14:04:40&nbsp;Thiago&nbsp;Macieira,&nbsp;vous&nbsp;avez&nbsp;écrit&nbsp;:<br>&gt;&gt;&nbsp;&gt;&nbsp;Em&nbsp;Quarta-feira&nbsp;12.&nbsp;Maio&nbsp;2010,&nbsp;às&nbsp;12.13.06,&nbsp;danny&nbsp;escreveu:<br>&gt;&gt;&nbsp;&gt;&nbsp;&gt;&nbsp;Dear&nbsp;All,<br>&gt;&gt;&nbsp;&gt;&nbsp;&gt;&nbsp;<br>&gt;&gt;&nbsp;&gt;&nbsp;&gt;&nbsp;I&nbsp;have&nbsp;a&nbsp;program&nbsp;run&nbsp;as&nbsp;root,&nbsp;but&nbsp;I&nbsp;need&nbsp;get&nbsp;information&nbsp;from&nbsp;GVFS<br>&gt;&gt;&nbsp;&gt;&nbsp;&gt;&nbsp;which&nbsp;is&nbsp;on&nbsp;the&nbsp;session&nbsp;bus,&nbsp;and&nbsp;it&nbsp;shows&nbsp;"failed&nbsp;to&nbsp;connect&nbsp;the<br>&gt;&gt;&nbsp;&gt;&nbsp;&gt;&nbsp;session&nbsp;bus"&nbsp;while&nbsp;I&nbsp;run&nbsp;the&nbsp;test&nbsp;program,&nbsp;and&nbsp;it&nbsp;could&nbsp;not&nbsp;get&nbsp;any<br>&gt;&gt;&nbsp;&gt;&nbsp;&gt;&nbsp;information&nbsp;from&nbsp;the&nbsp;GVFS.<br>&gt;&gt;&nbsp;&gt;&nbsp;&gt;&nbsp;<br>&gt;&gt;&nbsp;&gt;&nbsp;&gt;&nbsp;Could&nbsp;I&nbsp;access&nbsp;the&nbsp;session&nbsp;bus&nbsp;in&nbsp;my&nbsp;"root"&nbsp;program?<br>&gt;&gt;&nbsp;&gt;&nbsp;<br>&gt;&gt;&nbsp;&gt;&nbsp;No.<br>&gt;&gt;&nbsp;&gt;&nbsp;<br>&gt;&gt;&nbsp;&gt;&nbsp;The&nbsp;user's&nbsp;session&nbsp;bus&nbsp;requires&nbsp;the&nbsp;user's&nbsp;UID.&nbsp;Your&nbsp;program&nbsp;needs&nbsp;to<br>&gt;&gt;&nbsp;&gt;&nbsp;drop&nbsp;privileges&nbsp;before&nbsp;connecting.<br>&gt;&gt;&nbsp;<br>&gt;&gt;&nbsp;I&nbsp;think,&nbsp;in&nbsp;principles,&nbsp;the&nbsp;root&nbsp;process&nbsp;could&nbsp;just&nbsp;set&nbsp;its&nbsp;effective&nbsp;UID<br>&gt;&gt;&nbsp;to&nbsp;the&nbsp;user,&nbsp;while&nbsp;establishing&nbsp;the&nbsp;DBus&nbsp;connection,&nbsp;then&nbsp;back&nbsp;to&nbsp;root.&nbsp;It<br>&gt;&gt;&nbsp;does&nbsp;not&nbsp;need&nbsp;to&nbsp;drop&nbsp;the&nbsp;root&nbsp;privileges.<br>&gt;<br>&gt;Well,&nbsp;setting&nbsp;the&nbsp;EUID&nbsp;is&nbsp;dropping&nbsp;the&nbsp;privileges,&nbsp;even&nbsp;if&nbsp;you&nbsp;can&nbsp;still&nbsp;take&nbsp;<br>&gt;them&nbsp;back&nbsp;later&nbsp;:-)<br>&gt;<br>&gt;--&nbsp;<br>&gt;Thiago&nbsp;Macieira&nbsp;-&nbsp;thiago&nbsp;(AT)&nbsp;macieira.info&nbsp;-&nbsp;thiago&nbsp;(AT)&nbsp;kde.org<br>&gt;&nbsp;&nbsp;Senior&nbsp;Product&nbsp;Manager&nbsp;-&nbsp;Nokia,&nbsp;Qt&nbsp;Development&nbsp;Frameworks<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PGP/GPG:&nbsp;0x6EF45358;&nbsp;fingerprint:<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;E067&nbsp;918B&nbsp;B660&nbsp;DBD1&nbsp;105C&nbsp;&nbsp;966C&nbsp;33F5&nbsp;F005&nbsp;6EF4&nbsp;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>