Hacker News new | past | comments | ask | show | jobs | submit login

> Anyone that likes commodities of UNIX...

This is an untrue generalization. I know people who have a Mac, yet do all of their actual coding within containers set up to use some flavor of Linux and never really touch the underlying OS. Macs DO have solid, reliable hardware underneath, but the insinuation that there is no reason you need to run a true POSIX compliant OS vs MacOS is definitely not a hard and fast truth.




OS X is posix-compliant. It doesn't implement certain GNUisms but depending on any vendor-specific problems is going to be a pain point.


OS X is UNIX 03 certified. That's a formalism.

1) That's a nearly 15-year-old standard (03 == 2003). Windows was certified to the predecessor POSIX standard.

2) While certified, it's compliance in actuality is poor. macOS's certified status says more about flaws in the certification test suite and administrative process, and the OpenGroup's need for income, than it does about macOS. The manual page for pselect, for example, even admits that it's not compliant!

FreeBSD, Linux, NetBSD, OpenBSD and probably DragonflyBSD are all more compliant in a practical sense than macOS, and much more compliant to the latest specification as they implement all or most of the newly defined interfaces, while macOS implements very few.

That said, yeah, macOS is compliant enough as a practical matter, especially compared to Windows. Porting stuff to macOS is about as painful as porting to Solaris or AIX, which is to say not very painful.

The biggest headaches with macOS are the bugs. For example, last week I was adding support for sendmsg/recvmsg and IP_PKTINFO in my Lua unix module and was able to reliably crash the kernel with a single line of unprivileged code:

  https://www.irif.fr/~boutier/mac-crash.html
I was really confused at first. I had to reboot multiple times before I figured out it was my test code and not my hardware dying. After some Googling I found the above link that verified what I was seeing. This bug was only fixed, AFAIK, in macOS 10.12. (I don't have a 10.11 machine to test.) I was testing the code on 10.10, and as of the latest update a couple of days ago 10.10 is still broken AFAIK.

The most famous problem was kqueue not supporting pipes, but that was fixed eons ago. There are alot of other problems with their neworking subsystem, though. For example, shutdown() fails if the associated stream has already entered the equivalent state, either because the sender sent RSET/FIN or shutdown was alreayd called; no other implementation does that. If you send a socket descriptor to another process with sendmsg and then immediately exit the process, the receiving process will receive an unuseable file descriptor. There are lots of stupid quirks like this that make writing reliable, high-availability software for macOS very difficult. Fortunately, few people use macOS for HA server stuff, but I like porting my software to other platforms because it helps catch bugs (bugs that, for w'eve reason, stay hidden when running on Linux), and macOS provides a poor signal/noise ratio in that regard.


> [...] The manual page for pselect, for example, even admits that it's not compliant!

Where does it say that? I'm running 10.12.1, and the man page for pselect(2) looks like the one from FreeBSD. I could believe that pselect doesn't actually behave as documented, having run into my share of macOS bugs.

> [...] Porting stuff to macOS is about as painful as porting to Solaris or AIX, which is to say not very painful [...] The biggest headaches with macOS are the bugs.

Agreed, though AFAIK all commercial UNIXes are buggy in one way or another, and I don't know if macOS is worse in comparison. Autoconf exists for a reason, even though few if any really like it.


  IMPLEMENTATION NOTES
    The pselect() function is implemented in the C library as a wrapper
    around select().
That means it's not actually compliant. pselect needs to _atomically_ unblock signals so that if any signal handlers are invoked pselect returns EINTR error. User code can't emulate the correct semantics because there's a race between unblocking (i.e. changing the signal mask), checking for signal arrival, and calling select. It's _impossible_ to fix this race without resort to pselect, or by use of system extensions like kqueue on BSD/macOS[1] or signalfd on Linux, which can be tricky. On Solaris you just have to use pselect, or resort to the so-called "pipe trick".

So the fact that pselect is a wrapper around select admits that it's not conformant. It's basically useless; worse, it's dangerous because application code calling pselect expecting there won't be a race.

You can see the race with this code, which on 10.10.5 still fails. What should happen is that it exits immediately with OK. What actually happens on macOS is that its pselect wrapper unblocks the signal, which is immediately delivered. But by then we've already checked that interrupted was still 0. It then calls select, which will timeout because it won't be interrupted--the signal was already delivered. On every other platform implementing pselect (which all do it correctly), pselect will always return EINTR because the signal unmasking and delivery happens in the kernel _after_ entering select.

  #include <errno.h>
  #include <signal.h>
  #include <stdio.h>
  #include <sys/select.h>
  #include <unistd.h>
  #include <err.h>
  
  static int interrupted;
  
  static void
  interrupt(int signo)
  {
    interrupted = signo;
  }
  
  int
  main(void)
  {
    struct timespec timeout = { 3, 0 };
    sigset_t empty, block;
    int n;
  
    sigemptyset(&empty);
    sigemptyset(&block);
  
    sigaddset(&block, SIGHUP);
    sigprocmask(SIG_BLOCK, &block, NULL);
  
    signal(SIGHUP, &interrupt);
    raise(SIGHUP);
  
    while (!interrupted) {
      /*
       * If pselect does not atomically unblock signals, pselect
       * will never be interrupted and we'll lose the signal until
       * something else wakes us up AND we remember to check even
       * though we never saw EINTR.
       */
      n = pselect(0, NULL, NULL, NULL, &timeout, &empty);
      if (n == -1 && errno != EINTR) {
        err(1, "pselect");
      } else if (n == 0) {
        errx(1, "timed out");
      }
    }
  
    if (interrupted != SIGHUP)
      errx(1, "wrong signal (got %d, expected %d)",
          interrupted, SIGHUP);
  
    puts("OK");
    
    return 0;  
  }
[1] FWIW, here's a correct implementation of pselect for macOS using kqueue. (https://github.com/wahern/cqueues/blob/snap-20160812/src/cqu...) The only caveat is that creating a kqueue descriptor might fail, and you don't normally expect pselect to fail with EMFILE or ENFILE; it might even make it non-compliant. Except for that caveat, it's a compliant implementation of pselect AFAICT.


Hey, I didn't catch your reply in time, sorry about that.

Anyway, the man page for pselect(2) in 10.12.1 doesn't have that section, and when I run your test program it prints "OK". So it appears that this particular bug was fixed. It only took them 2 years (could be less if it was fixed in 10.11) this time, which was an improvement over their turnaround for fixing the rename(2) bug[0]. :-P

[0] http://www.weirdnet.nl/apple/rename.html


> I like porting my software to other platforms because it helps catch bugs (bugs that, for w'eve reason, stay hidden when running on Linux)

I wish more people subscribed to this - I've banged on about this at previous jobs with little success (even though I've demonstrably caught "mistaken assumption" and other bugs through the exercise.)


From PSELECT(2):

STANDARDS The pselect() function conforms to IEEE Std 1003.1-2001 (``POSIX.1'').


See my reply to RJIb8RBYxzAMX9u, elsethread. It's misleading. The interface conforms, but the implementation is not compliant At least for all the version of macOS I've tested (I haven't tested 10.12, yet) the very problem pselect was created to address exists. When I said that the manual page admits it's non-compliant, I was referring to the section that said pselect was implemented as a wrapper around select; but you can't correctly implement pselect as a simple wrapper around select.

But see my link in the other reply to a correct implementation I wrote that relies on kqueue. I even submitted it to Apple years ago along with the bug report.


Darwin is very much not POSIX-compliant. It may aim to be, but there are so many blatant unfixed bugs (broken poll, broken cmsg, etc.) and missing features (ptrace, etc.) that porting work is frequently necessary.


Yes, it is POSIX compliant. Its also an official UNIX.

http://stackoverflow.com/questions/5785516/is-osx-a-posix-os

https://en.wikipedia.org/wiki/Single_UNIX_Specification

Presence of bugs does not preclude you from being compliant to the spec.

...also... Linux is not officially compliant to either one.


Whether I have to do porting work in practice (which I do, a lot) matters a lot more than what a consortium from 1996 says.

The fact is, Darwin gives me trouble a lot more than Linux does when using POSIX APIs. If there were a comprehensive, up to date POSIX test suite, Darwin would fail it.


Basically just like any other UNIX during the golden days of UNIX wars.


So it is. I was under the impression that its microkernel approach, specifically the system calls directed to Mach, caused it to deviate from the standard. You learn something new every day.




Join us for AI Startup School this June 16-17 in San Francisco!

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: