@@ -1031,6 +1031,20 @@ pub fn getgroups() -> Result<Vec<Gid>> {
10311031/// calling `setgroups()`. Apple discourages the use of `setgroups()`.
10321032///
10331033/// [Further reading](http://man7.org/linux/man-pages/man2/getgroups.2.html)
1034+ ///
1035+ /// # Examples
1036+ ///
1037+ /// `setgroups` can be used when dropping privileges from the root user to a
1038+ /// specific user and group. For example, given the user `www-data` with UID
1039+ /// `33` and the group `backup` with the GID `34`, one could switch user as
1040+ /// follows:
1041+ /// ```
1042+ /// let uid = Uid::from_raw(33);
1043+ /// let gid = Gid::from_raw(34);
1044+ /// setgroups(&[gid])?;
1045+ /// setgid(gid)?;
1046+ /// setuid(uid)?;
1047+ /// ```
10341048pub fn setgroups ( groups : & [ Gid ] ) -> Result < ( ) > {
10351049 cfg_if ! {
10361050 if #[ cfg( any( target_os = "dragonfly" ,
@@ -1123,6 +1137,22 @@ pub fn getgrouplist(user: &CString, group: Gid) -> Result<Vec<Gid>> {
11231137/// of. The additional group `group` is also added to the list.
11241138///
11251139/// [Further reading](http://man7.org/linux/man-pages/man3/initgroups.3.html)
1140+ ///
1141+ /// # Examples
1142+ ///
1143+ /// `initgroups` can be used when dropping privileges from the root user to
1144+ /// another user. For example, given the user `www-data`, we could look up the
1145+ /// UID and GID for the user in the system's password database (usually found
1146+ /// in `/etc/passwd`). If the `www-data` user's UID and GID were `33` and `33`,
1147+ /// respectively, one could switch user as follows:
1148+ /// ```
1149+ /// let user = CString::new("www-data").unwrap();
1150+ /// let uid = Uid::from_raw(33);
1151+ /// let gid = Gid::from_raw(33);
1152+ /// initgroups(&user, gid)?;
1153+ /// setgid(gid)?;
1154+ /// setuid(uid)?;
1155+ /// ```
11261156pub fn initgroups ( user : & CString , group : Gid ) -> Result < ( ) > {
11271157 cfg_if ! {
11281158 if #[ cfg( any( target_os = "ios" , target_os = "macos" ) ) ] {
0 commit comments