18 static random_fn random_generate;
19 static bool_t random_generate(void *data, uint32_t bytes, uint8_t *buff)
21 struct rgen_data *st=data;
23 /* XXX XXX error checking */
24 read(st->fd,buff,bytes);
29 static list_t *random_apply(closure_t *self, struct cloc loc,
30 dict_t *context, list_t *args)
34 string_t filename=NULL;
36 st=safe_malloc(sizeof(*st),"random_apply");
38 st->cl.description="randomsource";
39 st->cl.type=CL_RANDOMSRC;
41 st->cl.interface=&st->ops;
43 st->ops.blocking=False;
44 st->ops.generate=random_generate;
47 arg1=list_elem(args,0);
48 arg2=list_elem(args,1);
51 fatal("randomsource: requires a filename\n");
53 if (arg1->type != t_string) {
54 cfgfatal(arg1->loc,"randomsource",
55 "filename (arg1) must be a string\n");
57 filename=arg1->data.string;
60 if (arg2->type != t_bool) {
61 cfgfatal(arg2->loc,"randomsource",
62 "blocking parameter (arg2) must be bool\n");
64 st->ops.blocking=arg2->data.bool;
68 fatal("randomsource requires a filename");
70 st->fd=open(filename,O_RDONLY);
72 fatal_perror("randomsource (%s:%d): cannot open %s",arg1->loc.file,
73 arg1->loc.line,filename);
75 return new_closure(&st->cl);
78 void random_module(dict_t *d)
80 add_closure(d,"randomfile",random_apply);