2

I'm trying to construct a block of shared memory on iOS. It compiles and links, but shmget() throws a SIGSYS signal on the most innocent parameters:

NSString *p = [[NSBundle mainBundle] pathForResource:@"crash" ofType: nil];
key_t tok = ftok([p UTF8String], 918273);
int mid = shmget(tok, 4096, IPC_CREAT|S_IRUSR|S_IWUSR);

tok is a large positive integer, not -1. Size - tried 1024, same effect.

Is SysV shared memory even supported on iOS? The headers and the libraries are present, or compiler/linker would complain. Same code works on the simulator. What am I doing wrong?

My interest stems from this question.

Community
  • 1
  • 1
Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • I still can't work out why you feel the need to use shared memory on a system that doesn't properly support multitasking. – tc. Jul 21 '12 at 03:46
  • Want a storage that a) persists beyond process shutdown b) does not need to persist beyong device restart c) is fast and cheap d) is on the order of 1KB. Found some workarounds, but shared memory would be the cheapest of them all, if it worked. – Seva Alekseyev Jul 21 '12 at 04:04
  • Persisting across app shutdown but not device shutdown seems wrong. Device restart should be transparent to apps... – R.. GitHub STOP HELPING ICE Jul 21 '12 at 04:08
  • It's more normal to use NSUserDefaults, but a `mmap()`ed file probably would be similar to SHM, depending on how often the kernel schedules writeback. – tc. Jul 21 '12 at 04:16
  • @R..: tell that to the clipboard. By the way, private named UIPasteboard is one of the workarounds I found... – Seva Alekseyev Jul 21 '12 at 04:26
  • Why should the clipboard be lost if the device powers down? That's annoying. What if you drop your phone or accidentally let the battery run down while it has something important you want to paste? The clipboard contents should still be there when you get power back. This is basic common-sense interface design with the principle of least surprise... – R.. GitHub STOP HELPING ICE Jul 21 '12 at 13:41
  • @JensGustedt: It's Objective C, which unlike C++ is basically just a preprocessor and runtime environment on top of C, and which thankfully seems to be aligned with the underlying C standard. – R.. GitHub STOP HELPING ICE Jul 21 '12 at 13:49
  • My app is in Objective C++, but that's irrelevant to the question. The SysV shared memory mechanism is completely C friendly, predates object oriented programming as a concept, and is sometimes (incorrectly) considered "a part" of the *nix flavor of C. – Seva Alekseyev Jul 21 '12 at 14:44
  • @SevaAlekseyev, then either tag your question correctly or "translate" your example to C. – Jens Gustedt Jul 21 '12 at 16:27

2 Answers2

2

The shm_open()/mmap() combo works as advertised, both on simulator and on device (tested on iOS 4) without any explicit permission changes.

Note: shm_open() is weirdly documented as variadic. In reality, you need to specify a third parameter with an access mask - a combination of S_IRUSR-like flags, or an octal chmod value.

EDIT: looks like it broke in iOS 7. shm_open returns -1 with errno=2 (ENOENT) even though O_CREAT flag is specified.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
1

On iOS you cannot use shared memory at all I don't know the exact details but I do know that a) its for security and b) its part of the sandboxing environment

So I know above from myself and my presence in the jailbreak scene however here are a few links describing sandboxing and how they affect shared memory

http://www.trailofbits.com/resources/ios4_security_evaluation_paper.pdf

http://lists.apple.com/archives/cocoa-dev/2012/Apr/msg00535.html

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
DanZimm
  • 2,528
  • 2
  • 19
  • 27
  • The cited article states the opposite: "The “container” sandbox profile is the profile that gets applied to all third-party applications. (...) Finally, the profile `does not` restrict actions related to POSIX semaphores, shared memory, file IOCTLs, Mach bootstrap servers, network socket binding and accepting inbound connections, certain classes of privileged actions, and reading kernel state information through the kernel sysctl interface." The linked forum post just restates the problem. Sorry, no accept for you. – Seva Alekseyev Jul 23 '12 at 16:26
  • right, if you read further, it doesnt restrict it if you /add/ the permission to the entitlements file, however on idevice those entitlements do not work (at least in my experience) – DanZimm Jul 24 '12 at 03:47
  • the best i can get other than that is this: http://www.opensource.apple.com/source/mDNSResponder/mDNSResponder-320.5/mDNSMacOSX/mDNSResponder.sb?txt an actual example of it being used – DanZimm Jul 24 '12 at 03:57
  • That's system code, I believe. They get different permissions than the third party sandbox. – Seva Alekseyev Jul 25 '12 at 15:41