~ubuntu-branches/ubuntu/precise/util-linux/precise-proposed

« back to all changes in this revision

Viewing changes to misc-utils/wipefs.c

  • Committer: Bazaar Package Importer
  • Author(s): Steve Langasek
  • Date: 2011-06-20 22:31:50 UTC
  • mfrom: (1.6.3 upstream) (4.5.1 sid)
  • Revision ID: james.westby@ubuntu.com-20110620223150-lz8wrv0946ihcz3z
Tags: 2.19.1-2ubuntu1
* Merge from Debian unstable, remaining changes:
  - Build for multiarch.
  - Add pre-depends on multiarch-support.
  - configure.ac: don't try to be clever about extracting a path name from
    $libdir to append to /usr in a way that's not overridable; instead,
    reuse the built-in configurable libexecdir.
  - Fix up the .pc.in files to know about libexecdir, so our substitutions
    don't leave us with unusable pkg-config files.
  - Install custom blkid.conf to use /dev/.blkid.tab since we don't
    expect device names to survive a reboot
  - Mention mountall(8) in fstab(5) manpages, along with its special
    options.
  - Since upstart is required in Ubuntu, the hwclock.sh init script is not
    called on startup and the hwclockfirst.sh init script is removed.
  - Drop depends on initscripts for the above.
  - Replace hwclock udev rule with an Upstart job.
  - For the case where mount is called with a directory to mount, look
    that directory up in mountall's /lib/init/fstab if we couldn't find
    it mentioned anywhere else.  This means "mount /proc", "mount /sys",
    etc. work.
  - mount.8 points to the cifs-utils package, not the obsolete smbfs one. 
* Dropped changes:
  - mount.preinst: lsb_release has been fixed in lucid and above to be
    usable without configuration, so we don't have to diverge from Debian
    here anymore.
* Changes merged upstream:
  - sfdisk support for '+' with '-N'
  - mount/umount.c: fix a segfault on umount with empty mtab entry
  - Fix arbitrary unmount with fuse security issue

Show diffs side-by-side

added added

removed removed

Lines of Context:
4
4
 * Copyright (C) 2009 Red Hat, Inc. All rights reserved.
5
5
 * Written by Karel Zak <kzak@redhat.com>
6
6
 *
7
 
 * This program is free software; you can redistribute it and/or
8
 
 * modify it under the terms of the GNU General Public License as
9
 
 * published by the Free Software Foundation.
 
7
 * This program is free software; you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation; either version 2 of the License, or
 
10
 * (at your option) any later version.
10
11
 *
11
12
 * This program is distributed in the hope that it would be useful,
12
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
33
34
#include <blkid.h>
34
35
 
35
36
#include "nls.h"
 
37
#include "xalloc.h"
 
38
#include "strutils.h"
 
39
#include "writeall.h"
36
40
 
37
41
struct wipe_desc {
38
42
        loff_t          offset;         /* magic string offset */
133
137
        return wp;
134
138
}
135
139
 
136
 
static inline void *
137
 
xmalloc(size_t sz)
138
 
{
139
 
        void *x = malloc(sz);
140
 
        if (!x)
141
 
                err(EXIT_FAILURE, _("malloc failed"));
142
 
        return x;
143
 
}
144
 
 
145
 
static inline char *
146
 
xstrdup(const char *s)
147
 
{
148
 
        char *x = strdup(s);
149
 
        if (!x)
150
 
                err(EXIT_FAILURE, _("strdup failed"));
151
 
        return x;
152
 
}
153
 
 
154
140
static struct wipe_desc *
155
141
get_offset_from_probe(struct wipe_desc *wp, blkid_probe pr, int zap)
156
142
{
172
158
                wp->usage = xstrdup(usage);
173
159
                wp->type = xstrdup(type);
174
160
 
175
 
                wp->magic = xmalloc(wp->len);
 
161
                wp->magic = xmalloc(len);
176
162
                memcpy(wp->magic, mag, len);
177
163
                wp->len = len;
178
164
 
208
194
        if (rc == 0) {
209
195
                const char *type = NULL;
210
196
                blkid_probe_lookup_value(pr, "PTTYPE", &type, NULL);
211
 
                errx(EXIT_FAILURE, _("error: %s: appears to contain '%s' "
 
197
                warnx(_("WARNING: %s: appears to contain '%s' "
212
198
                                "partition table"), fname, type);
213
199
        }
214
200
 
228
214
}
229
215
 
230
216
static int
231
 
write_all(int fd, const void *buf, size_t count)
232
 
{
233
 
        while(count) {
234
 
                ssize_t tmp;
235
 
 
236
 
                errno = 0;
237
 
                tmp = write(fd, buf, count);
238
 
                if (tmp > 0) {
239
 
                        count -= tmp;
240
 
                        if (count)
241
 
                                buf += tmp;
242
 
                } else if (errno != EINTR && errno != EAGAIN)
243
 
                        return -1;
244
 
        }
245
 
        return 0;
246
 
}
247
 
 
248
 
static int
249
217
do_wipe_offset(int fd, struct wipe_desc *wp, const char *fname, int noact)
250
218
{
251
219
        char buf[BUFSIZ];
254
222
        size_t len;
255
223
 
256
224
        if (!wp->type) {
257
 
                warnx(_("can't found a magic string at offset "
258
 
                                "0x%jx - ignore."), wp->offset);
 
225
                warnx(_("no magic string found at offset "
 
226
                        "0x%jx -- ignored"), wp->offset);
259
227
                return 0;
260
228
        }
261
229
 
301
269
        return 0;
302
270
}
303
271
 
 
272
static void
 
273
free_wipe(struct wipe_desc *wp)
 
274
{
 
275
        while (wp) {
 
276
                struct wipe_desc *next = wp->next;
 
277
 
 
278
                free(wp->usage);
 
279
                free(wp->type);
 
280
                free(wp->magic);
 
281
                free(wp->label);
 
282
                free(wp->uuid);
 
283
                free(wp);
 
284
 
 
285
                wp = next;
 
286
        }
 
287
}
 
288
 
304
289
static loff_t
305
290
strtoll_offset(const char *str)
306
291
{
307
 
        char *end = NULL;
308
 
        loff_t off;
309
 
 
310
 
        errno = 0;
311
 
        off = strtoll(str, &end, 0);
312
 
 
313
 
        if ((errno == ERANGE && (off == LLONG_MAX || off == LONG_MIN)) ||
314
 
            (errno != 0 && off == 0))
315
 
                err(EXIT_FAILURE, _("invalid offset '%s' value specified"), str);
316
 
 
317
 
        if (*end != '\0')
318
 
                errx(EXIT_FAILURE, _("invalid offset '%s' value specified"), str);
319
 
 
320
 
        return off;
 
292
        uintmax_t sz;
 
293
 
 
294
        if (strtosize(str, &sz))
 
295
                errx(EXIT_FAILURE, _("invalid offset value '%s' specified"), str);
 
296
        return sz;
321
297
}
322
298
 
 
299
 
323
300
static void __attribute__((__noreturn__))
324
301
usage(FILE *out)
325
302
{
328
305
 
329
306
        fprintf(out, _(
330
307
        " -a, --all           wipe all magic strings (BE CAREFUL!)\n"
331
 
        " -h, --help          this help\n"
332
 
        " -n, --no-act        everything to be done except for the write() call\n"
 
308
        " -h, --help          show this help text\n"
 
309
        " -n, --no-act        do everything except the actual write() call\n"
333
310
        " -o, --offset <num>  offset to erase, in bytes\n"
334
311
        " -p, --parsable      print out in parsable instead of printable format\n"));
335
312
 
390
367
 
391
368
        fname = argv[optind++];
392
369
 
 
370
        if (optind != argc)
 
371
                errx(EXIT_FAILURE, _("only one device as argument is currently supported."));
 
372
 
393
373
        wp = read_offsets(wp, fname, all);
394
374
 
395
375
        if (wp) {
397
377
                        do_wipe(wp, fname, noact);
398
378
                else
399
379
                        print_all(wp, mode);
 
380
 
 
381
                free_wipe(wp);
400
382
        }
401
383
        return EXIT_SUCCESS;
402
384
}