Ambiguity in SysV IPC keys

Ask a Question related to UNIX Programming, Design and Development.

  1. #1

    Default Ambiguity in SysV IPC keys

    In SysV IPC, when you call ftok() to get a key, the key is not
    guaranteed to be unique. How do people code around this potential key
    ambiguity in real life? Do you just count on the probability of a key
    collission being very low and pretend it'll never happen?
    Roy Smith Guest

  2. Similar Questions and Discussions

    1. Ambiguity in result if ini_set
      I find that w.r.t. docs: string ini_set ( string varname, string newvalue) Sets the value of the given configuration option. Returns the old...
    2. ftok() not returning a valid SysV shared memory index?
      I'm running 4.3.3, and have made sure to compile it with --enable-sysvshm. The code works on PHP5, so I think it might be a problem with PHP4. ...
    3. SysV Semaphore Troubles
      On Sun, 03 Aug 2003 12:21:05 -0400, Marc Rochkind wrote: The troubling scenario is when the program increments or decrements the semaphore so...
    4. Dpkg without sysv-rc
      hey, one more question, by a strange set of circumstances, I have sysvinit (testing version), initscripts (testing version) installed, but didn't...
    5. #21788 [Sus->WFx]: array_multisort() changes array keys unexpectedly given numeric strings as keys
      ID: 21788 Updated by: sniper@php.net Reported By: jon at inet-specialists dot com -Status: Suspended +Status: ...
  3. #2

    Default Re: Ambiguity in SysV IPC keys

    On Sat, 19 Jul 2003 14:33:44 +0000, Roy Smith wrote:
    > In SysV IPC, when you call ftok() to get a key, the key is not guaranteed
    > to be unique. How do people code around this potential key ambiguity in
    > real life? Do you just count on the probability of a key collission
    > being very low
    Yes
    > and pretend it'll never happen?
    No. You should still code defensively. Simply performing normal sanity
    checks and refusing to continue if the expected conditions are not
    present is often appropriate and sufficient. For example, validate
    that the owner and permissions on the segment make sense before using
    its contents. Stick a "signature" in the segment and validate that.
    If those sanity checks fail provide the option of using a different
    pathname for the ftok() function or allow the key to be specified
    directly. However, given the very low probability of a ftok() key
    collision it isn't worth performing any action other than those you
    would normally perform even if you were guaranteed a globally unique key.

    Kurtis D. Rader Guest

  4. #3

    Default Re: Ambiguity in SysV IPC keys

    On Sat, 19 Jul 2003 14:33:44 -0400, Roy Smith <roy@panix.com> wrote:
    > In SysV IPC, when you call ftok() to get a key, the key is not guaranteed
    > to be unique.
    [snip]

    The Single UNIX Specification v. 3:

    "The ftok() function shall return the same key value for all paths that
    name the same file, when called with the same id value, and return
    different key values when called with different id values or with paths
    that name different files existing on the same file system at the same
    time. It is unspecified whether ftok() shall return the same key value when
    called again after the file named by path is removed and recreated with the
    same name."

    So, it seems to me that a solution is to follow the rule that the paths
    must all be in the same file system. For example, all of the form
    "/etc/SysVIPC/xyz" where xyz varies.

    Am I missing something?

    It is true that someone who knows nothing of the rule but is using the same
    system could come up with the same key, but presumambly the permissions
    would prevent such a person from doing anything with a SysV IPC object
    obtained with the key.

    --Marc


    Marc Rochkind Guest

  5. #4

    Default Re: Ambiguity in SysV IPC keys

    On Sun, 20 Jul 2003 11:59:01 +0000, Bjorn Reese <breese@mail1.stofanet.dk>
    wrote:

    [snip]
    >
    > Two different paths may map into the same hash value.
    >

    But not on the same file system. That's why a convention that paths be in
    the same directory sorts things out.

    --Marc
    Marc Rochkind Guest

  6. #5

    Default Re: Ambiguity in SysV IPC keys

    Marc Rochkind <rochkind@basepath.com> wrote:
    > But not on the same file system.
    My understanding is that's not guaranteed.
    > That's why a convention that paths be in
    > the same directory sorts things out.
    The same directory as what? I write a program which uses IPC, and you
    do the same. What directory are we supposed to use in common?
    Roy Smith Guest

  7. #6

    Default Re: Ambiguity in SysV IPC keys

    On Sun, 20 Jul 2003 13:26:22 -0400, Roy Smith <roy@panix.com> wrote:
    > Marc Rochkind <rochkind@basepath.com> wrote:
    >> But not on the same file system.
    >
    > My understanding is that's not guaranteed.
    >
    >> That's why a convention that paths be in the same directory sorts things
    >> out.
    >
    > The same directory as what? I write a program which uses IPC, and you do
    > the same. What directory are we supposed to use in common?
    >

    See my earlier msg in this thread...

    The guarantee is in the SUS (quoted in my earlier msg). The convention I
    suggested is that there be a common directory on each system.

    --Marc
    Marc Rochkind Guest

  8. #7

    Default Re: Ambiguity in SysV IPC keys

    On Sun, 20 Jul 2003 12:29:15 -0600, Marc Rochkind
    <rochkind@basepath.com> wrote:
    >On Sun, 20 Jul 2003 13:26:22 -0400, Roy Smith <roy@panix.com> wrote:
    >
    >> Marc Rochkind <rochkind@basepath.com> wrote:
    >>> But not on the same file system.
    >>
    >> My understanding is that's not guaranteed.
    >>
    >>> That's why a convention that paths be in the same directory sorts things
    >>> out.
    >>
    >> The same directory as what? I write a program which uses IPC, and you do
    >> the same. What directory are we supposed to use in common?
    >>
    >
    >
    >See my earlier msg in this thread...
    >
    >The guarantee is in the SUS (quoted in my earlier msg). The convention I
    >suggested is that there be a common directory on each system.
    I haven't checked over the SUSv3 wording, but I don't think that most
    implementations would gauantee uniqueness following your convention
    Marc. A typical implementation use just the lowest 16 bytes of the
    i-node number, rather than the whole number, so there is clealrly the
    possibility for two paths to generate the same key from ftok().

    Cheers

    Michael
    Michael Kerrisk Guest

  9. #8

    Default Re: Ambiguity in SysV IPC keys

    On Sat, 19 Jul 2003 13:20:21 -0700, "Kurtis D. Rader"
    <krader@skepticism.us> wrote:
    >On Sat, 19 Jul 2003 14:33:44 +0000, Roy Smith wrote:
    >
    >> In SysV IPC, when you call ftok() to get a key, the key is not guaranteed
    >> to be unique. How do people code around this potential key ambiguity in
    >> real life? Do you just count on the probability of a key collission
    >> being very low
    >
    >Yes
    >
    >> and pretend it'll never happen?
    >
    >No. You should still code defensively. Simply performing normal sanity
    >checks and refusing to continue if the expected conditions are not
    >present is often appropriate and sufficient. For example, validate
    >that the owner and permissions on the segment make sense before using
    >its contents. Stick a "signature" in the segment and validate that.
    (Minor quibble: a signature isn't possible with semaphores or message
    queues.)
    >If those sanity checks fail provide the option of using a different
    >pathname for the ftok() function or allow the key to be specified
    >directly. However, given the very low probability of a ftok() key
    >collision it isn't worth performing any action other than those you
    >would normally perform even if you were guaranteed a globally unique key.
    Another possibility to consider: abandon ftok() altogether. Use the
    IPC_PRIVATE key to create the object and then write the object
    identifier to a file which is read by all other applications requiring
    access to the object.

    Cheers,

    Michael
    Michael Kerrisk Guest

  10. #9

    Default Re: Ambiguity in SysV IPC keys

    In article <ei3nhv0ac4bsticebbdinlcsffkgkfbh38@4ax.com>,
    Michael Kerrisk <michael.kerrisk@freenet.de> wrote:
    > Another possibility to consider: abandon ftok() altogether. Use the
    > IPC_PRIVATE key to create the object and then write the object
    > identifier to a file which is read by all other applications requiring
    > access to the object.
    I like this idea a lot. Seems like it would also solve a lot of the
    semaphore race conditions Stevens talks about.
    Roy Smith Guest

  11. #10

    Default Re: Ambiguity in SysV IPC keys

    Marc Rochkind <rochkind@basepath.com> writes:
    > On Sun, 20 Jul 2003 13:26:22 -0400, Roy Smith <roy@panix.com> wrote:
    >
    > > Marc Rochkind <rochkind@basepath.com> wrote:
    > >> But not on the same file system.
    > >
    > > My understanding is that's not guaranteed.
    > >
    > >> That's why a convention that paths be in the same directory sorts things
    > >> out.
    > >
    > > The same directory as what? I write a program which uses IPC, and you do
    > > the same. What directory are we supposed to use in common?
    > >
    >
    >
    > See my earlier msg in this thread...
    >
    > The guarantee is in the SUS (quoted in my earlier msg). The convention I
    > suggested is that there be a common directory on each system.
    >
    Like lost+found :-) ?

    Bye, Dragan

    --
    Dragan Cvetkovic,

    To be or not to be is true. G. Boole No it isn't. L. E. J. Brouwer

    !!! Sender/From address is bogus. Use reply-to one !!!
    Dragan Cvetkovic Guest

  12. #11

    Default Re: Ambiguity in SysV IPC keys

    In article <ei3nhv0ac4bsticebbdinlcsffkgkfbh38@4ax.com>,
    [email]michael.kerrisk@freenet.de[/email] says...
    > Another possibility to consider: abandon ftok() altogether. Use the
    > IPC_PRIVATE key to create the object and then write the object
    > identifier to a file which is read by all other applications requiring
    > access to the object.
    >
    >
    Works great. Another possibility, if you have cooperating tasks, is just
    to predefine the keys in an include file.

    --
    Where ARE those Iraqi WMDs?
    Larry Blanchard Guest

  13. #12

    Default Re: Ambiguity in SysV IPC keys

    Larry Blanchard wrote:
    >
    > In article <ei3nhv0ac4bsticebbdinlcsffkgkfbh38@4ax.com>,
    > [email]michael.kerrisk@freenet.de[/email] says...
    > > Another possibility to consider: abandon ftok() altogether. Use the
    > > IPC_PRIVATE key to create the object and then write the object
    > > identifier to a file which is read by all other applications requiring
    > > access to the object.
    > >
    > >
    > Works great. Another possibility, if you have cooperating tasks, is just
    > to predefine the keys in an include file.
    ... and hope that no other application happens to use the
    same keys. One "other application" that's particularly likely
    to do so is a second instance of the same program.

    --
    [email]Eric.Sosman@sun.com[/email]
    Eric Sosman Guest

  14. #13

    Default Re: Ambiguity in SysV IPC keys

    On Mon, 21 Jul 2003 08:03:22 -0400, Roy Smith <roy@panix.com> wrote:
    >In article <ei3nhv0ac4bsticebbdinlcsffkgkfbh38@4ax.com>,
    > Michael Kerrisk <michael.kerrisk@freenet.de> wrote:
    >
    >> Another possibility to consider: abandon ftok() altogether. Use the
    >> IPC_PRIVATE key to create the object and then write the object
    >> identifier to a file which is read by all other applications requiring
    >> access to the object.
    >
    >I like this idea a lot. Seems like it would also solve a lot of the
    >semaphore race conditions Stevens talks about.
    No -- I don't think this really helps you with the race condition, but
    it is tangentially related: one thing that you need to be aware ofis
    that Stevens's solution to the race is not needed in all cases. If
    one process can be guaranteed (by other means) to be the first to
    create and initialize the semaphore set, then this can simply be done
    using a single calls to semget() and semop(). If your application is
    one where the IPC_PRIVATE technique can be applied, then probably also
    you create/initialize in this simple way.

    The solution given by Steven's is needed when there are multiple
    *peer* processes, that is, any one of a number of (i.e., two or more)
    processes may be the one to create and initalize the semaphore. If
    this is the case, then it's also likely that the IPC_PRIVATE technique
    will at least take a bit more effort to be workable (since any of the
    peers may be the one to create the file used to record the semaphore
    ID).

    Cheers

    Michael
    Michael Kerrisk Guest

  15. #14

    Default Re: Ambiguity in SysV IPC keys

    In article <3F1C15AB.8F159CB9@sun.com>, [email]Eric.Sosman@sun.com[/email] says...
    > Larry Blanchard wrote:
    > >
    > > >
    > > Works great. Another possibility, if you have cooperating tasks, is just
    > > to predefine the keys in an include file.
    >
    > ... and hope that no other application happens to use the
    > same keys. One "other application" that's particularly likely
    > to do so is a second instance of the same program.
    >
    I said "cooperating", didn't I? The applications I've used this in were
    all dedicated process control and/or data acquisition systems :-).

    --
    Where ARE those Iraqi WMDs?
    Larry Blanchard Guest

  16. #15

    Default Re: Ambiguity in SysV IPC keys

    In article <3F1D60CA.A3B5E5B9@sun.com>, [email]Eric.Sosman@sun.com[/email] says...
    > Larry Blanchard wrote:
    > >
    > > I said "cooperating", didn't I? The applications I've used this in were
    > > all dedicated process control and/or data acquisition systems :-).
    >
    > Ah, a difference in viewpoint. I've been burned far
    > too often by applications originally written for single-
    > user systems and then ported to multi-user systems.
    >
    Oh these were multi-user. It's just that the users were factory floor
    folks and they don't write programs :-).

    --
    Where ARE those Iraqi WMDs?
    Larry Blanchard Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139