smartmontools SVN Rev 5612
Utility to control and monitor storage systems with "S.M.A.R.T."
popen_as_ugid.h
Go to the documentation of this file.
1/*
2 * popen_as_ugid.h
3 *
4 * Home page of code is: https://www.smartmontools.org
5 *
6 * Copyright (C) 2021 Christian Franke
7 *
8 * SPDX-License-Identifier: GPL-2.0-or-later
9 */
10
11#ifndef POPEN_AS_UGID_H_CVSID
12#define POPEN_AS_UGID_H_CVSID "$Id: popen_as_ugid.h 5268 2021-12-13 18:55:13Z chrfranke $"
13
14#include <grp.h>
15#include <pwd.h>
16#include <stdio.h>
17#include <string>
18
19// Wrapper for popen(3) which prevents that unneeded file descriptors
20// are inherited to the command run by popen() and optionally drops
21// privileges of root user:
22// If uid != 0, popen() is run as this user.
23// If gid != 0, popen() is run as this group and no supplemental groups.
24// Only mode "r" is supported. Only one open stream at a time is supported.
25FILE * popen_as_ugid(const char * cmd, const char * mode, uid_t uid, gid_t gid);
26
27// Call corresponding pclose(3) and return its result.
28int pclose_as_ugid(FILE * f);
29
30// Parse "USER[:GROUP]" string and set uid, gid, uname and gname accordingly.
31// USER and GROUP may be specified as numeric ids or names.
32// If a numeric id is used and the corresponding user (or group) does not
33// exist, the function succeeds but leaves uname (or gname) unchanged.
34// If no GROUP is specified, the default group of USER is used instead.
35// Returns nullptr on success or a message string on error.
36const char * parse_ugid(const char * s, uid_t & uid, gid_t & gid,
37 std::string & uname, std::string & gname );
38
39#endif // POPEN_AS_UGID_H_CVSID
u8 cmd
Definition: megaraid.h:1
u16 s[6]
Definition: megaraid.h:18
FILE * popen_as_ugid(const char *cmd, const char *mode, uid_t uid, gid_t gid)
int pclose_as_ugid(FILE *f)
const char * parse_ugid(const char *s, uid_t &uid, gid_t &gid, std::string &uname, std::string &gname)