开发一个使用xcode在用户登录之前运行的osx守护进程 [英] developing a osx deamon that runs before user login using xcode

查看:80
本文介绍了开发一个使用xcode在用户登录之前运行的osx守护进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Objective-C/xcode开发一个守护程序.

我是Mac World的新手,我可以了解在xcode中选择哪种项目模板以及如何使用它.

我可以获取一个简单而基本的守护程序示例源代码吗?

解决方案

命令行工具"项目足以开始编写守护程序.这样就没有特殊的构建要求,它仅取决于您希望守护程序执行的操作.

编写Mac OS X守护程序的方式与在常规Unix系统上处理它的方式非常相似.因此,请记住以下几点:

  • 非交互式:您不会从用户那里获得直接输入,但由于该过程未连接到终端,您还必须使用syslogd之类的东西进行输出 li>
  • 环境:不要以为它具有特定的当前目录,路径,默认权限或任何其他环境设置-明确设置这些条件
  • 安全性:确保守护程序具有执行其功能所需的最低限度的最低特权,并且没有更多(这本身就是一个巨大的话题)
  • 信号:您将需要捕获并响应某些信号,因为这些信号通常用于过程控制(例如,SIGHUP强制守护程序重新加载其配置文件)

如果您愿意,在Unix守护程序上有一些很好的文字.斯蒂芬斯关于Unix的书也总是很好.

有一些有关Mac的特定信息守护程序,主要是与launchd集成.

典型的守护程序将执行以下操作:

  • 双叉,与父进程分离
  • 设置流程组和有效的UID
  • 安装信号处理程序
  • 设置文件权限umask
  • 将目录更改为工作目录
  • 读取配置文件
  • 打开插座等
  • 进入无限循环来处理请求

Wikipedia文章中也有一个简单守护程序示例. /p>

Hi I am trying to develop a daemon using objective-C/xcode.

I am new to mac world and can I get an idea of what project template to choose in xcode and how to do it.

Can I get a simple and basic daemon sample source code ?

解决方案

The "Command Line Tool" project is sufficient to start writing a daemon. There are no special build requirements as such, it just depends on what you want the daemon to do.

The way to write a Mac OS X daemon is very much like the way you would approach it on a regular Unix system. Accordingly, there are a few things to keep in mind:

  • non-interactive: you don't get direct input from the user, but you also have to use something like syslogd for output, as the process is not attached to a terminal
  • environment: don't assume it has a particular current directory, path, default permissions, or any other environmental settings - explicitly set these up
  • security: ensure the daemon has the barest minimum privileges required to perform its function, and no more (this is a huge topic in itself)
  • signals: you will need to trap and respond to certain signals, as these are typically used for process control (eg. SIGHUP forces the daemon to reload its config file)

There are some good writeup on Unix daemons if you go looking. The Stephens book on Unix is always good, too.

There is some Mac specific information on daemons to consider, mainly regarding integrating with launchd.

A typical daemon will do something like the following:

  • double-fork, to detach from the parent process
  • set up process group and effective UID
  • install signal handlers
  • set file permissions umask
  • change directory to a work directory
  • read configuration file
  • open sockets, etc
  • go into an infinite loop to service requests

There is a simple daemon example in the Wikipedia article too.

这篇关于开发一个使用xcode在用户登录之前运行的osx守护进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆