-1

Documentation for mq_unlink says

ENAMETOOLONG name was too long.

but what is this limit? I thought it is NAME_MAX, but it's not. The following code runs forever (as long as there is memory, I guess).

#include <mqueue.h>
#include <string>
#include <errno.h>
#include <unistd.h>

int main(void)
{
    std::string tooLong = "long";
    do
    {
        usleep(10);
        tooLong.append("longer");
        mq_unlink(tooLong.c_str());
    }
    while(errno != ENAMETOOLONG);
}

So what is the limit? When does this function return ENAMETOOLONG?

TriskalJM
  • 2,393
  • 1
  • 19
  • 20
kuga
  • 1,483
  • 1
  • 17
  • 38
  • You should check the return value of the function before you assume there's an error. It also seems like your name may be in the incorrect format. http://man7.org/linux/man-pages/man7/mq_overview.7.html – Retired Ninja Jun 13 '17 at 13:01
  • as far as I can test, it stops when the string becomes 257 characters long, i.e. longer than `NAME_MAX` of 255. – ilkkachu Jun 13 '17 at 13:21

1 Answers1

0

Thanks, you are right.

The problem was the missing slash!

#include <mqueue.h>
#include <string>
#include <errno.h>
#include <unistd.h>
#include <iostream>
#include <limits.h>

int main(void)
{
    std::string tooLong = "/long";
    do
    {
        usleep(10);
        tooLong.append("longer");
        mq_unlink(tooLong.c_str());
    }
    while(errno != ENAMETOOLONG);
    std::cout << tooLong.length() << " " << tooLong << std::endl;    
}

This works and the length is 257 which is right above NAME_MAX.

kuga
  • 1,483
  • 1
  • 17
  • 38
  • "The problem was the missing slash!", no the problem is that you don't look the error value of `mq_unlink()`. You could avoid this question is you have good practice, please read [a book of C++](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list?noredirect=1&lq=1) – Stargateur Jun 13 '17 at 13:34