使用 _execv() 调用子进程时 Windows 服务退出

发布于 2024-10-07 00:45:43 字数 297 浏览 2 评论 0原文

我有一个 C++ Windows 应用程序,它被设计为 Windows 服务。它定期执行更新程序以查看是否有新版本。要执行更新程序,请使用_execv()。更新程序会查找新版本、下载它们并停止 Windows 服务(所有这些操作都会被记录)、替换文件并再次启动服务。在 CLI 模式(不进入服务模式)下执行此操作可以正常工作。根据我的日志文件,子进程已启动,但父进程(Windows 服务)退出。

它甚至“允许”在 Windows 服务中启动子进程吗?为什么服务会意外退出呢?我的日志文件没有显示错误(我什至正在监视写入日志的段错误等)。

I have a C++ Windows application that was designed to be a Windows service. It executes an updater periodically to see if there's a new version. To execute the updater, _execv() is used. The updater looks for new versions, downloads them and stops the Windows service (all of these actions are logged), replaces the files, and starts the service again. Doing that in CLI mode (not going into service mode) works fine that way. According to my log files, the child process is launched, but the parent process (the Windows service) exits.

Is it even "allowed" to launch child processes in Windows services, and, why does the service exit unexpected then? My log files show no error (I am even monitoring for segfaults etc which is written to the log).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

违心° 2024-10-14 00:45:43

_execv 将现有进程替换为运行作为参数传递的文件的新进程。在 Unix(和类似的)下,这是直接/本机处理的。然而,Windows 并不直接支持这一点——因此它是通过让父进程退出并安排子进程在退出时立即启动来完成的。

IOW,听起来 _execv 正在完全按照其设计目的进行操作 - 但在这种情况下,它可能不是您真正想要的。您可以从服务生成进程,但通常希望使用 CreateProcessAsUser 在指定帐户而不是服务帐户(分配有一组相当不寻常的权限)下创建它。当更新程序调用 ControlServiceCreateService

_execv replaces the existing process with a new one running the file you pass as the parameter. Under Unix (and similar) that's handled directly/natively. Windows, however, doesn't support that directly -- so it's done by having the parent process exit and arrange for a child process to be started as soon as it does.

IOW, it sounds like _execv is doing exactly what it's designed to -- but in this case, it's probably not what you really want. You can spawn a process from a service, but you generally want to use CreateProcessAsUser to create it under a specified account instead of the service account (which has a rather unusual set of rights assigned to it). The service process will then exit and restart when it's asked to by the service manager when your updater calls ControlService, CreateService, etc.

最美的太阳 2024-10-14 00:45:43

为什么使用 _execv() 而不是按照 Windows 方式并使用 CreateProcess()

我假设您已经在服务中进行了一些调试,并且您没有超越在服务中调用 _execv() 的点?

Why are you using _execv() rather than doing it the windows way and using CreateProcess()?

I assume you've put some debug into your service and you aren't getting past the point where you call _execv() in your service?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文