THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
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.

99 lines
3.1KB

  1. #ifndef SUBPROCESS_H
  2. #define SUBPROCESS_H
  3. #include "git-compat-util.h"
  4. #include "hashmap.h"
  5. #include "run-command.h"
  6. /*
  7. * The sub-process API makes it possible to run background sub-processes
  8. * for the entire lifetime of a Git invocation. If Git needs to communicate
  9. * with an external process multiple times, then this can reduces the process
  10. * invocation overhead. Git and the sub-process communicate through stdin and
  11. * stdout.
  12. *
  13. * The sub-processes are kept in a hashmap by command name and looked up
  14. * via the subprocess_find_entry function. If an existing instance can not
  15. * be found then a new process should be created and started. When the
  16. * parent git command terminates, all sub-processes are also terminated.
  17. *
  18. * This API is based on the run-command API.
  19. */
  20. /* data structures */
  21. /* Members should not be accessed directly. */
  22. struct subprocess_entry {
  23. struct hashmap_entry ent;
  24. const char *cmd;
  25. struct child_process process;
  26. };
  27. struct subprocess_capability {
  28. const char *name;
  29. /*
  30. * subprocess_handshake will "|=" this value to supported_capabilities
  31. * if the server reports that it supports this capability.
  32. */
  33. unsigned int flag;
  34. };
  35. /* subprocess functions */
  36. /* Function to test two subprocess hashmap entries for equality. */
  37. int cmd2process_cmp(const void *unused_cmp_data,
  38. const struct hashmap_entry *e,
  39. const struct hashmap_entry *entry_or_key,
  40. const void *unused_keydata);
  41. /*
  42. * User-supplied function to initialize the sub-process. This is
  43. * typically used to negotiate the interface version and capabilities.
  44. */
  45. typedef int(*subprocess_start_fn)(struct subprocess_entry *entry);
  46. /* Start a subprocess and add it to the subprocess hashmap. */
  47. int subprocess_start(struct hashmap *hashmap, struct subprocess_entry *entry, const char *cmd,
  48. subprocess_start_fn startfn);
  49. /* Kill a subprocess and remove it from the subprocess hashmap. */
  50. void subprocess_stop(struct hashmap *hashmap, struct subprocess_entry *entry);
  51. /* Find a subprocess in the subprocess hashmap. */
  52. struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const char *cmd);
  53. /* subprocess helper functions */
  54. /* Get the underlying `struct child_process` from a subprocess. */
  55. static inline struct child_process *subprocess_get_child_process(
  56. struct subprocess_entry *entry)
  57. {
  58. return &entry->process;
  59. }
  60. /*
  61. * Perform the version and capability negotiation as described in the
  62. * "Handshake" section of long-running-process-protocol.txt using the
  63. * given requested versions and capabilities. The "versions" and "capabilities"
  64. * parameters are arrays terminated by a 0 or blank struct.
  65. *
  66. * This function is typically called when a subprocess is started (as part of
  67. * the "startfn" passed to subprocess_start).
  68. */
  69. int subprocess_handshake(struct subprocess_entry *entry,
  70. const char *welcome_prefix,
  71. int *versions,
  72. int *chosen_version,
  73. struct subprocess_capability *capabilities,
  74. unsigned int *supported_capabilities);
  75. /*
  76. * Helper function that will read packets looking for "status=<foo>"
  77. * key/value pairs and return the value from the last "status" packet
  78. */
  79. int subprocess_read_status(int fd, struct strbuf *status);
  80. #endif