You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by "hujun260 (via GitHub)" <gi...@apache.org> on 2023/06/08 12:13:32 UTC

[GitHub] [nuttx] hujun260 opened a new pull request, #9497: libc/fdcheck: add fdcheck module

hujun260 opened a new pull request, #9497:
URL: https://github.com/apache/nuttx/pull/9497

   ## Summary
   In embedded development environments, due to the lack of address isolation between processes, fd may be passed between processes and lead to misuse,
   
   We have designed an fd cross-process automatic detection tool, fdcheck_protect returns the fd containing the pid information, indicating that the ownership of the current fd belongs to the pid and is not allowed to be used by other processes. fdcheck_restore will obtain the true fd and check if the ownership of the fd is legal
   
   For ease of understanding, let's give an example where the following information is represented in 32-bit binary format
   
   fd        00000000 00000000 00000000 10001010
   pid       00000000 00000000 00000011 01010101
   ret       00000000 00000011 01010101 10001010
   
   ## Impact
   none
   
   ## Testing
   ostest
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pkarashchenko commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1595849613

   In general if we do a swap and store `pid` value in lower bits and `fd` in upper bits it will give a correct comparison result based on `fd` value.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] fjpanag commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "fjpanag (via GitHub)" <gi...@apache.org>.
fjpanag commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1600654606

   In fact, if I change all uses of `sd` in [this](https://github.com/fjpanag/code_for_nuttx/blob/main/mqtt_broker/mqtt_br_server.c#L150) loop with `fdcheck_protect(sd)` everything works as expected, and all tests pass.
   
   So, as it seems, it is not `select()` that has the problem. Rather it is `FD_ISSET()` which fails in [such](https://github.com/fjpanag/code_for_nuttx/blob/main/mqtt_broker/mqtt_br_server.c#L150) constructs.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] fjpanag commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "fjpanag (via GitHub)" <gi...@apache.org>.
fjpanag commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1600759466

   My current solution to this very specific problem is to add a `min_sd` [here](https://github.com/fjpanag/code_for_nuttx/blob/main/mqtt_broker/mqtt_br_server.c#L121) as it is done with `max_sd`.
   
   Then [this](https://github.com/fjpanag/code_for_nuttx/blob/main/mqtt_broker/mqtt_br_server.c#L150) loop starts of from `min_sd`, instead of 0:
   
   ```
   for (int sd = min_sd; (sd <= max_sd) && (available > 0); sd++)
   ```
   
   This works because `min_sd` now is an actual protected sd. The loop (and thus `FD_ISSET()`) will only check the protected range of descriptors.
   
   I guess this will also slightly increase performance even without `CONFIG_FDCHECK`, as fd 0 to 2 will now not be checked (as they aren't actually needed).
   
   ---
   
   I would insist however that `fdcheck_restore()` should check the validity of its arguments.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] hujun260 commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "hujun260 (via GitHub)" <gi...@apache.org>.
hujun260 commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1596368428

   > It seems that only `select()` is affected by this.
   > The other failures that I see are something else, probably introduced in the late
   
   @fjpanag  I am currently unable to analyze the specific cause of the problem you encountered. Are you using the latest code?
   Can you provide a reproduction method? Or provide some printing information, such as max_sd for better analysis.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pkarashchenko commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1603270796

   @fjpanag I think you need to adopt code from https://github.com/apache/nuttx-apps/blob/3bd106081fa68e58d58a23998553b4e27c372058/canutils/candump/candump.c#L614-L629 and instead if `sd++` save the list of all open descriptors into the array of socket indexes and then iterate over an array instead of incrementing descriptor value directly


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] fjpanag commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "fjpanag (via GitHub)" <gi...@apache.org>.
fjpanag commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1595826148

   @hujun260 @xiaoxiang781216 I think that something is wrong with this.  
   After enabling `CONFIG_FDCHECK` several things break in various ways.
   
   For example, I was using `select()` before on a set of TCP sockets in a server. It was working correctly before.
   
   Now, when I enable this option, `select()` returns an error. Checking `errno`, it contains garbage.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] fjpanag commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "fjpanag (via GitHub)" <gi...@apache.org>.
fjpanag commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1595839154

   > could you share your code around select?
   
   Have a look [here](https://github.com/fjpanag/code_for_nuttx/blob/main/mqtt_broker/mqtt_br_server.c#L133).
   
   It seems that only `select()` is affected by this.  
   The other failures that I see are something else, probably introduced in the latest commits as well.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] fjpanag commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "fjpanag (via GitHub)" <gi...@apache.org>.
fjpanag commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1593706324

   Hello @hujun260,
   
   I just tried this but build fails with:
   
   ```
   arm-none-eabi-ld: nuttx/staging/libc.a(lib_fdcheck.o): in function `fdcheck_restore':
   nuttx/libs/libc/misc/lib_fdcheck.c:88: undefined reference to `getppid'
   ```
   
   It seems that I need to enable `SCHED_HAVE_PARENT`.  
   Do we really need to have the dependency to this config?
   
   If so, maybe `FDCHECK` should also `select SCHED_HAVE_PARENT`?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] fjpanag commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "fjpanag (via GitHub)" <gi...@apache.org>.
fjpanag commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1600630934

   @hujun260 I am sorry but I haven't been able to create a minimum working example yet.
   I working on it, but I have limited availability.
   
   I am currently testing on sim, using [this](https://github.com/fjpanag/code_for_nuttx/tree/main/mqtt_broker) server.
   
   I think I did some more progress though...  
   Check [this](https://github.com/fjpanag/code_for_nuttx/blob/main/mqtt_broker/mqtt_br_server.c#L157) line. This fails before `select()` fails.
   
   The thing is that `socket()` returns a protected file descriptor.  
   However, `FD_ISSET()` is checking on an unprotected descriptor, as seen [here](https://github.com/apache/nuttx/blob/master/include/sys/select.h#L83).
   
   So, although my server descriptor is for example 7172 (0x1C04), `FD_ISSET()` returns true when checking for fd = 4.
   
   I changed the above line to:
   ```
   		if (sd == fdcheck_restore(broker->server.sd))
   ```
   
   and I was able to move forward a bit.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] hujun260 commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "hujun260 (via GitHub)" <gi...@apache.org>.
hujun260 commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1600325164

   > @hujun260 Here is what I think it happens:
   > 
   > 1. `select()` is called, and uses `fdcheck_restore()`.
   > 2. `select()` is calling `poll()` internally.
   > 3. `poll()` calls `poll_setup()`.
   > 4. `poll_setup()` calls `poll_fdsetup()`.
   > 5. `poll_fdsetup()` calls `fs_getfilep()` before `file_poll()`.
   > 6. `fs_getfilep()` calls _again_ `fdcheck_restore()` on an already restored file descriptor.
   > 
   > I added prints, and I verified that the error originates [here](https://github.com/apache/nuttx/blob/master/fs/inode/fs_files.c#L469) (following the above sequence).
   > 
   > The actual error is `EBADF` (no idea why `errno` contained garbage).
   
   I have written some test code and it seems to be fine. 
   Can you provide a complete and runnable test code so that I can reproduce it? for example
   
           int fd = open("/dev/null", O_WRONLY);
           int rc;
           fd_set w_set;
           FD_ZERO(&w_set);
           FD_SET(fd, &w_set);
   
           if ((rc = select(fd + 1, NULL, &w_set, NULL, NULL)) == -1) {
               printf("error  %d\n", rc);
               close(fd);
               goto end;
           }
           if (rc == 0) {  // we had a timeout
               printf("timeout\n");
               close(fd);
               goto end;
           }
   
           if (FD_ISSET(fd, &w_set)) {
               int len = write(fd, "test",4);
               printf("writeok %d\n", len);
               close(fd);
               goto end;
           } else {
               printf("faild %d\n", rc);
               close(fd);
               goto end;
           }


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] hujun260 commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "hujun260 (via GitHub)" <gi...@apache.org>.
hujun260 commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1600660579

   Based on your description, I have a idea of the problem, fdcheck does have compatibility issues and some user codes need to be changed. But I haven't found a good solution yet. If fdcheck affects your work, don't enable it.
   
   > if (sd == fdcheck_restore(broker->server.sd))
   after this change, is everything normal now? 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1595930837

   > > But select already contain the code to restore:
   > > https://github.com/apache/nuttx/blob/master/fs/vfs/fs_select.c#L105
   > 
   > @xiaoxiang781216 The `fdcheck_protect()` is implemented as
   > 
   > ```
   > /****************************************************************************
   >  * Name: fdcheck_protect
   >  *
   >  * Description: Obtain the combined value of fd and pid
   >  *
   >  * the return value carries the pid and fd information.
   >  * The original fd information is stored in low bit of val.
   >  * The pid information is stored in high bit of val.
   >  * For ease of understanding, let's give an example where
   >  * the following information is represented in 32-bit binary format
   >  *
   >  *  fd        00000000 00000000 00000000 10001010
   >  *  pid       00000000 00000000 00000000 01010101
   >  *  val       00000000 00000000 01010101 10001010
   > ```
   > 
   > So for example we have 2 pthreads spawned from application `main` task. `main` has pid 50 and open fd 4 while pthread1/2 have pid 40/30 and open fd 5/6. Finally main wants to select based on 3 fds and in normal case it will get max of 4, 5, 6 and finally will pass max_fd + 1 that is 7 to select. While with `fdcheck_protect()` modified fds it will select `main`'s fd 4 as max because of `main` has higher pid value and will pass it +1 to select. Does this makes sense?
   
   Yes, you are right.
   
   > In general if we do a swap and store `pid` value in lower bits and `fd` in upper bits it will give a correct comparison result based on `fd` value.
   
   @hujun260 please take a look.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] fjpanag commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "fjpanag (via GitHub)" <gi...@apache.org>.
fjpanag commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1593709192

   Oh, also I think that the relevant `#include` is missing for `getppid()`?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] fjpanag commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "fjpanag (via GitHub)" <gi...@apache.org>.
fjpanag commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1599562628

   @hujun260 Here is what I think it happens:
   
   1. `select()` is called, and uses `fdcheck_restore()`.
   2. `select()` is calling `poll()` internally.
   3. `poll()` calls `poll_setup()`.
   4. `poll_setup()` calls `poll_fdsetup()`.
   5. `poll_fdsetup()` calls `fs_getfilep()` before `file_poll()`.
   6. `fs_getfilep()` calls *again* `fdcheck_restore()` on an already restored file descriptor.
   
   I added prints, and I verified that the error originates [here](https://github.com/apache/nuttx/blob/master/fs/inode/fs_files.c#L469) (following the above sequence).
   
   The actual error is `EBADF` (no idea why `errno` contained garbage).


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1594019876

   > Oh, also I think that the relevant `#include` is missing for `getppid()`?
   
   @fjpanag please try this patch: https://github.com/apache/nuttx/pull/9497


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] hujun260 commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "hujun260 (via GitHub)" <gi...@apache.org>.
hujun260 commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1600675756

   > In fact, if I change all uses of `sd` in [this](https://github.com/fjpanag/code_for_nuttx/blob/main/mqtt_broker/mqtt_br_server.c#L150) loop with `fdcheck_protect(sd)` everything works as expected, and all tests pass.
   > 
   > So, as it seems, it is not `select()` that has the problem. Rather it is `FD_ISSET()` which fails in [such](https://github.com/fjpanag/code_for_nuttx/blob/main/mqtt_broker/mqtt_br_server.c#L150) constructs.
   
   You are right, you can also refer to the modifications made to fs_select.c when I submitted this module, 
   mainly in CONFIG_FDCHECK 
   https://github.com/apache/nuttx/blob/master/fs/vfs/fs_select.c#LL105C20-L105C20


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] fjpanag commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "fjpanag (via GitHub)" <gi...@apache.org>.
fjpanag commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1600700892

   I get it now.
   
   The thing is that `fdcheck_restore()` does not know if it operates on a raw or a protected descriptor.
   
   I believe that the first thing to do is fix this.  
   For example, when `fdcheck_protect()` protects an fd, it can set a flag (let's say set bit 31).  
   Then `fdcheck_restore()` can check if this flag is set (and thus if the fd is actually protected), and act accordingly.
   
   As a first defense, `fdcheck_restore()` should raise an error when it is passed an unprotected fd.  
   This would have saved a lot of time when troubleshooting this specific issue.  
   Users will immediately be informed about the wrong use (or at least the limitations) of `fdcheck_restore()`.
   
   As a second step, in these cases `fdcheck_restore()` can return an invalid fd so, code like the one mentioned above may work correctly? I am still experimenting with this.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] hujun260 commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "hujun260 (via GitHub)" <gi...@apache.org>.
hujun260 commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1596350439

   > > But select already contain the code to restore:
   > > https://github.com/apache/nuttx/blob/master/fs/vfs/fs_select.c#L105
   > 
   > @xiaoxiang781216 The `fdcheck_protect()` is implemented as
   > 
   > ```
   > /****************************************************************************
   >  * Name: fdcheck_protect
   >  *
   >  * Description: Obtain the combined value of fd and pid
   >  *
   >  * the return value carries the pid and fd information.
   >  * The original fd information is stored in low bit of val.
   >  * The pid information is stored in high bit of val.
   >  * For ease of understanding, let's give an example where
   >  * the following information is represented in 32-bit binary format
   >  *
   >  *  fd        00000000 00000000 00000000 10001010
   >  *  pid       00000000 00000000 00000000 01010101
   >  *  val       00000000 00000000 01010101 10001010
   > ```
   > 
   > So for example we have 2 pthreads spawned from application `main` task. `main` has pid 50 and open fd 4 while pthread1/2 have pid 40/30 and open fd 5/6. Finally main wants to select based on 3 fds and in normal case it will get max of 4, 5, 6 and finally will pass max_fd + 1 that is 7 to select. While with `fdcheck_protect()` modified fds it will select `main`'s fd 4 as max because of `main` has higher pid value and will pass it +1 to select. Does this makes sense?
   
   hi @pkarashchenko   In fact, I am using pid instead of tid, and the pid used by the thread is the same as application `main` task.
   The situation you mentioned will not happen。


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pkarashchenko commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1595829511

   @xiaoxiang781216 I think there may be a case in `max_fd + 1` that is passed to select. I think it should be computed based on `fdcheck_restore()`.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1595831570

   But select already contain the code to restore:
   https://github.com/apache/nuttx/blob/master/fs/vfs/fs_select.c#L105


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1595826910

   could you share your code around select?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] pkarashchenko commented on pull request #9497: libc/fdcheck: add fdcheck module

Posted by "pkarashchenko (via GitHub)" <gi...@apache.org>.
pkarashchenko commented on PR #9497:
URL: https://github.com/apache/nuttx/pull/9497#issuecomment-1595845971

   The `fdcheck_protect()` is implemented as
   ```
   /****************************************************************************
    * Name: fdcheck_protect
    *
    * Description: Obtain the combined value of fd and pid
    *
    * the return value carries the pid and fd information.
    * The original fd information is stored in low bit of val.
    * The pid information is stored in high bit of val.
    * For ease of understanding, let's give an example where
    * the following information is represented in 32-bit binary format
    *
    *  fd        00000000 00000000 00000000 10001010
    *  pid       00000000 00000000 00000000 01010101
    *  val       00000000 00000000 01010101 10001010
   ```
   So for example we have 2 pthreads spawned from application `main` task. `main` has pid 50 and open fd 4 while pthread1/2 has pid 40/30 and opens fd 5/6. Finally main wants to select based on 3 fds and in normal case it will get max of 4, 5, 6 and finally will pass max_fd + 1 that is 7 to select. While with `fdcheck_protect()` modified fds it will select `main`'s fd 4 as max because of `main` has higher pid value and will pass it +1 to select.
   Does this makes sense?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [nuttx] xiaoxiang781216 merged pull request #9497: libc/fdcheck: add fdcheck module

Posted by "xiaoxiang781216 (via GitHub)" <gi...@apache.org>.
xiaoxiang781216 merged PR #9497:
URL: https://github.com/apache/nuttx/pull/9497


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org