Service.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_SERVICE_HPP
  28. #define ZT_SERVICE_HPP
  29. #include <string>
  30. #include <stdexcept>
  31. #include "Constants.hpp"
  32. #include "Dictionary.hpp"
  33. #include "Thread.hpp"
  34. namespace ZeroTier {
  35. class RuntimeEnvironment;
  36. #ifndef __WINDOWS__
  37. /**
  38. * A subprocess that communicates with the host via a simple protocol
  39. *
  40. * This is currently only supported on *nix systems, and is used to implement
  41. * special plugins that are used by supernodes and network configuration
  42. * master nodes. Users will probably have no use for it.
  43. *
  44. * The simple binary protocol consists of a bidirectional stream of string-
  45. * serialized Dictionaries prefixed by a 32-bit message length. Input
  46. * messages are sent to the subprocess via its stdin, and output is read
  47. * from its stdout. Messages printed by the subprocess on its stderr are
  48. * logged via the standard Logger instance. If the subprocess dies, an
  49. * attempt is made to restart it every second.
  50. */
  51. class Service
  52. {
  53. public:
  54. /**
  55. * Create and launch a new service
  56. *
  57. * @param renv Runtime environment
  58. * @param name Name of service
  59. * @param path Path to service binary
  60. * @param handler Handler function to call when service generates output
  61. * @param arg First argument to service
  62. */
  63. Service(const RuntimeEnvironment *renv,
  64. const char *name,
  65. const char *path,
  66. void (*handler)(void *,Service &,const Dictionary &),
  67. void *arg);
  68. ~Service();
  69. /**
  70. * Send a message to service subprocess
  71. *
  72. * @param msg Message in key/value dictionary form
  73. * @return True if message was sent
  74. */
  75. bool send(const Dictionary &msg);
  76. /**
  77. * @return Name of service
  78. */
  79. inline const char *name() const throw() { return _name.c_str(); }
  80. /**
  81. * @return True if subprocess is running
  82. */
  83. inline bool running() const throw() { return (_pid > 0); }
  84. /**
  85. * Thread main method; do not call elsewhere
  86. */
  87. void threadMain()
  88. throw();
  89. private:
  90. const RuntimeEnvironment *RR;
  91. Thread _thread;
  92. std::string _path;
  93. std::string _name;
  94. void *_arg;
  95. void (*_handler)(void *,Service &,const Dictionary &);
  96. volatile long _pid;
  97. volatile int _childStdin;
  98. volatile int _childStdout;
  99. volatile int _childStderr;
  100. volatile bool _run;
  101. };
  102. #endif // __WINDOWS__
  103. } // namespace ZeroTier
  104. #endif