@@ -1031,6 +1031,20 @@ pub fn getgroups() -> Result<Vec<Gid>> {
1031
1031
/// calling `setgroups()`. Apple discourages the use of `setgroups()`.
1032
1032
///
1033
1033
/// [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
+ /// ```
1034
1048
pub fn setgroups ( groups : & [ Gid ] ) -> Result < ( ) > {
1035
1049
cfg_if ! {
1036
1050
if #[ cfg( any( target_os = "dragonfly" ,
@@ -1123,6 +1137,22 @@ pub fn getgrouplist(user: &CString, group: Gid) -> Result<Vec<Gid>> {
1123
1137
/// of. The additional group `group` is also added to the list.
1124
1138
///
1125
1139
/// [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
+ /// ```
1126
1156
pub fn initgroups ( user : & CString , group : Gid ) -> Result < ( ) > {
1127
1157
cfg_if ! {
1128
1158
if #[ cfg( any( target_os = "ios" , target_os = "macos" ) ) ] {
0 commit comments