跨用户和跨进程共享设置 [英] Cross-user and cross-process shared settings

查看:90
本文介绍了跨用户和跨进程共享设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个进程 - 用户进程和根级别的LaunchDaemon。我希望两个进程都有共享设置。我试图让这通过一个sqlite数据库工作,但已经遇到腐败问题。我已经考虑过使用 NSUserDefaults ,但是 NSGlobalDomain 似乎只对用户是全局的,我需要一个跨用户持久性域, NSUserDefaults 似乎不提供。



我试过读写一个XML文件直接,我可以得到这个工作正常与多线程(通过一个简单的 NSLock ),但是当我尝试应用 O_EXLOCK



<$ c>

$ c> CFPreferences
似乎有大部分与 NSUserDefaults 相同的问题。有一个 kCFPreferencesAnyUser 常量,但文档说,我只能使用,如果我有管理员权限(用户进程没有)。



所以我的问题是:



如何有效地实现跨进程和跨用户共享设置?

$你最好的选择可能是 O_EXLOCK 方法,为了方便起见,它被封装在一个NSFileHandle中 code> -initWithFileDescriptor:)。类似这样的东西(未测试;未编译):



写作:

  int fd = open([path UTF8String],O_TRUNC | O_EXLOCK); 
if(fd> = 0)
{
NSFileHandle * fh = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES];
[fh writeData:xmlData]; //为读者创建xmlData作为练习
[fh release];
}
else //错误的东西

阅读:

  int fd = open([path UTF8String],O_RDONLY | O_SHLOCK); 
if(fd> = 0)
{
NSFileHandle * fh = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES];
NSData * xmlData = [fh readDataToEndOfFile];
[fh release];
}
else //错误的东西

你需要使用O_NONBLOCK或将它们放在非UI线程,所以你的GUI应用程序不会滩。



使用O_EXLOCK遇到什么问题? p>

I have two processes - a user process and a root-level LaunchDaemon. I'd like both processes to have shared settings. I've tried getting this to work via a sqlite database, but have run into corruption issues. I've thought about using NSUserDefaults, but the NSGlobalDomain seems to only be global for the user, and I need a cross-user persistent domain, which NSUserDefaults doesn't seem to provide.

I've tried reading and writing an XML file directly, and I can get this to work fine with multiple threads (via a simple NSLock), but when I attempt to apply an O_EXLOCK to the file to prevent one process from writing to the file while the other is, it doesn't seem to work.

CFPreferences seems to have most of the same issues as NSUserDefaults. There is a kCFPreferencesAnyUser constant, but the documentation says that I can only use that if I have admin privileges (which the user process does not have).

So my question is this:

How can I effectively implement cross-process and cross-user shared settings?

解决方案

Your best bet is probably the O_EXLOCK approach, wrapped into an NSFileHandle for convenience (-initWithFileDescriptor:). Something like this (untested; uncompiled):

Writing:

int fd = open([path UTF8String], O_TRUNC|O_EXLOCK);
if (fd >= 0)
{
    NSFileHandle *fh = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES];
    [fh writeData:xmlData]; // Creating xmlData left as exercise for the reader
    [fh release];
}
else // Error stuff

Reading:

int fd = open([path UTF8String], O_RDONLY|O_SHLOCK);
if (fd >= 0)
{
    NSFileHandle *fh = [[NSFileHandle alloc] initWithFileDescriptor:fd closeOnDealloc:YES];
    NSData *xmlData = [fh readDataToEndOfFile];
    [fh release];
}
else // Error stuff

Of course these will block, so you need to use O_NONBLOCK or put them on a non-UI thread so your GUI app doesn't beachball.

What's the problem you're seeing with O_EXLOCK?

这篇关于跨用户和跨进程共享设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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