~wgrant/ubuntu/natty/landscape-client/natty-updates-broken

« back to all changes in this revision

Viewing changes to smart-update/smart-update.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathias Gug, Free Ekanayaka
  • Date: 2009-07-22 14:54:50 UTC
  • mfrom: (1.1.9 upstream)
  • Revision ID: james.westby@ubuntu.com-20090722145450-pvbp13gh8734c8ft
Tags: 1.3.2.2-0ubuntu0.9.10.1
[ Free Ekanayaka ]
* New upstream release:
  - Include the README file in landscape-client (LP: #396260)
  - Fix client capturing stderr from run_command when constructing
    hash-id-databases url (LP: #397480)
  - Use substvars to conditionally depend on update-motd or
    libpam-modules (LP: #393454)
  - Fix reporting wrong version to the server (LP: #391225)
  - The init script does not wait for the network to be available
    before checking for EC2 user data (LP: #383336)
  - When the broker is restarted by the watchdog, the state of the client
    is inconsistent (LP: #380633)
  - Package stays unknown forever in the client with hash-id-databases
    support (LP: #381356)
  - Standard error not captured when calling smart-update (LP: #387441)
  - Changer calls reporter without switching groups, just user (LP: #388092)
  - Run smart update in the package-reporter instead of having a cronjob (LP: #362355)
  - Package changer does not inherit proxy settings (LP: #381241)
  - The ./test script doesn't work in landscape-client (LP: #381613)
  - The source package should build on all supported releases (LP: #385098)
  - Strip smart update's output (LP: #387331)
  - The fetch() timeout isn't based on activity (#389224)
  - Client can use a UUID of "None" when fetching the hash-id-database (LP: #381291)
  - Registration should use the fqdn rather than just the hostname (LP: #385730)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
 Copyright (c) 2004 Conectiva, Inc.
 
4
 Copyright (c) 2009 Canonical, Ltd.
 
5
 
 
6
 Written by Gustavo Niemeyer <niemeyer@conectiva.com>,
 
7
            Free Ekanayaka <free.ekanayaka@canonical.com>
 
8
 
 
9
 This file is part of Smart Package Manager.
 
10
 
 
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.
 
15
 
 
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.
 
20
 
 
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
 
24
 
 
25
*/
 
26
#define _GNU_SOURCE
 
27
#include <sys/resource.h>
 
28
#include <sys/types.h>
 
29
#include <sys/stat.h>
 
30
#include <grp.h>
 
31
#include <unistd.h>
 
32
#include <stdlib.h>
 
33
#include <string.h>
 
34
#include <errno.h>
 
35
#include <stdio.h>
 
36
#include <pwd.h>
 
37
 
 
38
int main(int argc, char *argv[], char *envp[])
 
39
{
 
40
    char *smart_argv[] = {"/usr/share/smart/smart", "update", NULL, NULL};
 
41
    char *smart_envp[] = {"PATH=/bin:/usr/bin", NULL, NULL};
 
42
 
 
43
    // Set the HOME environment variable
 
44
    struct passwd *pwd = getpwuid(geteuid());
 
45
    if (!pwd) {
 
46
        fprintf(stderr, "error: Unable to find passwd entry for uid %d (%s)\n",
 
47
                geteuid(), strerror(errno));
 
48
        exit(1);
 
49
    }
 
50
    if (asprintf(&smart_envp[1], "HOME=%s", pwd->pw_dir) == -1) {
 
51
        perror("error: Unable to create HOME environment variable");
 
52
        exit(1);
 
53
    }
 
54
 
 
55
    // Handle the --after command line option
 
56
    if (argc != 1) {
 
57
        if (argc != 3 || strcmp(argv[1], "--after") != 0) {
 
58
          fprintf(stderr, "error: Unsupported command line option\n");
 
59
          exit(1);
 
60
        }
 
61
        char *end;
 
62
        long interval = strtol(argv[2], &end, 10);
 
63
        if (end == argv[2]) {
 
64
          fprintf(stderr, "error: Interval value '%s' not a number\n", argv[2]);
 
65
          exit(1);
 
66
        }
 
67
        if (asprintf(&smart_argv[2], "--after=%ld", interval) == -1) {
 
68
          perror("error: Unable to create argument variable");
 
69
          exit(1);
 
70
        }
 
71
    }
 
72
 
 
73
    // Drop any supplementary group
 
74
    if (setgroups(0, NULL) == -1) {
 
75
        perror("error: Unable to set supplementary groups IDs");
 
76
        exit(1);
 
77
    }
 
78
 
 
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",
 
82
                strerror(errno));
 
83
        exit(1);
 
84
    }
 
85
    if (setreuid(pwd->pw_uid, pwd->pw_uid) == -1) {
 
86
        perror("error: Unable to set real and effective uid");
 
87
        exit(1);
 
88
    }
 
89
 
 
90
    // Close all file descriptors except the standard ones
 
91
    struct rlimit rlp;
 
92
    if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
 
93
        perror("error: Unable to determine file descriptor limits");
 
94
        exit(1);
 
95
    }
 
96
    int file_max;
 
97
    if (rlp.rlim_max == RLIM_INFINITY || rlp.rlim_max > 4096)
 
98
        file_max = 4096;
 
99
    else
 
100
        file_max = rlp.rlim_max;
 
101
    int file;
 
102
    for (file = 3; file < file_max; file++) {
 
103
        close(file);
 
104
    }
 
105
 
 
106
    // Set umask to 022
 
107
    umask(S_IWGRP | S_IWOTH);
 
108
 
 
109
    if (chdir("/") == -1) {
 
110
        perror("error: Unable to change working directory");
 
111
        exit(1);
 
112
    }
 
113
 
 
114
    // Run smart update
 
115
    execve(smart_argv[0], smart_argv, smart_envp);
 
116
    perror("error: Unable to execute smart");
 
117
    return 1;
 
118
}
 
119
 
 
120
/* vim:ts=4:sw=4:et
 
121
*/