守护进程与可可

发布于 2024-12-18 12:00:10 字数 161 浏览 1 评论 0原文

我写了一个 FTP 服务器,它是通过 ftpd.command 从命令行启动的。 现在我想从 Cocoa 应用程序运行该命令。

我想退出应用程序并且命令应保持运行。 当我返回 Cocoa 应用程序时,它应该知道 FTP 服务器是否仍在运行。

有人可以帮助我吗? 多谢! 朱利安

I wrote a FTP Server, witch is started from the command line via ftpd.command.
Now I want to run that command from a Cocoa app.

I want to quit the app and the command should remain running.
And when I return to the Cocoa app, it should know, if the FTP Server is still running.

Is anybody out there who can help me?
Thanks a lot!
Julian

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

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

发布评论

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

评论(3

安穩 2024-12-25 12:00:11

continue

You should be using launchd to handle the FTP server. It is specifically designed for the situation you describe, namely launching and managing background services.

You need to create a launchd configuration file, which can be placed in one of these locations:

  • ~/Library/LaunchAgents: Per-user agents provided by the user.
  • /Library/LaunchAgents: Per-user agents provided by the administrator.
  • /Library/LaunchDaemons: System-wide daemons provided by the
    administrator.

A daemon is a system-wide service of which there is one instance for all clients. An agent is a service that runs on a per-user basis.

launchd Configuration files are in the form of a property list.

You need to create a launchd configuration plist and place it in one of the above locations. You can configure the plist so that launchd runs your service at launch or periodically, or in response to various actions (such as the contents of a folder changing).

To check to see whether or not your job is running, you need to use the Service Management framework. You can ask launchd for the status of your job like so:

CFDictionaryRef jobDict = SMJobCopyDictionary(kSMDomainUserLaunchd, CFSTR("com.your.apps.bundle.id"));

If the job could not be found then jobDict will be NULL.

(If you're using a system-level daemon then you would replace kSMDomainUserLaunchd with kSMDomainSystemLaunchd).

苯莒 2024-12-25 12:00:11

我认为您必须调整您的 FTP 服务器,以便在完成之前它不会“阻止”命令行。我在这里找到了一些示例代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <unistd.h>

void start_daemon(void)
{
    chdir("/");
    setsid();

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    open("/dev/null", O_RDWR);
    dup(STDIN_FILENO);
    dup(STDIN_FILENO);

    for(;;)
        sleep(100);
}

int main(int argc, char *argv[])
{
    pid_t pid;

    if ((pid = fork()) < 0)
    {
        perror("fork() failed");
        return 1;
    }

    if (pid == 0)
        start_daemon();

    printf("Child has PID %i.\n", pid);

    return 0;
}

I think you will have to adjust your FTP server so that it won't "block" the command line until it's finished. I have found some sample code here:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/fcntl.h>
#include <unistd.h>

void start_daemon(void)
{
    chdir("/");
    setsid();

    close(STDIN_FILENO);
    close(STDOUT_FILENO);
    close(STDERR_FILENO);

    open("/dev/null", O_RDWR);
    dup(STDIN_FILENO);
    dup(STDIN_FILENO);

    for(;;)
        sleep(100);
}

int main(int argc, char *argv[])
{
    pid_t pid;

    if ((pid = fork()) < 0)
    {
        perror("fork() failed");
        return 1;
    }

    if (pid == 0)
        start_daemon();

    printf("Child has PID %i.\n", pid);

    return 0;
}
滥情哥ㄟ 2024-12-25 12:00:11

查看 GitHub 上我的项目 TabletMagic 的源代码。它有一个可启动用户空间守护进程的 Cocoa 首选项窗格,此外它还管理一个 launchd 项目。 Cocoa 部分使用外部帮助程序来完成其所有特权任务,这是在第一次启动时自行设置的 - 由 Cocoa 应用程序在特权上下文中请求管理员授权。尽管不是纯可可,但这都是标准程序。

Take a look at the source code for my project TabletMagic on GitHub. It has a Cocoa preference pane which launches a user space daemon, plus it manages a launchd item. The Cocoa portion uses an external helper to do all its privileged tasks, which is self-setuid the first time it's launched - in a privileged context by the Cocoa app asking for admin authorization. This is all standard procedure, despite not being pure Cocoa.

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