open_by_handle_at(2) - wariua/manpages-ko GitHub Wiki

NAME

name_to_handle_at, open_by_handle_at - ๊ฒฝ๋กœ๋ช…์— ๋Œ€ํ•œ ํ•ธ๋“ค์„ ์–ป๊ณ  ํ•ธ๋“ค์„ ํ†ตํ•ด ํŒŒ์ผ ์—ด๊ธฐ

SYNOPSIS

#define _GNU_SOURCE         /* feature_test_macros(7) ์ฐธ๊ณ  */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int name_to_handle_at(int dirfd, const char *pathname,
                      struct file_handle *handle,
                      int *mount_id, int flags);

int open_by_handle_at(int mount_fd, struct file_handle *handle,
                      int flags);

DESCRIPTION

name_to_handle_at() ๋ฐ open_by_handle_at() ์‹œ์Šคํ…œ ํ˜ธ์ถœ์€ openat(2)์˜ ๊ธฐ๋Šฅ์„ ๋‘ ๋ถ€๋ถ„์œผ๋กœ ์ชผ๊ฐ  ๊ฒƒ์ด๋‹ค. name_to_handle_at()์€ ์ง€์ •ํ•œ ํŒŒ์ผ์— ๋Œ€์‘ํ•˜๋Š” ๋ถˆํˆฌ๋ช… ํ•ธ๋“ค์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ open_by_handle_at()์€ ์•ž์„  name_to_handle_at() ํ˜ธ์ถœ์ด ๋ฐ˜ํ™˜ํ•œ ํ•ธ๋“ค์— ๋Œ€์‘ํ•˜๋Š” ํŒŒ์ผ์„ ์—ด์–ด์„œ ์—ด๋ฆฐ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

name_to_handle_at()

name_to_handle_at() ์‹œ์Šคํ…œ ํ˜ธ์ถœ์€ dirfd ๋ฐ pathname ์ธ์ž๋กœ ์ง€์ •ํ•œ ํŒŒ์ผ์— ๋Œ€์‘ํ•˜๋Š” ํŒŒ์ผ ํ•ธ๋“ค๊ณผ ๋งˆ์šดํŠธ ID๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ธ์ž handle์„ ํ†ตํ•ด ํŒŒ์ผ ํ•ธ๋“ค์„ ๋ฐ˜ํ™˜ํ•˜๋Š”๋ฐ, ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ตฌ์กฐ์ฒด์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ์ด๋‹ค.

struct file_handle {
    unsigned int  handle_bytes;   /* f_handle์˜ ํฌ๊ธฐ [in, out] */
    int           handle_type;    /* ํ•ธ๋“ค ํƒ€์ž… [out] */
    unsigned char f_handle[0];    /* ํŒŒ์ผ ์‹๋ณ„์ž (ํ˜ธ์ถœ์ž๊ฐ€ ํฌ๊ธฐ ์ง€์ •)
                                     [out] */
};

f_handle๋กœ ๋ฐ˜ํ™˜๋˜๋Š” ํ•ธ๋“ค์„ ๋‹ด๊ธฐ์— ์ถฉ๋ถ„ํžˆ ํฐ ๊ตฌ์กฐ์ฒด๋ฅผ ํ• ๋‹นํ•˜๋Š” ๊ฑด ํ˜ธ์ถœ์ž์˜ ์ฑ…์ž„์ด๋‹ค. ํ˜ธ์ถœ ์ „์— f_handle์— ํ• ๋‹น๋œ ํฌ๊ธฐ๋ฅผ ๋‹ด๋„๋ก handle_bytes ํ•„๋“œ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผ ํ•œ๋‹ค. (<fcntl.h>์— ์ •์˜๋œ ์ƒ์ˆ˜ MAX_HANDLE_SZ๊ฐ€ ํŒŒ์ผ ํ•ธ๋“ค ํฌ๊ธฐ์˜ ์˜ˆ์ƒ ์ตœ๋Œ“๊ฐ’์„ ๋‚˜ํƒ€๋‚ธ๋‹ค. ํ–ฅํ›„์˜ ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ๋” ํฐ ๊ณต๊ฐ„์„ ํ•„์š”๋กœ ํ•  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ๋ณด์žฅ๋œ ์ƒํ•œ์€ ์•„๋‹ˆ๋‹ค.) ์„ฑ๊ณต ๋ฐ˜ํ™˜ ์‹œ ์‹ค์ œ f_handle์— ๊ธฐ๋ก๋œ ๋ฐ”์ดํŠธ ์ˆ˜๋ฅผ ๋‹ด๋„๋ก handle_bytes ํ•„๋“œ๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค.

ํ˜ธ์ถœ์ž๊ฐ€ handle->handle_bytes๋ฅผ 0์œผ๋กœ ํ˜ธ์ถœ์„ ํ•ด์„œ file_handle ๊ตฌ์กฐ์ฒด์— ํ•„์š”ํ•œ ํฌ๊ธฐ๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ ํ˜ธ์ถœ์ด EOVERFLOW ์˜ค๋ฅ˜๋กœ ์‹คํŒจํ•˜๋ฉฐ handle->handle_bytes๊ฐ€ ํ•„์š” ํฌ๊ธฐ๋กœ ์„ค์ •๋œ๋‹ค. ๊ทธ๋Ÿฌ๋ฉด ํ˜ธ์ถœ์ž๊ฐ€ ๊ทธ ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ์˜ฌ๋ฐ”๋ฅธ ํฌ๊ธฐ์˜ ๊ตฌ์กฐ์ฒด๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ๋‹ค. (์•„๋ž˜ EXAMPLE ์ฐธ๊ณ .) EOVERFLOW ์˜ค๋ฅ˜๊ฐ€ ํŒŒ์ผ ํ•ธ๋“ค ๊ฒ€์ƒ‰์„ ์ •์ƒ์ ์œผ๋กœ ์ง€์›ํ•˜๋Š” ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ์ด ํŠน์ • ์ด๋ฆ„์— ๋Œ€ํ•ด ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•œ ํŒŒ์ผ ํ•ธ๋“ค์ด ์—†๋‹ค๋Š” ๋œป์ผ ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์•ฝ๊ฐ„์˜ ์ฃผ์˜๊ฐ€ ํ•„์š”ํ•˜๋‹ค. handle_bytes๊ฐ€ ์ฆ๊ฐ€ํ•˜์ง€ ์•Š๊ณ  EOVERFLOW ์˜ค๋ฅ˜๊ฐ€ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฒƒ์œผ๋กœ ์ด ๊ฒฝ์šฐ๋ฅผ ์•Œ์•„๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

handle_bytes ํ•„๋“œ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ ์™ธ์—๋Š” ํ˜ธ์ถœ์ž๊ฐ€ file_handle ๊ตฌ์กฐ์ฒด๋ฅผ ๋ถˆํˆฌ๋ช…ํ•œ ๋ฐ์ดํ„ฐ ํƒ€์ž…์œผ๋กœ ๋‹ค๋ค„์•ผ ํ•œ๋‹ค. handle_type ๋ฐ f_handle ํ•„๋“œ๊ฐ€ ํ•„์š”ํ•œ ๊ณณ์€ ์ด์–ด์ง€๋Š” open_by_handle_at() ํ˜ธ์ถœ๋ฟ์ด๋‹ค.

flags ์ธ์ž๋Š” AT_EMPTY_PATH์™€ AT_SYMLINK_FOLLOW๋ฅผ 0๊ฐœ ์ด์ƒ OR ํ•ด์„œ ๊ตฌ์„ฑํ•œ ๋น„ํŠธ ๋งˆ์Šคํฌ์ด๋‹ค. ๋‘˜์— ๋Œ€ํ•ด์„  ์•„๋ž˜์—์„œ ์„ค๋ช…ํ•œ๋‹ค.

pathname ๋ฐ dirfd ์ธ์ž๋Š” ๋‘˜์ด ํ•จ๊ป˜ ํ•ธ๋“ค์„ ์–ป์„ ํŒŒ์ผ์„ ์‹๋ณ„ํ•œ๋‹ค. ๋„ค ๊ฐ€์ง€ ๊ฒฝ์šฐ๊ฐ€ ์žˆ๋‹ค.

  • pathname์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๋ฌธ์ž์—ด์ด๊ณ  ์ ˆ๋Œ€ ๊ฒฝ๋กœ๋ช…์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉด ๊ทธ ๊ฒฝ๋กœ๋ช…์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ์— ๋Œ€ํ•œ ํ•ธ๋“ค์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด ๊ฒฝ์šฐ dirfd๋Š” ๋ฌด์‹œํ•œ๋‹ค.

  • pathname์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๋ฌธ์ž์—ด์ด๊ณ  ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ช…์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ dirfd๊ฐ€ ํŠน์ˆ˜ ๊ฐ’ AT_FDCWD์ด๋ฉด ํ˜ธ์ถœ์ž์˜ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ pathname์„ ํ•ด์„ํ•ด์„œ ๊ทธ ๊ฒฝ๋กœ๋ช…์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ์— ๋Œ€ํ•œ ํ•ธ๋“ค์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

  • pathname์ด ๋น„์–ด ์žˆ์ง€ ์•Š์€ ๋ฌธ์ž์—ด์ด๊ณ  ์ƒ๋Œ€ ๊ฒฝ๋กœ๋ช…์„ ๋‹ด๊ณ  ์žˆ์œผ๋ฉฐ dirfd๊ฐ€ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ์ด๋ฉด dirfd๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ธฐ์ค€์œผ๋กœ pathname์„ ํ•ด์„ํ•ด์„œ ๊ทธ ๊ฒฝ๋กœ๋ช…์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ์— ๋Œ€ํ•œ ํ•ธ๋“ค์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ("๋””๋ ‰ํ„ฐ๋ฆฌ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ"๊ฐ€ ์œ ์šฉํ•œ ์ด์œ ์— ๋Œ€ํ•ด์„  openat(2) ์ฐธ๊ณ .)

  • pathname์ด ๋นˆ ๋ฌธ์ž์—ด์ด๊ณ  flags์— AT_EMPTY_PATH ๊ฐ’์ด ์ง€์ •๋ผ ์žˆ์œผ๋ฉด dirfd๊ฐ€ ์ž„์˜ ์ข…๋ฅ˜์˜ ํŒŒ์ผ์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์—ด๋ฆฐ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ์ด๊ฑฐ๋‚˜ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋œปํ•˜๋Š” AT_FDCWD์ผ ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ทธ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ์— ๋Œ€ํ•œ ํ•ธ๋“ค์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

mount_id ์ธ์ž๋Š” pathname์— ๋ถ€ํ•ฉํ•˜๋Š” ํŒŒ์ผ ์‹œ์Šคํ…œ ๋งˆ์šดํŠธ์— ๋Œ€ํ•œ ์‹๋ณ„์ž๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ด๋Š” /proc/self/mountinfo์— ์žˆ๋Š” ํ•œ ๋ ˆ์ฝ”๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ํ•„๋“œ์— ๋Œ€์‘ํ•œ๋‹ค. ๊ทธ ๋ ˆ์ฝ”๋“œ์˜ ๋‹ค์„ฏ ๋ฒˆ์งธ ํ•„๋“œ์— ์žˆ๋Š” ๊ฒฝ๋กœ๋ช…์„ ์—ด๋ฉด ๊ทธ ๋งˆ์šดํŠธ ์ง€์ ์— ๋Œ€ํ•œ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๊ฐ€ ๋‚˜์˜ค๊ณ , ๊ทธ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ์ด์–ด์ง€๋Š” open_by_handle_at() ํ˜ธ์ถœ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์„ฑ๊ณต ํ˜ธ์ถœ๊ณผ EOVERFLOW ์˜ค๋ฅ˜๊ฐ€ ๋‚œ ํ˜ธ์ถœ ๋ชจ๋‘์—์„œ mount_id๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ name_to_handle_at()์—์„œ๋Š” pathname์ด ์‹ฌ๋ณผ๋ฆญ ๋งํฌ์ธ ๊ฒฝ์šฐ ์—ญ์ฐธ์กฐ๋ฅผ ํ•˜์ง€ ์•Š๊ณ  ๊ทธ ๋งํฌ ์ž์ฒด์— ๋Œ€ํ•œ ํ•ธ๋“ค์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. flags์— AT_SYMLINK_FOLLOW๋ฅผ ์ง€์ •ํ•˜๋ฉด pathname์ด ์‹ฌ๋ณผ๋ฆญ ๋งํฌ์ธ ๊ฒฝ์šฐ ์—ญ์ฐธ์กฐ๋ฅผ ํ•œ๋‹ค. (๊ทธ๋ž˜์„œ ๊ทธ ๋งํฌ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ์— ๋Œ€ํ•œ ํ•ธ๋“ค์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.)

๊ฒฝ๋กœ๋ช…์˜ ๋งˆ์ง€๋ง‰ ๊ตฌ์„ฑ ์š”์†Œ๊ฐ€ automount ์ง€์ ์ผ ๋•Œ๋Š” name_to_handle_at()์ด ๋งˆ์šดํŠธ๋ฅผ ์œ ๋ฐœํ•˜์ง€ ์•Š๋Š”๋‹ค. ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ํŒŒ์ผ ํ•ธ๋“ค๊ณผ automount ์ง€์  ๋ชจ๋‘๋ฅผ ์ง€์›ํ•  ๋•Œ automount ์ง€์ ์— ๋Œ€ํ•œ name_to_handle_at() ํ˜ธ์ถœ์€ handle_bytes๋ฅผ ์˜ฌ๋ฆฌ์ง€ ์•Š์€ ์ฑ„ EOVERFLOW ์˜ค๋ฅ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค. ๋ฆฌ๋ˆ…์Šค 4.13 ์ด์ƒ์—์„œ NFS๋ฅผ ์“ธ ๋•Œ ์„œ๋ฒ„ ์ƒ์˜ ๋ณ„๋„ ํŒŒ์ผ ์‹œ์Šคํ…œ์— ์žˆ๋Š” ๋””๋ ‰ํ„ฐ๋ฆฌ์— ์ ‘๊ทผ ์‹œ ์ด๋Ÿฐ ์ƒํ™ฉ์ด ์ƒ๊ธธ ์ˆ˜ ์žˆ๋‹ค. ์ด ๊ฒฝ์šฐ์— ๊ฒฝ๋กœ๋ช… ๋์— "/"๋ฅผ ๋ง๋ถ™์ด๋ฉด automount๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

open_by_handle_at()

open_by_handle_at() ์‹œ์Šคํ…œ ํ˜ธ์ถœ์€ ์•ž์„  name_to_handle_at() ํ˜ธ์ถœ์ด ๋ฐ˜ํ™˜ํ•œ ํŒŒ์ผ ํ•ธ๋“ค์ธ handle์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ์„ ์—ฐ๋‹ค.

mount_fd ์ธ์ž๋Š” handle์„ ํ•ด์„ํ•  ๊ธฐ์ค€์ด ๋ผ์•ผ ํ•˜๋Š” ๋งˆ์šดํŠธ ๋œ ํŒŒ์ผ ์‹œ์Šคํ…œ ๋‚ด์˜ ์ž„์˜ ๊ฐ์ฒด(ํŒŒ์ผ, ๋””๋ ‰ํ„ฐ๋ฆฌ ๋“ฑ)์— ๋Œ€ํ•œ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ์ด๋‹ค. ํ˜ธ์ถœ์ž์˜ ํ˜„์žฌ ์ž‘์—… ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋œปํ•˜๋Š” ํŠน์ˆ˜ ๊ฐ’ AT_FDCWD๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

flags ์ธ์ž๋Š” open(2)์—์„œ์™€ ๊ฐ™๋‹ค. handle์ด ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฒฝ์šฐ ํ˜ธ์ถœ์ž๊ฐ€ O_PATH ํ”Œ๋ž˜๊ทธ๋ฅผ ์ง€์ •ํ•ด์•ผ ํ•˜๋ฉฐ, ๊ทธ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ์—ญ์ฐธ์กฐํ•˜์ง€ ์•Š๋Š”๋‹ค. O_NOFOLLOW ํ”Œ๋ž˜๊ทธ๋Š” ์ง€์ • ์‹œ ๋ฌด์‹œํ•œ๋‹ค.

open_by_handle_at()์„ ์“ฐ๋ ค๋ฉด ํ˜ธ์ถœ์ž์—๊ฒŒ CAP_DAC_READ_SEARCH ์—ญ๋Šฅ์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค.

RETURN VALUE

์„ฑ๊ณต ์‹œ name_to_handle_at()์€ 0์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ open_by_handle_at()์€ ์Œ์ˆ˜ ์•„๋‹Œ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ๋‘ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์€ -1์„ ๋ฐ˜ํ™˜ํ•˜๋ฉฐ ์˜ค๋ฅ˜ ์›์ธ์„ ๋‚˜ํƒ€๋‚ด๋„๋ก errno๋ฅผ ์„ค์ •ํ•œ๋‹ค.

ERRORS

name_to_handle_at()๊ณผ open_by_handle_at()์ด openat(2)๊ณผ ๊ฐ™์€ ์˜ค๋ฅ˜๋กœ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”๋ถˆ์–ด ์•„๋ž˜์— ์ ์€ ์˜ค๋ฅ˜๋“ค๋กœ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค.

name_to_handle_at()์ด ๋‹ค์Œ ์˜ค๋ฅ˜๋กœ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค.

EFAULT
pathname์ด๋‚˜ mount_id, handle์ด ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ์ฃผ์†Œ ๊ณต๊ฐ„ ๋ฐ–์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
EINVAL
flags์— ์œ ํšจํ•˜์ง€ ์•Š์€ ๋น„ํŠธ ๊ฐ’์ด ํฌํ•จ๋ผ ์žˆ๋‹ค.
EINVAL
handle->handle_bytes๊ฐ€ MAX_HANDLE_SZ๋ณด๋‹ค ํฌ๋‹ค.
ENOENT
pathname์ด ๋นˆ ๋ฌธ์ž์—ด์ธ๋ฐ flags์— AT_EMPTY_PATH๋ฅผ ์ง€์ •ํ•˜์ง€ ์•Š์•˜๋‹ค.
ENOTDIR
dirfd๋กœ ์ค€ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๊ฐ€ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์ง€ ์•Š์œผ๋ฉฐ, flags์— AT_EMPTY_PATH๊ฐ€ ์žˆ์œผ๋ฉด์„œ pathname์ด ๋นˆ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋‹ค.
EOPNOTSUPP
ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ๊ฒฝ๋กœ๋ช…์„ ํŒŒ์ผ ํ•ธ๋“ค๋กœ ๋””์ฝ”๋”ฉ ํ•˜๋Š” ๊ฑธ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค.
EOVERFLOW
ํ˜ธ์ถœ๋กœ ์ „๋‹ฌํ•œ handle->handle_bytes ๊ฐ’์ด ๋„ˆ๋ฌด ์ž‘๋‹ค. ์ด ์˜ค๋ฅ˜ ๋ฐœ์ƒ ์‹œ ํ•ธ๋“ค์— ํ•„์š”ํ•œ ํฌ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋„๋ก handle->handle_bytes๊ฐ€ ๊ฐฑ์‹ ๋œ๋‹ค.

open_by_handle_at()์ด ๋‹ค์Œ ์˜ค๋ฅ˜๋กœ ์‹คํŒจํ•  ์ˆ˜ ์žˆ๋‹ค.

EBADF
mount_fd๊ฐ€ ์—ด๋ฆฐ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๊ฐ€ ์•„๋‹ˆ๋‹ค.
EFAULT
handle์ด ์ ‘๊ทผ ๊ฐ€๋Šฅํ•œ ์ฃผ์†Œ ๊ณต๊ฐ„ ๋ฐ–์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
EINVAL
handle->handle_bytes๊ฐ€ MAX_HANDLE_SZ๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ 0๊ณผ ๊ฐ™๋‹ค.
ELOOP
handle์ด ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š”๋ฐ flags์— O_PATH๊ฐ€ ์ง€์ •๋ผ ์žˆ์ง€ ์•Š๋‹ค.
EPERM
ํ˜ธ์ถœ์ž์—๊ฒŒ CAP_DAC_READ_SEARCH ์—ญ๋Šฅ์ด ์—†๋‹ค.
ESTALE
์ง€์ •ํ•œ handle์ด ์œ ํšจํ•˜์ง€ ์•Š๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ํŒŒ์ผ์ด ์‚ญ์ œ๋์„ ๋•Œ ์ด ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

VERSIONS

๋ฆฌ๋ˆ…์Šค 2.6.39์—์„œ ์ด ์‹œ์Šคํ…œ ํ˜ธ์ถœ๋“ค์ด ์ฒ˜์Œ ๋“ฑ์žฅํ–ˆ๋‹ค. glibc ๋ฒ„์ „ 2.14๋ถ€ํ„ฐ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ง€์›์„ ์ œ๊ณตํ•œ๋‹ค.

CONFORMING TO

์ด ์‹œ์Šคํ…œ ํ˜ธ์ถœ๋“ค์€ ๋น„ํ‘œ์ค€ ๋ฆฌ๋ˆ…์Šค ํ™•์žฅ์ด๋‹ค.

FreeBSD์— ๋Œ€๋žต ๋น„์Šทํ•œ ํ˜•ํƒœ์˜ ์‹œ์Šคํ…œ ํ˜ธ์ถœ ์Œ getfh()์™€ openfh()๊ฐ€ ์žˆ๋‹ค.

NOTES

ํ•œ ํ”„๋กœ์„ธ์Šค์—์„œ name_to_handle_at()์œผ๋กœ ํŒŒ์ผ ํ•ธ๋“ค์„ ๋งŒ๋“ค๊ณ  ๋‚˜์ค‘์— ๋‹ค๋ฅธ ํ”„๋กœ์„ธ์Šค์—์„œ open_by_handle_at() ํ˜ธ์ถœ์— ์“ธ ์ˆ˜ ์žˆ๋‹ค.

์ผ๋ถ€ ํŒŒ์ผ ์‹œ์Šคํ…œ์€ ๊ฒฝ๋กœ๋ช…์—์„œ ํŒŒ์ผ ํ•ธ๋“ค๋กœ์˜ ๋ณ€ํ™˜์„ ์ง€์›ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์˜ˆ๋กœ /proc, /sys, ์—ฌ๋Ÿฌ ๋„คํŠธ์›Œํฌ ํŒŒ์ผ ์‹œ์Šคํ…œ๋“ค์ด ์žˆ๋‹ค.

ํŒŒ์ผ์ด ์‚ญ์ œ๋˜๊ฑฐ๋‚˜ ๊ธฐํƒ€ ํŒŒ์ผ ์‹œ์Šคํ…œ ์ž์ฒด์ ์ธ ์–ด๋–ค ์ด์œ ๋กœ ํŒŒ์ผ ํ•ธ๋“ค์ด ์œ ํšจํ•˜์ง€ ์•Š๊ฒŒ ๋  ("์ƒํ• ") ์ˆ˜ ์žˆ๋‹ค. ์œ ํšจํ•˜์ง€ ์•Š์€ ํ•ธ๋“ค์€ open_by_handle_at()์—์„œ ESTALE ์˜ค๋ฅ˜๋กœ ์•Œ๋ ค ์ค€๋‹ค.

์ด ์‹œ์Šคํ…œ ํ˜ธ์ถœ๋“ค์€ ์‚ฌ์šฉ์ž ๊ณต๊ฐ„ ํŒŒ์ผ ์„œ๋ฒ„์—์„œ ์“ฐ๋„๋ก ๋งŒ๋“ค์–ด์ง„ ๊ฒƒ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด ์‚ฌ์šฉ์ž ๊ณต๊ฐ„ NFS ์„œ๋ฒ„์—์„œ ํŒŒ์ผ ํ•ธ๋“ค์„ ์ƒ์„ฑํ•ด์„œ NFS ํด๋ผ์ด์–ธํŠธ๋กœ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ์„ ๊ฒƒ์ด๋‹ค. ์ดํ›„ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๊ทธ ํŒŒ์ผ์„ ์—ด๊ณ  ์‹ถ์œผ๋ฉด ๊ทธ ํ•ธ๋“ค์„ ์„œ๋ฒ„์—๊ฒŒ ๋‹ค์‹œ ์ „๋‹ฌํ•  ์ˆ˜ ์žˆ๋‹ค. ์ด๋Ÿฐ ๋ฅ˜์˜ ๊ธฐ๋Šฅ์„ ์ด์šฉํ•˜๋ฉด ์‚ฌ์šฉ์ž ๊ณต๊ฐ„ ํŒŒ์ผ ์„œ๋ฒ„๊ฐ€ ์šด์šฉ ํŒŒ์ผ๋“ค์— ๋Œ€ํ•ด ๋ฌด์ƒํƒœ ๋ฐฉ์‹์œผ๋กœ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ๋‹ค.

pathname์ด ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  flags์— AT_SYMLINK_FOLLOW๊ฐ€ ์ง€์ •๋ผ ์žˆ์ง€ ์•Š์€ ๊ฒฝ์šฐ์— name_to_handle_at()์€ (๋งํฌ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํŒŒ์ผ์ด ์•„๋‹ˆ๋ผ) ๋งํฌ์— ๋Œ€ํ•œ ํ•ธ๋“ค์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ดํ›„ ๊ทธ ํ•ธ๋“ค์„ ๋ฐ›์€ ํ”„๋กœ์„ธ์Šค๊ฐ€ O_PATH ํ”Œ๋ž˜๊ทธ๋ฅผ ์“ด open_by_handle_at()์œผ๋กœ ํ•ธ๋“ค์—์„œ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋กœ์˜ ๋ณ€ํ™˜์„ ํ•˜๊ณ ์„œ ๊ทธ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ readlinkat(2)๊ณผ fchownat(2) ๊ฐ™์€ ์‹œ์Šคํ…œ ํ˜ธ์ถœ์— dirfd ์ธ์ž๋กœ ์ „๋‹ฌํ•ด์„œ ๊ทธ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ์— ๋Œ€ํ•œ ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜์†์ ์ธ ํŒŒ์ผ ์‹œ์Šคํ…œ ID ์–ป๊ธฐ

ํŒŒ์ผ ์‹œ์Šคํ…œ์ด ๋งˆ์šดํŠธ ๋˜๊ณ  ํ•ด์ œ๋˜๋ฉด์„œ /proc/self/mountinfo์˜ ๋งˆ์šดํŠธ ID๊ฐ€ ์žฌ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ name_to_handle_at()์ด (*mount_id์—) ๋ฐ˜ํ™˜ํ•œ ๋งˆ์šดํŠธ ID๋ฅผ ํ•ด๋‹นํ•˜๋Š” ๋งˆ์šดํŠธ ๋œ ํŒŒ์ผ ์‹œ์Šคํ…œ์— ๋Œ€ํ•œ ์˜์†์  ์‹๋ณ„์ž๋กœ ๋‹ค๋ฃจ์ง€ ๋ง์•„์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์‘์šฉ์—์„œ ๊ทธ ๋งˆ์šดํŠธ ID์— ๋Œ€์‘ํ•˜๋Š” mountinfo ๋ ˆ์ฝ”๋“œ์˜ ์ •๋ณด๋ฅผ ์ด์šฉํ•ด ์˜์†์ ์ธ ์‹๋ณ„์ž๋ฅผ ์–ป์–ด๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด mountinfo ๋ ˆ์ฝ”๋“œ์˜ ๋‹ค์„ฏ ๋ฒˆ์งธ ํ•„๋“œ์— ์žˆ๋Š” ์žฅ์น˜ ์ด๋ฆ„์„ ๊ฐ€์ง€๊ณ  /dev/disks/by-uuid ๋‚ด์˜ ์‹ฌ๋ณผ๋ฆญ ๋งํฌ๋ฅผ ํ†ตํ•ด์„œ ํ•ด๋‹น ์žฅ์น˜์˜ UUID๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค. (UUID๋ฅผ ์–ป๋Š” ๋” ํŽธํ•œ ๋ฐฉ๋ฒ•์€ libblkid(3) ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ด์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.) ๊ทธ๋Ÿฌ๋ฉด ๊ทธ ๊ณผ์ •์„ ๋’ค์ง‘์–ด์„œ UUID๋ฅผ ๊ฐ€์ง€๊ณ  ์žฅ์น˜ ์ด๋ฆ„์„ ์ฐพ์•„์„œ ํ•ด๋‹น ๋งˆ์šดํŠธ ์ง€์ ์„ ์–ป๊ณ , ๊ทธ๋ž˜์„œ open_by_handle_at()์— ์“ธ mount_fd ์ธ์ž๋ฅผ ๋งŒ๋“ค์–ด ๋‚ผ ์ˆ˜ ์žˆ๋‹ค.

EXAMPLE

์•„๋ž˜ ๋‘ ํ”„๋กœ๊ทธ๋žจ์ด name_to_handle_at()๊ณผ open_by_handle_at() ์‚ฌ์šฉ ๋ฐฉ์‹์„ ๋ณด์—ฌ ์ค€๋‹ค. ์ฒซ ๋ฒˆ์งธ ํ”„๋กœ๊ทธ๋žจ(t_name_to_handle_at.c)์—์„œ๋Š” name_to_handle_at()์„ ์‚ฌ์šฉํ•ด ๋ช…๋ นํ–‰ ์ธ์ž๋กœ ์ง€์ •ํ•œ ํŒŒ์ผ์— ๋Œ€ํ•œ ํŒŒ์ผ ํ•ธ๋“ค๊ณผ ๋งˆ์šดํŠธ ID๋ฅผ ์–ป๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ํ•ธ๋“ค๊ณผ ๋งˆ์šดํŠธ ID๋ฅผ ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ์ฐ๋Š”๋‹ค.

๋‘ ๋ฒˆ์งธ ํ”„๋กœ๊ทธ๋žจ(t_open_by_handle_at.c)์—์„œ๋Š” ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ๋ถ€ํ„ฐ ๋งˆ์šดํŠธ ID์™€ ํŒŒ์ผ ํ•ธ๋“ค์„ ์ฝ์–ด ๋“ค์ธ๋‹ค. ๊ทธ๋Ÿฌ๊ณ  ๋‚˜์„œ open_by_handle_at()์„ ์‚ฌ์šฉํ•ด ๊ทธ ํ•ธ๋“ค๋กœ ํŒŒ์ผ์„ ์—ฐ๋‹ค. ์„ ํƒ์ ์ธ ๋ช…๋ นํ–‰ ์ธ์ž๋ฅผ ์ค€ ๊ฒฝ์šฐ์—๋Š” ์ง€๋ช…๋œ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์—ด์–ด์„œ open_by_handle_at()์— ์“ธ mount_fd ์ธ์ž๋ฅผ ์–ป๋Š”๋‹ค. ์•„๋‹Œ ๊ฒฝ์šฐ์—๋Š” /proc/self/mountinfo๋ฅผ ํƒ์ƒ‰ํ•ด์„œ ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ์ฝ์€ ๋งˆ์šดํŠธ ID์™€ ์ผ์น˜ํ•˜๋Š” ๋ ˆ์ฝ”๋“œ๋ฅผ ์ฐพ์€ ๋‹ค์Œ ๊ทธ ๋ ˆ์ฝ”๋“œ์— ๋‚˜์˜จ ๋งˆ์šดํŠธ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ์—ด์–ด์„œ mount_fd๋ฅผ ์–ป๋Š”๋‹ค. (์ด ํ”„๋กœ๊ทธ๋žจ๋“ค์—์„œ๋Š” ๋งˆ์šดํŠธ ID๊ฐ€ ์˜์†์ ์ด์ง€ ์•Š๋‹ค๋Š” ์ ๊นŒ์ง€๋Š” ๋‹ค๋ฃจ์ง€ ์•Š๋Š”๋‹ค.)

๋‹ค์Œ ์…ธ ์„ธ์…˜์ด ๋‘ ํ”„๋กœ๊ทธ๋žจ์˜ ์‚ฌ์šฉ ๋ฐฉ์‹์„ ๋ณด์—ฌ ์ค€๋‹ค.

$ echo 'Can you please think about it?' > cecilia.txt
$ ./t_name_to_handle_at cecilia.txt > fh
$ ./t_open_by_handle_at < fh
open_by_handle_at: Operation not permitted
$ sudo ./t_open_by_handle_at < fh      # CAP_SYS_ADMIN ํ•„์š”ํ•จ
Read 31 bytes
$ rm cecilia.txt

์ด์ œ ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๊ณ  (์žฌ๋นจ๋ฆฌ) ๋‹ค์‹œ ๋งŒ๋“ค์–ด์„œ ๋‚ด์šฉ์ด ๊ฐ™๊ณ  (์šฐ์—ฐํžˆ) ์•„์ด๋…ธ๋“œ๋„ ๊ฐ™๋„๋ก ํ•œ๋‹ค. ๊ทธ๋ ‡๊ฒŒ ํ•ด๋„ open_by_handle_at()์—์„œ ํŒŒ์ผ ํ•ธ๋“ค์ด ๊ฐ€๋ฆฌํ‚ค๋Š” ์›๋ž˜ ํŒŒ์ผ์ด ๋” ์ด์ƒ ์กด์žฌํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ๊ฑธ ์•ˆ๋‹ค.

$ stat --printf="%i\n" cecilia.txt     # ์•„์ด๋…ธ๋“œ ๋ฒˆํ˜ธ ํ‘œ์‹œ
4072121
$ rm cecilia.txt
$ echo 'Can you please think about it?' > cecilia.txt
$ stat --printf="%i\n" cecilia.txt     # ์•„์ด๋…ธ๋“œ ๋ฒˆํ˜ธ ํ™•์ธ
4072121
$ sudo ./t_open_by_handle_at < fh
open_by_handle_at: Stale NFS file handle

ํ”„๋กœ๊ทธ๋žจ ์†Œ์Šค: t_name_to_handle_at.c

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
                        } while (0)

int
main(int argc, char *argv[])
{
    struct file_handle *fhp;
    int mount_id, fhsize, flags, dirfd, j;
    char *pathname;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s pathname\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    pathname = argv[1];

    /* file_handle ๊ตฌ์กฐ์ฒด ํ• ๋‹น */

    fhsize = sizeof(*fhp);
    fhp = malloc(fhsize);
    if (fhp == NULL)
        errExit("malloc");

    /* ์ฒซ ๋ฒˆ์งธ name_to_handle_at() ํ˜ธ์ถœ๋กœ ํŒŒ์ผ ํ•ธ๋“ค์— ํ•„์š”ํ•œ
       ํฌ๊ธฐ ์•Œ์•„๋‚ด๊ธฐ */

    dirfd = AT_FDCWD;           /* name_to_handle_at() ํ˜ธ์ถœ์— ์‚ฌ์šฉ */
    flags = 0;                  /* name_to_handle_at() ํ˜ธ์ถœ์— ์‚ฌ์šฉ */
    fhp->handle_bytes = 0;
    if (name_to_handle_at(dirfd, pathname, fhp,
                &mount_id, flags) != -1 || errno != EOVERFLOW) {
        fprintf(stderr, "Unexpected result from name_to_handle_at()\n");
        exit(EXIT_FAILURE);
    }

    /* ์˜ฌ๋ฐ”๋ฅธ ํฌ๊ธฐ๋กœ file_handle ๊ตฌ์กฐ์ฒด ์žฌํ• ๋‹น */

    fhsize = sizeof(struct file_handle) + fhp->handle_bytes;
    fhp = realloc(fhp, fhsize);         /* fhp->handle_bytes ๋ณต์‚ฌ๋จ */
    if (fhp == NULL)
        errExit("realloc");

    /* ๋ช…๋ นํ–‰์—์„œ ๋ฐ›์€ pathname์„ ๊ฐ€์ง€๊ณ  ํŒŒ์ผ ํ•ธ๋“ค ์–ป๊ธฐ */

    if (name_to_handle_at(dirfd, pathname, fhp, &mount_id, flags) == -1)
        errExit("name_to_handle_at");

    /* ๋งˆ์šดํŠธ ID, ํŒŒ์ผ ํ•ธ๋“ค ํฌ๊ธฐ, ํŒŒ์ผ ํ•ธ๋“ค์„ stdout์œผ๋กœ ์ถœ๋ ฅ.
       ์ดํ›„ t_open_by_handle_at.c์—์„œ ์‚ฌ์šฉํ•จ */

    printf("%d\n", mount_id);
    printf("%d %d   ", fhp->handle_bytes, fhp->handle_type);
    for (j = 0; j < fhp->handle_bytes; j++)
        printf(" %02x", fhp->f_handle[j]);
    printf("\n");

    exit(EXIT_SUCCESS);
}

ํ”„๋กœ๊ทธ๋žจ ์†Œ์Šค: t_open_by_handle_at.c

#define _GNU_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#define errExit(msg)    do { perror(msg); exit(EXIT_FAILURE); \
                        } while (0)

/* /proc/self/mountinfo๋ฅผ ํƒ์ƒ‰ํ•ด์„œ ๋งˆ์šดํŠธ ID๊ฐ€ 'mount_id'์™€ ์ผ์น˜ํ•˜๋Š”
   ํ–‰์„ ์ฐพ์•„๋‚ธ๋‹ค. (๋” ์‰ฌ์šด ๋ฐฉ๋ฒ•์€ 'util-linux' ํ”„๋กœ์ ํŠธ์—์„œ ์ œ๊ณตํ•˜๋Š”
   'libmount'๋ฅผ ์„ค์น˜ํ•ด์„œ ์“ฐ๋Š” ๊ฒƒ์ด๋‹ค.)
   ํ•ด๋‹น ๋งˆ์šดํŠธ ๊ฒฝ๋กœ๋ฅผ ์—ด์–ด์„œ ๋‚˜์˜ค๋Š” ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. */

static int
open_mount_path_by_id(int mount_id)
{
    char *linep;
    size_t lsize;
    char mount_path[PATH_MAX];
    int mi_mount_id, found;
    ssize_t nread;
    FILE *fp;

    fp = fopen("/proc/self/mountinfo", "r");
    if (fp == NULL)
        errExit("fopen");

    found = 0;
    linep = NULL;
    while (!found) {
        nread = getline(&linep, &lsize, fp);
        if (nread == -1)
            break;

        nread = sscanf(linep, "%d %*d %*s %*s %s",
                       &mi_mount_id, mount_path);
        if (nread != 2) {
            fprintf(stderr, "Bad sscanf()\n");
            exit(EXIT_FAILURE);
        }

        if (mi_mount_id == mount_id)
            found = 1;
    }
    free(linep);

    fclose(fp);

    if (!found) {
        fprintf(stderr, "Could not find mount point\n");
        exit(EXIT_FAILURE);
    }

    return open(mount_path, O_RDONLY);
}

int
main(int argc, char *argv[])
{
    struct file_handle *fhp;
    int mount_id, fd, mount_fd, handle_bytes, j;
    ssize_t nread;
    char buf[1000];
#define LINE_SIZE 100
    char line1[LINE_SIZE], line2[LINE_SIZE];
    char *nextp;

    if ((argc > 1 && strcmp(argv[1], "--help") == 0) || argc > 2) {
        fprintf(stderr, "Usage: %s [mount-path]\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    /* ํ‘œ์ค€ ์ž…๋ ฅ์—๋Š” ๋งˆ์šดํŠธ ID์™€ ํŒŒ์ผ ํ•ธ๋“ค ์ •๋ณด๊ฐ€ ์žˆ๋‹ค:

         1ํ–‰: <mount_id>
         2ํ–‰: <handle_bytes> <handle_type>   <16์ง„์ˆ˜๋กœ ๋‚˜ํƒ€๋‚ธ ํ•ธ๋“ค>
    */

    if ((fgets(line1, sizeof(line1), stdin) == NULL) ||
           (fgets(line2, sizeof(line2), stdin) == NULL)) {
        fprintf(stderr, "Missing mount_id / file handle\n");
        exit(EXIT_FAILURE);
    }

    mount_id = atoi(line1);

    handle_bytes = strtoul(line2, &nextp, 0);

    /* handle_bytes๊ฐ€ ์ฃผ์–ด์ ธ์„œ ์ด์ œ file_handle ๊ตฌ์กฐ์ฒด๋ฅผ ํ• ๋‹นํ•  ์ˆ˜ ์žˆ์Œ */

    fhp = malloc(sizeof(struct file_handle) + handle_bytes);
    if (fhp == NULL)
        errExit("malloc");

    fhp->handle_bytes = handle_bytes;

    fhp->handle_type = strtoul(nextp, &nextp, 0);

    for (j = 0; j < fhp->handle_bytes; j++)
        fhp->f_handle[j] = strtoul(nextp, &nextp, 16);

    /* ๋งˆ์šดํŠธ ์ง€์ ์— ๋Œ€ํ•œ ํŒŒ์ผ ๋””์Šคํฌ๋ฆฝํ„ฐ ์–ป๊ธฐ.
       ๋ช…๋ นํ–‰์— ์ง€์ •๋œ ๊ฒฝ๋กœ๋ช…์„ ์—ด๊ฑฐ๋‚˜, /proc/self/mounts๋ฅผ
       ํƒ์ƒ‰ํ•ด์„œ stdin์œผ๋กœ ๋ฐ›์€ 'mount_id'์— ์ผ์น˜ํ•˜๋Š” ๋งˆ์šดํŠธ ํ•ญ๋ชฉ
       ์ฐพ์•„๋‚ด๊ธฐ. */

    if (argc > 1)
        mount_fd = open(argv[1], O_RDONLY);
    else
        mount_fd = open_mount_path_by_id(mount_id);

    if (mount_fd == -1)
        errExit("opening mount fd");

    /* ํ•ธ๋“ค๊ณผ ๋งˆ์šดํŠธ ์ง€์ ์œผ๋กœ ํŒŒ์ผ ์—ด๊ธฐ */

    fd = open_by_handle_at(mount_fd, fhp, O_RDONLY);
    if (fd == -1)
        errExit("open_by_handle_at");

    /* ํŒŒ์ผ์—์„œ ๋ช‡ ๋ฐ”์ดํŠธ ์ฝ์–ด ๋ณด๊ธฐ */

    nread = read(fd, buf, sizeof(buf));
    if (nread == -1)
        errExit("read");

    printf("Read %zd bytes\n", nread);

    exit(EXIT_SUCCESS);
}

SEE ALSO

open(2), libblkid(3), blkid(8), findfs(8), mount(8)

util-linux ์ตœ์‹  ๋ฆด๋ฆฌ์Šค์˜ libblkid ๋ฐ libmount ๋ฌธ์„œ (https://www.kernel.org/pub/linux/utils/util-linux/)


2019-03-06

โš ๏ธ **GitHub.com Fallback** โš ๏ธ