util.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /* SLiM - Simple Login Manager
  2. Copyright (C) 2009 Eygene Ryabinkin <rea@codelabs.ru>
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. */
  8. #include <sys/types.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <time.h>
  12. #include <unistd.h>
  13. #include "util.h"
  14. extern "C" bool rs_add_mcookie(const char*, const char*, const char*, const char*);
  15. /*
  16. * Adds the given cookie to the specified Xauthority file.
  17. * Returns true on success, false on fault.
  18. */
  19. bool Util::add_mcookie(const std::string &mcookie, const char *display,
  20. const std::string &xauth_cmd, const std::string &authfile)
  21. {
  22. return rs_add_mcookie(mcookie.c_str(), display, xauth_cmd.c_str(), authfile.c_str());
  23. }
  24. /*
  25. * Interface for random number generator. Just now it uses ordinary
  26. * random/srandom routines and serves as a wrapper for them.
  27. */
  28. void Util::srandom(unsigned long seed)
  29. {
  30. ::srandom(seed);
  31. }
  32. long Util::random(void)
  33. {
  34. return ::random();
  35. }
  36. /*
  37. * Makes seed for the srandom() using "random" values obtained from
  38. * getpid(), time(NULL) and others.
  39. */
  40. long Util::makeseed(void)
  41. {
  42. struct timespec ts;
  43. long pid = getpid();
  44. long tm = time(NULL);
  45. if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
  46. ts.tv_sec = ts.tv_nsec = 0;
  47. }
  48. return pid + tm + (ts.tv_sec ^ ts.tv_nsec);
  49. }