libc/ipc: add ftok(3) support

https://man.openbsd.org/ftok.3

Signed-off-by: chao.an <anchao@xiaomi.com>
This commit is contained in:
chao.an 2022-05-16 11:25:37 +08:00 committed by Petro Karashchenko
parent 795884dbf1
commit 644283c8ff
4 changed files with 94 additions and 3 deletions

View File

@ -88,7 +88,29 @@ struct ipc_perm
* Public Function Prototypes
****************************************************************************/
key_t ftok(FAR const char *path, int id);
/****************************************************************************
* Name: ftok
*
* Description:
* Convert a pathname and a project identifier to a System V IPC key.
* The ftok() function uses the identity of the file named by the given
* pathname (which must refer to an existing, accessible file) and the
* least significant 8 bits of proj_id (which must be nonzero) to
* generate a key_t type System V IPC key, suitable for use with
* msgget(2), semget(2), or shmget(2).
*
* Input Parameters:
* pathname - identity of the file name
* proj_id - The value that uniquely project identifies.
*
* Returned Value:
* On success, the generated key_t value is returned.
* On failure -1 is returned, with errno indicating the error as for the
* stat(2) system call.
*
****************************************************************************/
key_t ftok(FAR const char *pathname, int proj_id);
#undef EXTERN
#if defined(__cplusplus)

View File

@ -158,7 +158,7 @@ typedef int id_t;
* semaphores. A key is simply an integer of type key_t
*/
typedef int16_t key_t;
typedef int32_t key_t;
/* Signed integral type of the result of subtracting two pointers */

View File

@ -37,7 +37,7 @@ endif
CSRCS += lib_dumpbuffer.c lib_dumpvbuffer.c lib_fnmatch.c lib_debug.c
CSRCS += lib_crc64.c lib_crc32.c lib_crc16.c lib_crc8.c lib_crc8ccitt.c
CSRCS += lib_crc8table.c lib_glob.c lib_execinfo.c
CSRCS += lib_crc8table.c lib_glob.c lib_execinfo.c lib_ftok.c
# Keyboard driver encoder/decoder

69
libs/libc/misc/lib_ftok.c Normal file
View File

@ -0,0 +1,69 @@
/****************************************************************************
* libs/libc/misc/lib_ftok.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ipc.h>
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: ftok
*
* Description:
* Convert a pathname and a project identifier to a System V IPC key.
* The ftok() function uses the identity of the file named by the given
* pathname (which must refer to an existing, accessible file) and the
* least significant 8 bits of proj_id (which must be nonzero) to
* generate a key_t type System V IPC key, suitable for use with
* msgget(2), semget(2), or shmget(2).
*
* Input Parameters:
* pathname - identity of the file name
* proj_id - The value that uniquely project identifies.
*
* Returned Value:
* On success, the generated key_t value is returned.
* On failure -1 is returned, with errno indicating the error as for the
* stat(2) system call.
*
****************************************************************************/
key_t ftok(FAR const char *pathname, int proj_id)
{
struct stat st;
if (stat(pathname, &st) < 0)
{
return (key_t)-1;
}
return ((key_t)proj_id << 24 |
(key_t)(st.st_dev & 0xff) << 16 |
(key_t)(st.st_ino & 0xffff));
}