You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

70 lines
1.5 KiB

  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. /*
  15. * Adds the given cookie to the specified Xauthority file.
  16. * Returns true on success, false on fault.
  17. */
  18. bool Util::add_mcookie(const std::string &mcookie, const char *display,
  19. const std::string &xauth_cmd, const std::string &authfile)
  20. {
  21. FILE *fp;
  22. std::string cmd = xauth_cmd + " -f " + authfile + " -q";
  23. fp = popen(cmd.c_str(), "w");
  24. if (!fp)
  25. return false;
  26. fprintf(fp, "remove %s\n", display);
  27. fprintf(fp, "add %s %s %s\n", display, ".", mcookie.c_str());
  28. fprintf(fp, "exit\n");
  29. pclose(fp);
  30. return true;
  31. }
  32. /*
  33. * Interface for random number generator. Just now it uses ordinary
  34. * random/srandom routines and serves as a wrapper for them.
  35. */
  36. void Util::srandom(unsigned long seed)
  37. {
  38. ::srandom(seed);
  39. }
  40. long Util::random(void)
  41. {
  42. return ::random();
  43. }
  44. /*
  45. * Makes seed for the srandom() using "random" values obtained from
  46. * getpid(), time(NULL) and others.
  47. */
  48. long Util::makeseed(void)
  49. {
  50. struct timespec ts;
  51. long pid = getpid();
  52. long tm = time(NULL);
  53. if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) {
  54. ts.tv_sec = ts.tv_nsec = 0;
  55. }
  56. return pid + tm + (ts.tv_sec ^ ts.tv_nsec);
  57. }