~ubuntu-branches/ubuntu/raring/parrot/raring-proposed

« back to all changes in this revision

Viewing changes to src/platform/generic/entropy.c

  • Committer: Bazaar Package Importer
  • Author(s): Allison Randal
  • Date: 2011-07-30 18:45:03 UTC
  • mfrom: (1.1.13 upstream)
  • Revision ID: james.westby@ubuntu.com-20110730184503-34d4mprtfx6pt5h3
Tags: 3.6.0-1
* New upstream release
* debian/watch:
  - Modified regular expression to capture numbered directory name
    (patch from Dominique Dumont).
* debian/rules:
  - Split build-arch and build-indep, resolving lintian warning.
  - Update path to pbc_disassemble for manpage generation (patch
    from Dominique Dumont).
* debian/patches:
  - Added patch 02_fix_perl_interpreter_path.patch, resolving
    lintian warnings.
* debian/control:
  - Added DM-Upload-Allowed field.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2011, Parrot Foundation.
 
3
 */
 
4
 
 
5
/*
 
6
 
 
7
=head1 NAME
 
8
 
 
9
src/platform/generic/entropy.c
 
10
 
 
11
=head1 DESCRIPTION
 
12
 
 
13
Get some entropy from the system.
 
14
 
 
15
=head2 Functions
 
16
 
 
17
=over 4
 
18
 
 
19
=cut
 
20
 
 
21
*/
 
22
 
 
23
#include "parrot/parrot.h"
 
24
 
 
25
/* HEADERIZER HFILE: none */
 
26
 
 
27
/*
 
28
 
 
29
=item C<void Parrot_get_entropy(PARROT_INTERP)>
 
30
 
 
31
Get one INTVAL worth of entropy from the system.
 
32
 
 
33
=cut
 
34
 
 
35
*/
 
36
 
 
37
INTVAL
 
38
Parrot_get_entropy(PARROT_INTERP) {
 
39
    INTVAL  entropy;
 
40
    size_t  count;
 
41
    FILE   *urand_fh = fopen("/dev/urandom", "r");
 
42
 
 
43
    if (!urand_fh) {
 
44
        const char *msg = "Couldn't open /dev/urandom for reading.";
 
45
        /* This function is called during interp init, so use the GC registry
 
46
         * as a way to figure out interp's initialziedness.
 
47
         */
 
48
        if (interp->gc_registry)
 
49
            Parrot_ex_throw_from_c_args(interp, NULL, 1, msg);
 
50
        else
 
51
            PANIC(interp, msg);
 
52
    }
 
53
    count = fread(&entropy, sizeof (INTVAL), 1, urand_fh);
 
54
    if (count != 1) {
 
55
        const char* msg = "Couldn't read from /dev/urandom.";
 
56
        fclose(urand_fh);
 
57
        if (interp->gc_registry)
 
58
            Parrot_ex_throw_from_c_args(interp, NULL, 1, msg);
 
59
        else
 
60
            PANIC(interp, msg);
 
61
    }
 
62
    fclose(urand_fh);
 
63
    return entropy;
 
64
}
 
65
 
 
66
/*
 
67
 
 
68
=back
 
69
 
 
70
=cut
 
71
 
 
72
*/
 
73
 
 
74
/*
 
75
 * Local variables:
 
76
 *   c-file-style: "parrot"
 
77
 * End:
 
78
 * vim: expandtab shiftwidth=4 cinoptions='\:2=2' :
 
79
 */