3
Copyright (c) 2004 Conectiva, Inc.
4
Copyright (c) 2009 Canonical, Ltd.
6
Written by Gustavo Niemeyer <niemeyer@conectiva.com>,
7
Free Ekanayaka <free.ekanayaka@canonical.com>
9
This file is part of Smart Package Manager.
11
Smart Package Manager is free software; you can redistribute it and/or
12
modify it under the terms of the GNU General Public License as published
13
by the Free Software Foundation; either version 2 of the License, or (at
14
your option) any later version.
16
Smart Package Manager is distributed in the hope that it will be useful,
17
but WITHOUT ANY WARRANTY; without even the implied warranty of
18
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19
General Public License for more details.
21
You should have received a copy of the GNU General Public License
22
along with Smart Package Manager; if not, write to the Free Software
23
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27
#include <sys/resource.h>
28
#include <sys/types.h>
38
int main(int argc, char *argv[], char *envp[])
40
char *smart_argv[] = {"/usr/share/smart/smart", "update", NULL, NULL};
41
char *smart_envp[] = {"PATH=/bin:/usr/bin", NULL, NULL};
43
// Set the HOME environment variable
44
struct passwd *pwd = getpwuid(geteuid());
46
fprintf(stderr, "error: Unable to find passwd entry for uid %d (%s)\n",
47
geteuid(), strerror(errno));
50
if (asprintf(&smart_envp[1], "HOME=%s", pwd->pw_dir) == -1) {
51
perror("error: Unable to create HOME environment variable");
55
// Handle the --after command line option
57
if (argc != 3 || strcmp(argv[1], "--after") != 0) {
58
fprintf(stderr, "error: Unsupported command line option\n");
62
long interval = strtol(argv[2], &end, 10);
64
fprintf(stderr, "error: Interval value '%s' not a number\n", argv[2]);
67
if (asprintf(&smart_argv[2], "--after=%ld", interval) == -1) {
68
perror("error: Unable to create argument variable");
73
// Drop any supplementary group
74
if (setgroups(0, NULL) == -1) {
75
perror("error: Unable to set supplementary groups IDs");
79
// Set real/effective gid and uid
80
if (setregid(pwd->pw_gid, pwd->pw_gid) == -1) {
81
fprintf(stderr, "error: Unable to set real and effective gid (%s)\n",
85
if (setreuid(pwd->pw_uid, pwd->pw_uid) == -1) {
86
perror("error: Unable to set real and effective uid");
90
// Close all file descriptors except the standard ones
92
if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
93
perror("error: Unable to determine file descriptor limits");
97
if (rlp.rlim_max == RLIM_INFINITY || rlp.rlim_max > 4096)
100
file_max = rlp.rlim_max;
102
for (file = 3; file < file_max; file++) {
107
umask(S_IWGRP | S_IWOTH);
109
if (chdir("/") == -1) {
110
perror("error: Unable to change working directory");
115
execve(smart_argv[0], smart_argv, smart_envp);
116
perror("error: Unable to execute smart");