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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
|
# vim:expandtab:autoindent:tabstop=4:shiftwidth=4:filetype=python:textwidth=0:
# License: GPL2 or later see COPYING
# Written by Michael Brown
# Sections by Seth Vidal
# Sections taken from Mach by Thomas Vander Stichele
# Copyright (C) 2007 Michael E Brown <mebrown@michaels-house.net>
# python library imports
import logging
import os
import os.path
import popen2
import rpm
import rpmUtils
import rpmUtils.transaction
import shutil
import signal
import time
# our imports
import mock.exception
from mock.trace_decorator import traceLog
# set up logging
log = logging.getLogger("mock.util")
# classes
class commandTimeoutExpired(mock.exception.Error):
def __init__(self, msg):
Error.__init__(self, msg)
self.msg = msg
self.resultcode = 10
# functions
@traceLog(log)
def mkdirIfAbsent(*args):
for dir in args:
log.debug("ensuring that dir exists: %s" % dir)
if not os.path.exists(dir):
try:
log.debug("creating dir: %s" % dir)
os.makedirs(dir)
except OSError, e:
log.exception("Could not create dir %s. Error: %s" % (dir, e))
raise mock.exception.Error, "Could not create dir %s. Error: %s" % (dir, e)
@traceLog(log)
def touch(fileName):
log.debug("touching file: %s" % fileName)
fo = open(fileName, 'w')
fo.close()
@traceLog(log)
def rmtree(path, *args, **kargs):
"""version os shutil.rmtree that ignores no-such-file-or-directory errors,
and tries harder if it finds immutable files"""
tryAgain = 1
failedFilename = None
while tryAgain:
tryAgain = 0
try:
shutil.rmtree(path, *args, **kargs)
except OSError, e:
if e.errno == 2: # no such file or directory
pass
elif e.errno==1 or e.errno==13:
tryAgain = 1
if failedFilename == e.filename:
raise
failedFilename = e.filename
os.system("chattr -R -i %s" % path)
else:
raise
@traceLog(log)
def orphansKill(rootToKill):
"""kill off anything that is still chrooted."""
for fn in os.listdir("/proc"):
try:
root = os.readlink("/proc/%s/root" % fn)
if root == rootToKill:
log.warning("Process ID %s still running in chroot. Killing..." % fn)
os.kill(int(fn,10), 15)
except OSError, e:
pass
@traceLog(log)
def yieldSrpmHeaders(srpms, plainRpmOk=0):
ts = rpmUtils.transaction.initReadOnlyTransaction()
for srpm in srpms:
try:
hdr = rpmUtils.miscutils.hdrFromPackage(ts, srpm)
except (rpmUtils.RpmUtilsError,), e:
raise mock.exception.Error, "Cannot find/open srpm: %s. Error: %s" % (srpm, ''.join(e))
if not plainRpmOk and hdr[rpm.RPMTAG_SOURCEPACKAGE] != 1:
raise mock.exception.Error("File is not an srpm: %s." % srpm )
yield hdr
@traceLog(log)
def requiresTextFromHdr(hdr):
"""take a header and hand back a unique'd list of the requires as
strings"""
reqlist = []
names = hdr[rpm.RPMTAG_REQUIRENAME]
flags = hdr[rpm.RPMTAG_REQUIREFLAGS]
ver = hdr[rpm.RPMTAG_REQUIREVERSION]
if names is not None:
tmplst = zip(names, flags, ver)
for (n, f, v) in tmplst:
if n.startswith('rpmlib'):
continue
req = rpmUtils.miscutils.formatRequire(n, v, f)
reqlist.append(req)
return rpmUtils.miscutils.unique(reqlist)
@traceLog(log)
def getNEVRA(hdr):
name = hdr[rpm.RPMTAG_NAME]
ver = hdr[rpm.RPMTAG_VERSION]
rel = hdr[rpm.RPMTAG_RELEASE]
epoch = hdr[rpm.RPMTAG_EPOCH]
arch = hdr[rpm.RPMTAG_ARCH]
if epoch is None: epoch = 0
return (name, epoch, ver, rel, arch)
@traceLog(log)
def getAddtlReqs(hdr, conf):
# Add the 'more_buildreqs' for this SRPM (if defined in config file)
(name, epoch, ver, rel, arch) = getNEVRA(hdr)
reqlist = []
for this_srpm in ['-'.join([name,ver,rel]),
'-'.join([name,ver]),
'-'.join([name]),]:
if conf.has_key(this_srpm):
more_reqs = conf[this_srpm]
if type(more_reqs) in (type(u''), type(''),):
reqlist.append(more_reqs)
else:
reqlist.extend(more_reqs)
break
return rpmUtils.miscutils.unique(reqlist)
@traceLog(log)
def uniqReqs(*args):
master = []
for l in args:
master.extend(l)
return rpmUtils.miscutils.unique(master)
# logger =
# output = [1|0]
# chrootPath
#
# Warning: this is the function from hell. :(
#
@traceLog(log)
def do(command, chrootPath=None, timeout=0, raiseExc=True, returnOutput=0, *args, **kargs):
"""execute given command outside of chroot"""
logger = kargs.get("logger", log)
logger.debug("Run cmd: %s" % command)
class alarmExc(Exception): pass
def alarmhandler(signum,stackframe):
raise alarmExc("timeout expired")
retval = 0
logger.debug("Executing timeout(%s): %s" % (timeout, command))
output=""
(r,w) = os.pipe()
pid = os.fork()
if pid: #parent
rpid = ret = 0
os.close(w)
oldhandler=signal.signal(signal.SIGALRM,alarmhandler)
starttime = time.time()
# timeout=0 means disable alarm signal. no timeout
signal.alarm(timeout)
try:
# read output from child
r_fh = os.fdopen(r, "r")
for line in r_fh:
if line.endswith("\n"):
logger.debug(line[:-1])
else:
logger.debug(line)
if returnOutput:
output += line
# close read handle, get child return status, etc
r_fh.close()
(rpid, ret) = os.waitpid(pid, 0)
signal.alarm(0)
signal.signal(signal.SIGALRM,oldhandler)
except alarmExc:
os.kill(-pid, signal.SIGTERM)
time.sleep(1)
os.kill(-pid, signal.SIGKILL)
(rpid, ret) = os.waitpid(pid, 0)
signal.signal(signal.SIGALRM,oldhandler)
raise commandTimeoutExpired( "Timeout(%s) exceeded for command: %s" % (timeout, command))
# kill children for any exception...
except:
os.kill(-pid, signal.SIGTERM)
time.sleep(1)
os.kill(-pid, signal.SIGKILL)
(rpid, ret) = os.waitpid(pid, 0)
signal.signal(signal.SIGALRM,oldhandler)
raise
# kill all children
try:
os.kill(-pid, signal.SIGTERM)
except OSError:
pass
# mask and return just return value, plus child output
if raiseExc and os.WEXITSTATUS(ret):
if returnOutput:
raise mock.exception.Error, "Command(%s) failed. Output: %s" % (command, output)
else:
raise mock.exception.Error, "Command(%s) failed. See logs for output." % command
return output
else: #child
retval = 255
try:
os.close(r)
# become process group leader so that our parent
# can kill our children
os.setpgrp()
uidManager = kargs.get("uidManager")
if chrootPath is not None:
if uidManager:
logger.debug("elevate privs to run chroot")
uidManager.becomeUser(0)
os.chdir(chrootPath)
os.chroot(chrootPath)
if uidManager:
logger.debug("back to other privs")
uidManager.restorePrivs()
if uidManager:
logger.debug("about to drop privs")
uid = kargs.get("uid", None)
gid = kargs.get("gid", None)
if uid is not None: uidManager.unprivUid=uid
if gid is not None: uidManager.unprivGid=gid
uidManager.dropPrivsForever()
child = popen2.Popen4(command)
child.tochild.close()
w = os.fdopen(w, "w")
for line in child.fromchild:
w.write(line)
w.flush()
w.close()
retval=child.wait()
finally:
os._exit(os.WEXITSTATUS(retval))
|