From 2f3fdc4975d03e8c02848b4e3f9c585636fa721a Mon Sep 17 00:00:00 2001 From: Gregor Giesen Date: Mon, 12 Sep 2016 07:39:12 +0200 Subject: [PATCH] cron: replacement for os.getlogin() (#4777) os.getlogin() returns the user logged in on the controlling terminal. However 'crontab' only looks for the login name of the process' real user id which pwd.getpwuid(os.getuid())[0] does provide. While in most cases there is no difference, the former might fail under certain circumstances (e.g. a lxc container connected by attachment without login), throwing the error 'OSError: [Errno 25] Inappropriate ioctl for device'. --- system/cron.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/system/cron.py b/system/cron.py index 0c17777be4e..00ac3709b90 100644 --- a/system/cron.py +++ b/system/cron.py @@ -200,6 +200,7 @@ EXAMPLES = ''' ''' import os +import pwd import re import tempfile import platform @@ -479,7 +480,7 @@ class CronTab(object): return "%s -l %s" % (pipes.quote(CRONCMD), pipes.quote(self.user)) elif platform.system() == 'HP-UX': return "%s %s %s" % (CRONCMD , '-l', pipes.quote(self.user)) - elif os.getlogin() != self.user: + elif pwd.getpwuid(os.getuid())[0] != self.user: user = '-u %s' % pipes.quote(self.user) return "%s %s %s" % (CRONCMD , user, '-l') @@ -491,7 +492,7 @@ class CronTab(object): if self.user: if platform.system() in ['SunOS', 'HP-UX', 'AIX']: return "chown %s %s ; su '%s' -c '%s %s'" % (pipes.quote(self.user), pipes.quote(path), pipes.quote(self.user), CRONCMD, pipes.quote(path)) - elif os.getlogin() != self.user: + elif pwd.getpwuid(os.getuid())[0] != self.user: user = '-u %s' % pipes.quote(self.user) return "%s %s %s" % (CRONCMD , user, pipes.quote(path))