~ubuntu-branches/ubuntu/lucid/landscape-client/lucid-updates

« back to all changes in this revision

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

  • Committer: Package Import Robot
  • Author(s): Andreas Hasenack
  • Date: 2012-04-10 14:28:48 UTC
  • mfrom: (1.1.27)
  • mto: This revision was merged to the branch mainline in revision 35.
  • Revision ID: package-import@ubuntu.com-20120410142848-7xsy4g2xii7y7ntc
ImportĀ upstreamĀ versionĀ 12.04.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
 Copyright (c) 2011 Canonical, Ltd.
 
4
 
 
5
*/
 
6
 
 
7
#define _GNU_SOURCE
 
8
#include <sys/resource.h>
 
9
#include <sys/types.h>
 
10
#include <sys/stat.h>
 
11
#include <grp.h>
 
12
#include <unistd.h>
 
13
#include <stdlib.h>
 
14
#include <string.h>
 
15
#include <errno.h>
 
16
#include <stdio.h>
 
17
#include <pwd.h>
 
18
 
 
19
int main(int argc, char *argv[], char *envp[])
 
20
{
 
21
  char *apt_argv[] = {"/usr/bin/apt-get", "-q", "update", NULL};
 
22
  char *apt_envp[] = {"PATH=/bin:/usr/bin", NULL, NULL};
 
23
 
 
24
  // Set the HOME environment variable
 
25
  struct passwd *pwd = getpwuid(geteuid());
 
26
  if (!pwd) {
 
27
    fprintf(stderr, "error: Unable to find passwd entry for uid %d (%s)\n",
 
28
            geteuid(), strerror(errno));
 
29
    exit(1);
 
30
  }
 
31
  if (asprintf(&apt_envp[1], "HOME=%s", pwd->pw_dir) == -1) {
 
32
    perror("error: Unable to create HOME environment variable");
 
33
    exit(1);
 
34
  }
 
35
 
 
36
  // Drop any supplementary group
 
37
  if (setgroups(0, NULL) == -1) {
 
38
    perror("error: Unable to set supplementary groups IDs");
 
39
    exit(1);
 
40
  }
 
41
 
 
42
  // Set real/effective gid and uid
 
43
  if (setregid(pwd->pw_gid, pwd->pw_gid) == -1) {
 
44
    fprintf(stderr, "error: Unable to set real and effective gid (%s)\n",
 
45
            strerror(errno));
 
46
    exit(1);
 
47
  }
 
48
  if (setreuid(pwd->pw_uid, pwd->pw_uid) == -1) {
 
49
    perror("error: Unable to set real and effective uid");
 
50
    exit(1);
 
51
  }
 
52
 
 
53
  // Close all file descriptors except the standard ones
 
54
  struct rlimit rlp;
 
55
  if (getrlimit(RLIMIT_NOFILE, &rlp) == -1) {
 
56
    perror("error: Unable to determine file descriptor limits");
 
57
    exit(1);
 
58
  }
 
59
  int file_max;
 
60
  if (rlp.rlim_max == RLIM_INFINITY || rlp.rlim_max > 4096)
 
61
    file_max = 4096;
 
62
  else
 
63
    file_max = rlp.rlim_max;
 
64
  int file;
 
65
  for (file = 3; file < file_max; file++) {
 
66
    close(file);
 
67
  }
 
68
 
 
69
  // Set umask to 022
 
70
  umask(S_IWGRP | S_IWOTH);
 
71
 
 
72
  if (chdir("/") == -1) {
 
73
    perror("error: Unable to change working directory");
 
74
    exit(1);
 
75
  }
 
76
 
 
77
  // Run apt-get update
 
78
  execve(apt_argv[0], apt_argv, apt_envp);
 
79
  perror("error: Unable to execute apt-get");
 
80
  return 1;
 
81
}