리눅스 프로그래밍 4주차 - 3.1(file creation mask) ~ 3.2

고강희·2022년 9월 24일
1

The File Creation Mask

각 프로세스는 생성될 때 file creation mask를 가짐

  • 파일이 생성될 때 특정 permission bit들을 자동으로 off함
  • 어떤 권한이 우연히 turned on 되는것을 방지

The following two lines are coincide

filedes = open(pathname, O_WRONLY|O_CREAT, mode);
filedes = open(pathname, O_WRONLY|O_CREAT, (~mask) & mode);

시스템 상에서 group에대한 write, others의 write 권한을 없앰 그런데 꼭 써야할 경우가 생길수 있음 -> umask

System Call: umask(2)

#include <sys/stat.h>

mod_t umask(mod_t cmask);

umask 함수는 file creation mask를 설정해줌. 리턴값은 이전 file creation mask

mode_t lodmask;
oldmask = umask(022) //file creation mask를 022로 설정
//(others,group의 write권한을 막음) oldmask에는 이전 creation mask 저장

System Call: access(2)

#include <unistd.h>

int access(const char *pathname, int amode);

access는 ruid,rgid를 통해 pathname의 access 권한을 check한다.
성공 return 0, 실패 return -1
amode:

  • R_OK :일기 권한을 검사
  • W_OK :쓰기 권한을 검사
  • X_OK :실행 권한을 검사
  • F_OK :파일이 있는지 없는지 검사

System Call: chmod(2)

#include <unistd.h>

int chmod(const char *pathname, mode_t newmod);

존재하고 있는 파일의 모드를 바꿈
성공 return 0, 실패 return -1
파일의 소유자나 super user만이 사용할 수 있음

System Call: chown(2)

#include <unistd.h>

int chown(const char *pathname, uid_t owner_id, gid_t group_id);

소유자와 소유그룹을 바꾸는 system call
성공 return 0, 실패 return -1

Arguments:

  • owner_id : 새로운 owner_id
  • group_id : 새로운 group_id

이 시스템콜이 CALL되면 S_ISUID, S_ISGID 권한은 turned off됨

3.2 File with Multiple Names

파일에 여러 이름을 부여하는 방법 ex) 바로가기 in window system

i-node

모든 파일은 i-node라는 structure로 관리
i-node 0,1은 사용 x

  • 0: "no i-node"
  • 1: bad disk blocks들을 collect
  • 2: reserved for the root directory (/)

hard link는 file에 대한 직접적인 포인터
Link count는 디렉토리에서 i-node로 향하는 포인터에 갯수
Link count가 0이 돼야지만 file은 삭제될 수 있다.
file들 끼리 cross될 수 없음

symbolic link는 file에 대한 간접적인 포인터
실제 symbolic link의 contents는 그것과 연결된 file
파일 시스템 제한이 없다. (파일들끼리 cross가능)

System Call: link(2)

#include <unistd.h>

int link(const char *original_path, const char *new_path);
  • 새로운 디렉토리를 만들고,link count를 증가시킨다.(return 0 on success, -1 on error)

  • 오직 슈퍼유저만 directory(file이 아닌)에 대한 하드링크를 만들 수 있다.

    • system loop 발생 방지
  • argument -> 같은 파일 시스템 내에 있는 path name으로 줘야한다.

#include <unistd.h>

int unlink(const char *pathname);
  • 존재하고 잇는 디렉토리를 삭제함(return 0 on success, -1 on error)
  • link를 삭제하고 link count를 1 감소시킴
    • 만약 파일에 다른 링크가 있다면 여전히 다른 링크를 통해 그 파일을 접근 할 수 있음
    • 만약 링크 카운트가 0으로 감소된다면, free block list에 disk block이 추가됨

remove(3)

#include <stdio.h>

int remove(const char *pathname);
  • file 시스템에서 remove는 unlink와 완전 동일하지만, directory 시스템에선 조금 다름 (return 0 on success, -1 on error)
  • unlink는 Unix(Linux) specific하다.

System Call: rename(2)

#include <stdio.h>

int rename(const char *oldname, const char *newname);
  • 파일 또는 directory의 이름을 바꿈 (return 0 on success, -1 on error)
  • ISO C stantard에서 파일에 대한 rename은 가능하지만, directory에선 불가능
  • link, unlink로 rename 구현가능

System Call: symlink(2)

#include <unistd.h>

int symlink(const char *realname, const char *symname);
  • Symbolic link
  • symlink는 file의 realname을 가리키는 symname을 가진 새로운 파일을 생성함
  • symname 파일을 open system call을 통해 열면 real name의 파일이 open됨
  • 비록 원본 파일이 삭제되어도, 프로그램은 여전히 symbolic link를 가질 수 있음 하지만 open은 할 수 없고, errno을 EEXIST로 set하고 -1을 리턴
  • symname파일 자체를 열고 싶을때(symname이 가리키는 realname파일이 아닌), readlink call을 사용하면 된다.

System Call: readlink(2)

#include <unistd.h>

int readlink(const char *sympath, char *buffer, size_t bufsize);
  • sympath를 open하고, 파일의 내용을 buffer로 읽어들여 종료한다. (return number of bytes on success, -1 on error)
profile
그냥 AI 관련 유익해보이는거 이것저것 적어놓음

0개의 댓글