File: umask.c

package info (click to toggle)
libguestfs 1%3A1.40.2-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 123,660 kB
  • sloc: ansic: 460,074; ml: 63,059; sh: 14,955; java: 9,512; makefile: 9,133; cs: 6,300; haskell: 5,652; python: 3,856; perl: 3,619; erlang: 2,435; xml: 1,683; ruby: 350; pascal: 255; lex: 135; yacc: 128; cpp: 10
file content (170 lines) | stat: -rw-r--r-- 4,044 bytes parent folder | download | duplicates (6)
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
/* libguestfs
 * Copyright (C) 2016 Red Hat Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

/**
 * Return current umask in a thread-safe way.
 *
 * glibc documents, but does not actually implement, a "getumask(3)"
 * call.
 *
 * We use C<Umask> from F</proc/self/status> for Linux E<ge> 4.7.
 * For older Linux and other Unix, this file implements an expensive
 * but thread-safe way to get the current process's umask.
 *
 * Thanks to: Josh Stone, Jiri Jaburek, Eric Blake.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>

#include "ignore-value.h"

#include "guestfs.h"
#include "guestfs-internal.h"

static int get_umask_from_proc (guestfs_h *g);
static int get_umask_from_fork (guestfs_h *g);

/**
 * Returns the current process's umask.  On failure, returns C<-1> and
 * sets the error in the guestfs handle.
 */
int
guestfs_int_getumask (guestfs_h *g)
{
  int mask;

  mask = get_umask_from_proc (g);
  if (mask == -1)
    return -1;
  if (mask >= 0)
    return mask;

  return get_umask_from_fork (g);
}

/**
 * For Linux E<ge> 4.7 get the umask from F</proc/self/status>.
 *
 * On failure this returns C<-1>.  However if we could not open the
 * F</proc> file or find the C<Umask> entry in it, return C<-2> which
 * causes the fallback path to run.
 */
static int
get_umask_from_proc (guestfs_h *g)
{
  CLEANUP_FCLOSE FILE *fp = NULL;
  CLEANUP_FREE char *line = NULL;
  size_t allocsize = 0;
  ssize_t len;
  unsigned int mask;
  bool found = false;

  fp = fopen ("/proc/self/status", "r");
  if (fp == NULL) {
    if (errno == ENOENT || errno == ENOTDIR)
      return -2;                /* fallback */
    perrorf (g, "open: /proc/self/status");
    return -1;
  }

  while ((len = getline (&line, &allocsize, fp)) != -1) {
    if (len > 0 && line[len-1] == '\n')
      line[--len] = '\0';

    /* Looking for: "Umask:  0022" */
    if (sscanf (line, "Umask: %o", &mask) == 1) {
      found = true;
      break;
    }
  }

  if (!found)
    return -2;                  /* fallback */

  return (int) mask;
}

/**
 * Fallback method of getting the umask using fork.
 */
static int
get_umask_from_fork (guestfs_h *g)
{
  pid_t pid;
  int fd[2], r;
  int mask;
  int status;

  r = pipe2 (fd, O_CLOEXEC);
  if (r == -1) {
    perrorf (g, "pipe2");
    return -1;
  }

  pid = fork ();
  if (pid == -1) {
    perrorf (g, "fork");
    close (fd[0]);
    close (fd[1]);
    return -1;
  }
  if (pid == 0) {
    /* The child process must ONLY call async-safe functions. */
    close (fd[0]);

    /* umask can't fail. */
    mask = umask (0);

    if (write (fd[1], &mask, sizeof mask) != sizeof mask)
      _exit (EXIT_FAILURE);
    if (close (fd[1]) == -1)
      _exit (EXIT_FAILURE);

    _exit (EXIT_SUCCESS);
  }

  /* Parent. */
  close (fd[1]);

  /* Read the umask. */
  if (read (fd[0], &mask, sizeof mask) != sizeof mask) {
    perrorf (g, "read");
    close (fd[0]);
    guestfs_int_waitpid_noerror (pid);
    return -1;
  }
  close (fd[0]);

  if (guestfs_int_waitpid (g, pid, &status, "umask") == -1)
    return -1;
  else if (!WIFEXITED (status) || WEXITSTATUS (status) != 0) {
    guestfs_int_external_command_failed (g, status, "umask", NULL);
    return -1;
  }

  return mask;
}