sys_linux.c 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /* $MirOS: contrib/hosted/fwcf/sys_linux.c,v 1.3 2006/09/26 10:25:03 tg Exp $ */
  2. /*-
  3. * Copyright (c) 2006
  4. * Thorsten Glaser <tg@mirbsd.de>
  5. *
  6. * Licensee is hereby permitted to deal in this work without restric-
  7. * tion, including unlimited rights to use, publicly perform, modify,
  8. * merge, distribute, sell, give away or sublicence, provided all co-
  9. * pyright notices above, these terms and the disclaimer are retained
  10. * in all redistributions or reproduced in accompanying documentation
  11. * or other materials provided with binary redistributions.
  12. *
  13. * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
  14. * express, or implied, to the maximum extent permitted by applicable
  15. * law, without malicious intent or gross negligence; in no event may
  16. * licensor, an author or contributor be held liable for any indirect
  17. * or other damage, or direct damage except proven a consequence of a
  18. * direct error of said person and intended use of this work, loss or
  19. * other issues arising in any way out of its use, even if advised of
  20. * the possibility of such damage or existence of a defect.
  21. */
  22. #include <err.h>
  23. #include <fcntl.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <unistd.h>
  27. #include "defs.h"
  28. #include "sysdeps.h"
  29. __RCSID("$MirOS: contrib/hosted/fwcf/sys_linux.c,v 1.3 2006/09/26 10:25:03 tg Exp $");
  30. void
  31. pull_rndata(uint8_t *buf, size_t n)
  32. {
  33. int fd;
  34. if ((fd = open("/dev/urandom", O_RDONLY)) < 0) {
  35. warn("Cannot open /dev/urandom for %sing", "read");
  36. return;
  37. }
  38. if ((size_t)read(fd, buf, n) != n)
  39. warn("Cannot read %lu bytes from /dev/urandom", (u_long)n);
  40. close(fd);
  41. }
  42. void
  43. push_rndata(uint8_t *buf, size_t n)
  44. {
  45. int fd;
  46. if ((fd = open("/dev/urandom", O_WRONLY)) < 0) {
  47. warn("Cannot open /dev/urandom for %sing", "writ");
  48. return;
  49. }
  50. if ((size_t)write(fd, buf, n) != n)
  51. warn("Cannot write %lu bytes to /dev/urandom", (u_long)n);
  52. close(fd);
  53. }