llprocesslauncher.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**
  2. * @file llprocesslauncher.h
  3. * @brief Utility class for launching, terminating, and tracking the state of
  4. * processes.
  5. *
  6. * $LicenseInfo:firstyear=2008&license=viewergpl$
  7. *
  8. * Copyright (c) 2008-2009, Linden Research, Inc.
  9. *
  10. * Second Life Viewer Source Code
  11. * The source code in this file ("Source Code") is provided by Linden Lab
  12. * to you under the terms of the GNU General Public License, version 2.0
  13. * ("GPL"), unless you have obtained a separate licensing agreement
  14. * ("Other License"), formally executed by you and Linden Lab. Terms of
  15. * the GPL can be found in doc/GPL-license.txt in this distribution, or
  16. * online at http://secondlifegrid.net/programs/open_source/licensing/gplv2
  17. *
  18. * There are special exceptions to the terms and conditions of the GPL as
  19. * it is applied to this Source Code. View the full text of the exception
  20. * in the file doc/FLOSS-exception.txt in this software distribution, or
  21. * online at
  22. * http://secondlifegrid.net/programs/open_source/licensing/flossexception
  23. *
  24. * By copying, modifying or distributing this software, you acknowledge
  25. * that you have read and understood your obligations described above,
  26. * and agree to abide by those obligations.
  27. *
  28. * ALL LINDEN LAB SOURCE CODE IS PROVIDED "AS IS." LINDEN LAB MAKES NO
  29. * WARRANTIES, EXPRESS, IMPLIED OR OTHERWISE, REGARDING ITS ACCURACY,
  30. * COMPLETENESS OR PERFORMANCE.
  31. * $/LicenseInfo$
  32. */
  33. #ifndef LL_LLPROCESSLAUNCHER_H
  34. #define LL_LLPROCESSLAUNCHER_H
  35. #if LL_WINDOWS
  36. # include <windows.h>
  37. #endif
  38. #include "llpreprocessor.h"
  39. #include <string>
  40. #include <vector>
  41. /*
  42. * LLProcessLauncher handles launching external processes with specified
  43. * command line arguments. It also keeps track of whether the process is still
  44. * running, and can kill it if required.
  45. */
  46. class LLProcessLauncher
  47. {
  48. protected:
  49. LOG_CLASS(LLProcessLauncher);
  50. public:
  51. LLProcessLauncher();
  52. virtual ~LLProcessLauncher();
  53. LL_INLINE void setExecutable(const std::string& filename)
  54. {
  55. mExecutable = filename;
  56. }
  57. LL_INLINE void setWorkingDirectory(const std::string& dir)
  58. {
  59. mWorkingDir = dir;
  60. }
  61. LL_INLINE void clearArguments() { mLaunchArguments.clear(); }
  62. LL_INLINE void addArgument(const std::string& arg)
  63. {
  64. mLaunchArguments.push_back(arg);
  65. }
  66. LL_INLINE void addArgument(const char* arg)
  67. {
  68. mLaunchArguments.push_back(std::string(arg));
  69. }
  70. S32 launch();
  71. bool isRunning();
  72. // Attempt to kill the process. Returns true if the process is no longer
  73. // running when it returns. Note that even if this returns false, the
  74. // process may exit some time after it is called.
  75. bool kill();
  76. // Use this if you want the external process to continue execution after
  77. // the LLProcessLauncher instance controlling it is deleted. Normally, the
  78. // destructor will attempt to kill the process and wait for termination.
  79. // This should only be used if the viewer is about to exit, otherwise the
  80. // child process will become a zombie after it exits.
  81. void orphan();
  82. // This needs to be called periodically on Mac/Linux to clean up zombie
  83. // processes.
  84. static void reap();
  85. // Accessors for platform-specific process ID
  86. #if LL_WINDOWS
  87. LL_INLINE HANDLE getProcessHandle() { return mProcessHandle; }
  88. #else
  89. LL_INLINE pid_t getProcessID() { return mProcessID; }
  90. #endif
  91. private:
  92. #if LL_WINDOWS
  93. HANDLE mProcessHandle;
  94. #else
  95. pid_t mProcessID;
  96. #endif
  97. std::string mExecutable;
  98. std::string mWorkingDir;
  99. std::vector<std::string> mLaunchArguments;
  100. };
  101. #endif // LL_LLPROCESSLAUNCHER_H