Contents

System Programming Ch4

   Oct 21, 2022     4 min read

๐Ÿ’กย Chap_4

stat, fstat, and lstat

#include <sys/stat.h>
int stat(const char *pathname, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *pathname, struct stat *buf);

๐Ÿ“Ž stat, fstat : ํŒŒ์ผ ๊ตฌ์กฐ์— ๋Œ€ํ•œ ์„ค๋ช…์„ ์ถœ๋ ฅํ•œ๋‹ค

  • ํŒŒ์ผ ๊ตฌ์กฐ : i-node์— ์žˆ๋Š” ์ •๋ณด ์ค‘์— ์‚ฌ์ด์ฆˆ ๋ธ”๋ก, ๋ฐ์ดํ„ฐ ๋ช‡๊ฐœ๊ฐ€ ์“ฐ์ž„, ์†Œ์œ ์ฃผ๊ฐ€ ๋ˆ„๊ตฌ์•ผ.. ๋“ฑ๋“ฑ ์ด i-node๋ฅผ ์ฐธ์กฐํ•ด์„œ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ

lstat : ์•ž์— ๋ถ™์€ l์€ link, symbolic link์˜ ํŒŒ์ผ ๊ตฌ์กฐ๋‚˜ ์ •๋ณด๋ฅผ ๋ฐ˜ํ™˜


Set-User-ID and Set-Group-ID

Set-User-ID and Set-Group-ID

์‹ค์Šต 6์ฃผ์ฐจ ๊ฐ•์˜ ์ž๋ฃŒ

๐Ÿ“Ž setuid - 4 (user์— super ๊ถŒํ•œ) setgid - 2 (group์— super ๊ถŒํ•œ) sticky bit - 1 (other์— super ๊ถŒํ•œ)


File Access Permissions

๐Ÿ“Ž Directory

R - to obtain a list of all the file names in the dir W - to create/delete a file in the directory, both X and W are necessary X - to pass through the directory comprising a pathname, also called โ€œsearch bitโ€

File

R - O_RDONLY and O_RDWR for the open function

W - O_WRONLY, O_RDWR, O_TRUNC X - exec functions

(๋‹จ๋‹ตํ˜•) ๋””๋ ‰ํ† ๋ฆฌ์— ์ƒˆ๋กœ์šด ํŒŒ์ผ์„ ๋งŒ๋“ค ๋•Œ ํ•„์š”ํ•œ ๊ถŒํ•œ์€ ๋ฌด์—‡์ธ๊ฐ€?

โ‡’ X, W

(๋‹จ๋‹ตํ˜•) ๋””๋ ‰ํ† ๋ฆฌ์— ์กด์žฌํ•˜๋Š” ํŒŒ์ผ์„ ์ง€์šธ ๋•Œ ํ•„์š”ํ•œ ๊ถŒํ•œ์€ ๋ฌด์—‡์ธ๊ฐ€?

โ‡’ X, W

๐Ÿ“Ž access Function

  • ์ ‘๊ทผ ๊ถŒํ•œ์ด ์œ ํšจํ•œ์ง€๋ฅผ ํŒ๋‹จํ•ด์„œ ๋ฐ˜ํ™˜ํ•ด์ฃผ๋Š” ํ•จ์ˆ˜
  • ์ ‘๊ทผํ•  ๋•Œ ์“ฐ๋Š” ๊ฒƒ์ด ์•„๋‹Œ, ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋Š” ๊ถŒํ•œ์ด ์žˆ๋Š”์ง€ ์—†๋Š”์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ด์คŒ
#include <unistd.h>
int access(const char *pathname, int mode);

mode : R_OK(R๊ถŒํ•œ์ด ์—†์œผ๋ฉด 0 ๋ฐ˜ํ™˜, ์žˆ์œผ๋ฉด ์–‘์ˆ˜ ๋ฐ˜ํ™˜), W_OK, X_OK, F_OK(R,W,X ์ „๋ถ€ ๋‹ค)

๐Ÿ“Ž umask Function

#include <sys/stat.h>
mode_t umask(mode_t, cmask);
  • ํŒŒ์ผ์ด๋‚˜ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, ์ดˆ๊ธฐ๊ถŒํ•œ์„ ๋ญ˜๋กœ ์ค„์ง€ ๊ฒฐ์ •์ง€์„ ์ˆ˜ ์žˆ๋Š” ํ•จ์ˆ˜

  • ํŒŒ์ผ์€ ์‹คํ–‰๊ถŒํ•œ(X)์ด ํ•„์š”์—†์Œ ์ฆ‰, ํŒŒ์ผ์˜ ์ตœ๋Œ€๊ถŒํ•œ์€ 666

    ํŒŒ์ผ์˜ ์ตœ๋Œ€ ๊ถŒํ•œ666666
    umask002004
    umask๋ฅผ ๋งŒ๋“ค๊ณ  ๋ฐ”๋€ ๊ถŒํ•œ664662
  • umask๋Š” ํŒŒ์ผ์˜ ์ตœ๋Œ€๊ถŒํ•œ์—์„œ ์ง€๊ธˆ ์„ธํŒ…๋˜์–ด์žˆ๋Š” ์œ ๋งˆ์Šคํฌ ๊ฐ’์„ ๋บ€ ๊ฐ’์œผ๋กœ ํŒŒ์ผ์„ ์ƒ์„ฑํ•จ
  • umask๋Š” ํ•จ์ˆ˜, cmask๋Š” ์ธ์ž (umask์™€ cmask ๊ฐ’์˜ ์˜๋ฏธ๋Š” ๋™์ผ)
#include "apue.h"
#include <fcntl.h>
#define RWRWRW (S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH)

int main(void)
{ //ํŒŒ์ผ์˜ ์ตœ๋Œ€ ๊ถŒํ•œ์€ 666
	umask(0); //umask 0
	if(creat("foo", RWRWRW) < 0) //foo๋Š” 666์œผ๋กœ ์ƒ์„ฑ
		err_sys("creat error for foo")
	umask(S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH); //umask 066

	if(creat("bar", RWRWRW) < 0)//bar๋Š” 600์œผ๋กœ ์ƒ์„ฑ
		err_sys("creat error for bar");

	exit(0);
}

umask Function

๐Ÿ“Ž chmod, fchmod Functions

  • chmod : change mode, ํŒŒ์ผ์˜ ์ ‘๊ทผ ๊ถŒํ•œ์„ ๋ฐ”๊ฟ€ ๋•Œ ์“ฐ๋Š” ํ•จ์ˆ˜
  • fchmod : ๊ฐ™์€ ์—ญํ• , ์ธ์ž๋งŒ ๋‹ค๋ฆ„

๐Ÿ“Ž Sticky Bit

  • 1000์œผ๋กœ ์„ธํŒ…๋œ ํŠน์ˆ˜๊ถŒํ•œ

๐Ÿ“Ž chown, fchown, and lchown

  • chown : ์†Œ์œ ์ž์™€ ์†Œ์œ ๊ทธ๋ฃน์„ ๋ฐ”๊พธ๋Š” ํ•จ์ˆ˜
  • fchown : ์ธ์ž๋งŒ ๋‹ค๋ฆ„
  • lchown : symbolic link ์ž์ฒด์˜ ์†Œ์œ ์ฃผ์™€ ์†Œ์œ ๊ทธ๋ฃน์„ ๋ฐ”๊ฟˆ

๐Ÿ“Ž File Truncation

(๋‹จ๋‹ตํ˜•) ๋‹ค์Œ ์ค‘ ์ธ์ž๋กœ ๋„˜๊ฒจ์ฃผ๋Š” length๋งŒํผ ์ž˜๋ผ๋‚ด๋Š” ํ•จ์ˆ˜๋Š” ๋ฌด์—‡์ธ๊ฐ€?

โ‡’ truncate, ftruncate


symbolic link

๊ทธ๋ฆผ, ์ฝ”๋“œ ์ „๋ถ€ ์™ธ์šฐ๊ธฐ

๐Ÿ“Ž

  • foo directory ์•ˆ์— file a ๋ž‘ testdir directory๊ฐ€ ์žˆ์Œ
  • ln -s ../foo foo/testdir

    โ€”> testdir์ด ์ž๊ธฐ ๋ถ€๋ชจ์ธ foo directory๋ฅผ ํฌ์ธํŒ…ํ•จ (symbolic link๋ฅผ ๊ฑธ์—ˆ์Œ)

*directory๋ฅผ link ๊ฑธ ์ˆ˜ ์žˆ๋Š” ๊ฑด ๋ฌด์กฐ๊ฑด ์˜ค๋กœ์ง€ root ์‚ฌ์šฉ์ž๋งŒ (superuser๋งŒ)

๐Ÿ“Ž Symbolic Links

symbolic link

  • symbolic link (=soft link) ๋Š” hard link ์™€ ๋Œ€๋น„๋จ
  • H1์„ ์†Œ์Šค๋กœ ํ•˜๋Š” hard link๋ฅผ ๊ฑด H2, H3๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ์œ„์™€ ๊ฐ™์€ ํ˜•ํƒœ๊ฐ€ ๋จ
  • Hard link๋Š” ๊ฐ™์€ i-node๋ฅผ ๊ฐ€๋ฆฌํ‚ด
  • soft link๋Š” s1์„ ์›๋ณธ์œผ๋กœ s2๋ฅผ ๋งŒ๋“ค ๋•Œ, s2๋Š” ๋…๋ฆฝ์ ์ธ i-node๋ฅผ ํฌ์ธํŒ… ํ•˜๋Š”๋ฐ, ๊ทธ i-node๊ฐ€ origin์„ ํฌ์ธํŒ…ํ•˜๊ฒŒ๋จ
  • hard link๋Š” H1์„ ์‚ญ์ œํ•ด๋„ H2, H3์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์ง€๋งŒ,
  • soft link๋Š” S1์„ ์‚ญ์ œํ•˜๋ฉด i-node๋„ ๊ฐ™์ด ์‚ญ์ œ๋˜๋ฏ€๋กœ S2๋ฅผ ์—ด ์ˆ˜ ์—†์Œ
  • soft link๋Š” ๋ฐ”๋กœ๊ฐ€๊ธฐ ๋ผ๊ณ  ํ•จ, ์›๋ณธ์„ ๋ณต์ œ๋Š” ํ•˜์ง€๋งŒ ๊ทธ ์ž์ฒด๋ฅผ ๋ณต์ œํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๊ทธ ์›๋ณธ์„ ํฌ์ธํŒ…ํ•˜๋Š” ๋ญ”๊ฐ€๋ฅผ ๋งŒ๋“ฌ

๐Ÿ“Ž symlink, readlink Function

#include <unistd.h>
int symlink(const char *actualpath, const char *sympath);

symlink : symbolic link๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์“ฐ๋Š” ํ•จ์ˆ˜

#include <unistd.h>
ssize_t readlink(const char *pathname, char *buf, size_t bufsize);

readlink : link๊ฐ€ ๊ฑธ๋ ค์žˆ๋Š”, ์‹ค์ œ ํฌ์ธํŒ…ํ•˜๊ณ  ์žˆ๋Š” ํŒŒ์ผ์„ ์˜คํ”ˆํ•ด์ฃผ๋Š”, ์ฝ์–ด์ฃผ๋Š” ํ•จ์ˆ˜ (equivalent to the actions of open, read, and close)

๐Ÿ“Ž link, unlink, remove, and rename Functions

  • link : hard link๋ฅผ ๋งŒ๋“ฌ
  • unlink
  • remove : file์— ์“ฐ๋ฉด unllink์™€ ๊ฐ™์€ ํšจ๊ณผ, ๋””๋ ‰ํ† ๋ฆฌ์— ์“ฐ๋ฉด rmdir๊ณผ ๊ฐ™์€ ํšจ๊ณผ
  • rename

symclink๋ฅผ ์„ค๋ช…ํ•  ๋•Œ๋Š” ๋ฌด์กฐ๊ฑด symbolic link๊ฐ€ ๋‚˜์˜ด

์ด๋Ÿฐ ๋‚ด์šฉ์ด ์•ˆ๋‚˜์˜ค๋ฉด ๋ฌด์กฐ๊ฑด link ํ•จ์ˆ˜

๐Ÿ“Ž utime Function

#include <sys/types.h>
#include <utime.h>
int utime(const char *pathname, const struct utimbuf *times);
struct utimbuf{
	time_t actime /* access time */
	time_t modtime /* modification time */
}
  • ํŒŒ์ผ์— ์ ‘๊ทผํ•œ ์‹œ๊ฐ„(์ƒ์„ฑํ•œ ์‹œ๊ฐ„)๊ณผ ๋ณ€๊ฒฝํ•œ ์‹œ๊ฐ„์„ ์•Œ๊ณ  ์‹ถ์„ ๋•Œ ์“ฐ๋Š” ํ•จ์ˆ˜

๐Ÿ“Ž mkdir and rmdir Fuction mkdir : make directory rmdir : remove directory

๐Ÿ“Ž chdir, fchdir, and getcwd

chdir : change directory, ๋‚ด๊ฐ€ ํ˜„์žฌ ์ž‘์—…ํ•˜๊ณ  ์žˆ๋Š” ๊ณณ(working directory)์˜ ๊ฒฝ๋กœ๋ฅผ ๋ฐ”๊ฟ€ ๋•Œ ์“ฐ๋Š” ํ•จ์ˆ˜

fchdir : ์ธ์ž๋งŒ ๋‹ค๋ฆ„

getcwd : current working directory์˜ ๊ฒฝ๋กœ๋ฅผ ๊ฐ€์ง€๊ณ  ์˜ค๋Š” ํ•จ์ˆ˜