使用 _execv() 调用子进程时 Windows 服务退出
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
_execv
将现有进程替换为运行作为参数传递的文件的新进程。在 Unix(和类似的)下,这是直接/本机处理的。然而,Windows 并不直接支持这一点——因此它是通过让父进程退出并安排子进程在退出时立即启动来完成的。IOW,听起来
_execv
正在完全按照其设计目的进行操作 - 但在这种情况下,它可能不是您真正想要的。您可以从服务生成进程,但通常希望使用CreateProcessAsUser
在指定帐户而不是服务帐户(分配有一组相当不寻常的权限)下创建它。当更新程序调用ControlService
,CreateService
等_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 useCreateProcessAsUser
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 callsControlService
,CreateService
, etc.为什么使用
_execv()
而不是按照 Windows 方式并使用CreateProcess()
?我假设您已经在服务中进行了一些调试,并且您没有超越在服务中调用
_execv()
的点?Why are you using
_execv()
rather than doing it the windows way and usingCreateProcess()
?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?