15 static random_fn random_generate;
16 static bool_t random_generate(void *data, uint32_t bytes, uint8_t *buff)
18 struct rgen_data *st=data;
20 /* XXX XXX error checking */
21 read(st->fd,buff,bytes);
26 static list_t *random_apply(closure_t *self, struct cloc loc,
27 dict_t *context, list_t *args)
31 string_t filename=NULL;
33 st=safe_malloc(sizeof(*st),"random_apply");
35 st->cl.description="randomsource";
36 st->cl.type=CL_RANDOMSRC;
38 st->cl.interface=&st->ops;
40 st->ops.blocking=False;
41 st->ops.generate=random_generate;
44 arg1=list_elem(args,0);
45 arg2=list_elem(args,1);
48 fatal("randomsource: requires a filename\n");
50 if (arg1->type != t_string) {
51 cfgfatal(arg1->loc,"randomsource",
52 "filename (arg1) must be a string\n");
54 filename=arg1->data.string;
57 if (arg2->type != t_bool) {
58 cfgfatal(arg2->loc,"randomsource",
59 "blocking parameter (arg2) must be bool\n");
61 st->ops.blocking=arg2->data.bool;
65 fatal("randomsource requires a filename");
67 st->fd=open(filename,O_RDONLY);
69 fatal_perror("randomsource (%s:%d): cannot open %s",arg1->loc.file,
70 arg1->loc.line,filename);
72 return new_closure(&st->cl);
75 void random_module(dict_t *d)
77 add_closure(d,"randomfile",random_apply);