rpm  5.4.14
names.c
Go to the documentation of this file.
1 /*@-mods@*/
8 #include "system.h"
9 
10 #include <rpmio.h>
11 #include <rpmiotypes.h>
12 #include <rpmlog.h>
13 #include "rpmbuild.h"
14 #include "debug.h"
15 
16 typedef /*@owned@*/ /*@null@*/ const char * ugstr_t;
17 
18 /*@unchecked@*/
19 static uid_t uids[1024];
20 /*@unchecked@*/
21 static ugstr_t unames[1024];
22 /*@unchecked@*/
23 static int uid_used = 0;
24 
25 /*@unchecked@*/
26 static gid_t gids[1024];
27 /*@unchecked@*/
28 static ugstr_t gnames[1024];
29 /*@unchecked@*/
30 static int gid_used = 0;
31 
32 void freeNames(void)
33 {
34  int x;
35  for (x = 0; x < uid_used; x++)
36  unames[x] = _free(unames[x]);
37  for (x = 0; x < gid_used; x++)
38  gnames[x] = _free(gnames[x]);
39 }
40 
41 const char *getUname(uid_t uid)
42  /*@globals uid_used, uids, unames @*/
43  /*@modifies uid_used, uids, unames @*/
44 {
45  struct passwd *pw;
46  int x;
47 
48  for (x = 0; x < uid_used; x++) {
49  if (unames[x] == NULL) continue;
50  if (uids[x] == uid)
51  return unames[x];
52  }
53 
54  /* XXX - This is the other hard coded limit */
55  if (x == 1024)
56  rpmlog(RPMLOG_CRIT, _("getUname: too many uid's\n"));
57 
58  if ((pw = getpwuid(uid)) == NULL)
59  return NULL;
60  uids[uid_used] = uid;
61  unames[uid_used] = xstrdup(pw->pw_name);
62  return unames[uid_used++];
63 }
64 
65 const char *getUnameS(const char *uname)
66  /*@globals uid_used, uids, unames @*/
67  /*@modifies uid_used, uids, unames @*/
68 {
69  struct passwd *pw;
70  int x;
71 
72  for (x = 0; x < uid_used; x++) {
73  if (unames[x] == NULL) continue;
74  if (!strcmp(unames[x],uname))
75  return unames[x];
76  }
77 
78  /* XXX - This is the other hard coded limit */
79  if (x == 1024)
80  rpmlog(RPMLOG_CRIT, _("getUnameS: too many uid's\n"));
81 
82  if ((pw = getpwnam(uname)) == NULL) {
83  uids[uid_used] = -1;
84  unames[uid_used] = xstrdup(uname);
85  } else {
86  uids[uid_used] = pw->pw_uid;
87  unames[uid_used] = xstrdup(pw->pw_name);
88  }
89  return unames[uid_used++];
90 }
91 
92 uid_t getUidS(const char *uname)
93  /*@globals uid_used, uids, unames @*/
94  /*@modifies uid_used, uids, unames @*/
95 {
96  struct passwd *pw;
97  int x;
98 
99  for (x = 0; x < uid_used; x++) {
100  if (unames[x] == NULL) continue;
101  if (!strcmp(unames[x],uname))
102  return uids[x];
103  }
104 
105  /* XXX - This is the other hard coded limit */
106  if (x == 1024)
107  rpmlog(RPMLOG_CRIT, _("getUidS: too many uid's\n"));
108 
109  if ((pw = getpwnam(uname)) == NULL) {
110  uids[uid_used] = -1;
111  unames[uid_used] = xstrdup(uname);
112  } else {
113  uids[uid_used] = pw->pw_uid;
114  unames[uid_used] = xstrdup(pw->pw_name);
115  }
116  return uids[uid_used++];
117 }
118 
119 const char *getGname(gid_t gid)
120  /*@globals gid_used, gids, gnames @*/
121  /*@modifies gid_used, gids, gnames @*/
122 {
123  struct group *gr;
124  int x;
125 
126  for (x = 0; x < gid_used; x++) {
127  if (gnames[x] == NULL) continue;
128  if (gids[x] == gid)
129  return gnames[x];
130  }
131 
132  /* XXX - This is the other hard coded limit */
133  if (x == 1024)
134  rpmlog(RPMLOG_CRIT, _("getGname: too many gid's\n"));
135 
136  if ((gr = getgrgid(gid)) == NULL)
137  return NULL;
138  gids[gid_used] = gid;
139  gnames[gid_used] = xstrdup(gr->gr_name);
140  return gnames[gid_used++];
141 }
142 
143 const char *getGnameS(const char *gname)
144  /*@globals gid_used, gids, gnames @*/
145  /*@modifies gid_used, gids, gnames @*/
146 {
147  struct group *gr;
148  int x;
149 
150  for (x = 0; x < gid_used; x++) {
151  if (gnames[x] == NULL) continue;
152  if (!strcmp(gnames[x], gname))
153  return gnames[x];
154  }
155 
156  /* XXX - This is the other hard coded limit */
157  if (x == 1024)
158  rpmlog(RPMLOG_CRIT, _("getGnameS: too many gid's\n"));
159 
160  if ((gr = getgrnam(gname)) == NULL) {
161  gids[gid_used] = -1;
162  gnames[gid_used] = xstrdup(gname);
163  } else {
164  gids[gid_used] = gr->gr_gid;
165  gnames[gid_used] = xstrdup(gr->gr_name);
166  }
167  return gnames[gid_used++];
168 }
169 
170 gid_t getGidS(const char *gname)
171  /*@globals gid_used, gids, gnames @*/
172  /*@modifies gid_used, gids, gnames @*/
173 {
174  struct group *gr;
175  int x;
176 
177  for (x = 0; x < gid_used; x++) {
178  if (gnames[x] == NULL) continue;
179  if (!strcmp(gnames[x], gname))
180  return gids[x];
181  }
182 
183  /* XXX - This is the other hard coded limit */
184  if (x == 1024)
185  rpmlog(RPMLOG_CRIT, _("getGidS: too many gid's\n"));
186 
187  if ((gr = getgrnam(gname)) == NULL) {
188  gids[gid_used] = -1;
189  gnames[gid_used] = xstrdup(gname);
190  } else {
191  gids[gid_used] = gr->gr_gid;
192  gnames[gid_used] = xstrdup(gr->gr_name);
193  }
194  return gids[gid_used++];
195 }
196 
198 {
199  static rpmuint32_t buildTime[1];
200 
201  if (buildTime[0] == 0)
202  buildTime[0] = (rpmuint32_t) time(NULL);
203  return buildTime;
204 }
205 
206 const char * buildHost(void)
207 {
208  static char hostname[1024];
209  static int oneshot = 0;
210  struct hostent *hbn;
211 
212  if (! oneshot) {
213  (void) gethostname(hostname, sizeof(hostname));
214  /*@-unrecog -multithreaded @*/
215  /*@-globs@*/ /* FIX: h_errno access */
216  hbn = gethostbyname(hostname);
217  /*@=globs@*/
218  /*@=unrecog =multithreaded @*/
219  if (hbn) {
220  strncpy(hostname, hbn->h_name, sizeof(hostname)-1);
221  hostname[sizeof(hostname)-1] = '\0';
222  } else
224  _("Could not canonicalize hostname: %s\n"), hostname);
225  oneshot = 1;
226  }
227  return(hostname);
228 }
229 /*@=mods@*/
gid_t getGidS(const char *gname)
Return cached group id.
Definition: names.c:170
static ugstr_t gnames[1024]
Definition: names.c:28
rpmlog(RPMLOG_ERR,"%s\n", buf)
char * xstrdup(const char *str)
Definition: rpmmalloc.c:321
const char * getGnameS(const char *gname)
Return cached group name.
Definition: names.c:143
const char * buildHost(void)
Return build hostname.
Definition: names.c:206
const char * getUname(uid_t uid)
Return cached user name from user id.
Definition: names.c:41
uid_t getUidS(const char *uname)
Return cached user id.
Definition: names.c:92
Yet Another syslog(3) API clone.
unsigned int rpmuint32_t
Definition: rpmiotypes.h:28
static uid_t uids[1024]
Definition: names.c:19
rpmuint32_t * getBuildTime(void)
Return build time stamp.
Definition: names.c:197
const char * ugstr_t
Definition: names.c:16
void freeNames(void)
Destroy uid/gid caches.
Definition: names.c:32
static int gid_used
Definition: names.c:30
static ugstr_t unames[1024]
Definition: names.c:21
return strcmp(ame->name, bme->name)
static gid_t gids[1024]
Definition: names.c:26
static int uid_used
Definition: names.c:23
This is the only module users of librpmbuild should need to include.
return NULL
Definition: poptALL.c:613
const char * getUnameS(const char *uname)
Return cached user name.
Definition: names.c:65
static void
Print copy of spec file, filling in Group/Description/Summary from specspo.
Definition: spec.c:737
#define _(Text)
Definition: system.h:29
const char * getGname(gid_t gid)
Return cached group name from group id.
Definition: names.c:119
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:647
strncpy(sbuf, f, flen)