1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
/* vim: set sw=4 sts=4 et foldmethod=syntax : */
/*
* Copyright (c) 2016 Saleem Abdulrasool
*
* This file is part of the Paludis package manager. Paludis is free software;
* you can redistribute it and/or modify it under the terms of the GNU General
* Public License version 2, as published by the Free Software Foundation.
*
* Paludis is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License along with
* this program; if not, write to the Free Software Foundation, Inc., 59 Temple
* Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <paludis/util/persona.hh>
#include <paludis/util/log.hh>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
namespace
{
template <unsigned Name>
struct SystemConfigurationParameter;
template <>
struct SystemConfigurationParameter<_SC_GETPW_R_SIZE_MAX> {
static constexpr const char * const Spelling = "_SC_GETPW_R_SIZE_MAX";
};
template <>
struct SystemConfigurationParameter<_SC_GETGR_R_SIZE_MAX> {
static constexpr const char * const Spelling = "_SC_GETGR_R_SIZE_MAX";
};
template <unsigned Name, size_t DefaultSize = 1024, long DodgyLimit = 1024 * 128>
size_t initial_buffer_size(const char *context)
{
using namespace paludis;
long size;
errno = 0;
size = ::sysconf(Name);
if (size == -1 && errno == 0)
return DefaultSize;
if (size > DodgyLimit)
Log::get_instance()->message(context, ll_warning, lc_context)
<< "Got dodgy value " << size << " from "
<< "sysconf(" << SystemConfigurationParameter<Name>::Spelling << ")";
return std::min(size, DodgyLimit);
}
}
namespace paludis
{
int getpwnam_r_s(const char *name, std::vector<char> & buffer,
struct passwd & pwd, struct passwd * & result)
{
size_t length;
int rv;
buffer.clear();
length =
initial_buffer_size<_SC_GETPW_R_SIZE_MAX>("accounts.getpw_r_size");
for (buffer.resize(length);
(rv = ::getpwnam_r(name, &pwd, buffer.data(), buffer.capacity(),
&result)) == ERANGE;
buffer.resize(length))
length = length * 2;
return rv;
}
int getgrgid_r_s(gid_t gid, std::vector<char> & buffer,
struct group & grp, struct group * & result)
{
size_t length;
int rv;
buffer.clear();
length =
initial_buffer_size<_SC_GETGR_R_SIZE_MAX>("accounts.getgr_r_size");
for (buffer.resize(length);
(rv = ::getgrgid_r(gid, &grp, buffer.data(), buffer.capacity(),
&result)) == ERANGE;
buffer.resize(length))
length = length * 2;
return rv;
}
int getpwuid_r_s(uid_t uid, std::vector<char> & buffer,
struct passwd & pwd, struct passwd * & result)
{
size_t length;
int rv;
buffer.clear();
length =
initial_buffer_size<_SC_GETPW_R_SIZE_MAX>("accounts.getpw_r_size");
for (buffer.resize(length);
(rv = ::getpwuid_r(uid, &pwd, buffer.data(), buffer.capacity(),
&result)) == ERANGE;
buffer.resize(length))
length = length * 2;
return rv;
}
}
|