Service.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. #include "Mutex.hpp"
  35. /**
  36. * Maximum size of a service message in bytes (sanity limit)
  37. */
  38. #define ZT_SERVICE_MAX_MESSAGE_SIZE 131072
  39. namespace ZeroTier {
  40. class RuntimeEnvironment;
  41. #ifndef __WINDOWS__
  42. /**
  43. * A subprocess that communicates with the host via a simple protocol
  44. *
  45. * This is currently only supported on *nix systems, and is used to implement
  46. * special plugins that are used by supernodes and network configuration
  47. * master nodes. Users will probably have no use for it.
  48. *
  49. * The simple binary protocol consists of a bidirectional stream of string-
  50. * serialized Dictionaries prefixed by a 32-bit message length. Input
  51. * messages are sent to the subprocess via its stdin, and output is read
  52. * from its stdout. Messages printed by the subprocess on its stderr are
  53. * logged via the standard Logger instance. If the subprocess dies, an
  54. * attempt is made to restart it every second.
  55. */
  56. class Service
  57. {
  58. public:
  59. /**
  60. * Create and launch a new service
  61. *
  62. * @param renv Runtime environment
  63. * @param name Name of service
  64. * @param path Path to service binary
  65. * @param handler Handler function to call when service generates output
  66. * @param arg First argument to service
  67. */
  68. Service(const RuntimeEnvironment *renv,
  69. const char *name,
  70. const char *path,
  71. void (*handler)(void *,Service &,const Dictionary &),
  72. void *arg);
  73. ~Service();
  74. /**
  75. * Send a message to service subprocess
  76. *
  77. * @param msg Message in key/value dictionary form
  78. * @return True if message was sent
  79. */
  80. bool send(const Dictionary &msg);
  81. /**
  82. * @return Name of service
  83. */
  84. inline const char *name() const
  85. throw()
  86. {
  87. return _name.c_str();
  88. }
  89. /**
  90. * @return True if subprocess is running
  91. */
  92. inline bool running() const
  93. throw()
  94. {
  95. return (_pid > 0);
  96. }
  97. /**
  98. * Thread main method; do not call elsewhere
  99. */
  100. void threadMain()
  101. throw();
  102. private:
  103. const RuntimeEnvironment *_r;
  104. Thread _thread;
  105. std::string _path;
  106. std::string _name;
  107. void *_arg;
  108. void (*_handler)(void *,Service &,const Dictionary &);
  109. long _pid;
  110. int _childStdin;
  111. int _childStdout;
  112. int _childStderr;
  113. volatile bool _run;
  114. };
  115. #endif // __WINDOWS__
  116. } // namespace ZeroTier
  117. #endif