CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

Similar documents
I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Files and Directories Filesystems from a user s perspective

I/O OPERATIONS. UNIX Programming 2014 Fall by Euiseong Seo

Linux C C man mkdir( ) mkdir Linux man mkdir mkdir( ) mkdir mkdir( )

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

System Calls. Library Functions Vs. System Calls. Library Functions Vs. System Calls

CS 33. Files Part 2. CS33 Intro to Computer Systems XXI 1 Copyright 2018 Thomas W. Doeppner. All rights reserved.

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CSci 4061 Introduction to Operating Systems. File Systems: Basics

f90 unix file: Unix File Operations Module March 9, 2009

Files and Directories

Hyo-bong Son Computer Systems Laboratory Sungkyunkwan University

Preview. System Call. System Call. System Call. System Call. Library Functions 9/20/2018. System Call

Files and Directories

The link() System Call. Preview. The link() System Call. The link() System Call. The unlink() System Call 9/25/2017

Lecture files in /home/hwang/cs375/lecture05 on csserver.

CSCI-E28 Lecture 3 Outline. Directories, File Attributes, Bits, File Operations. Write our own versions of Unix programs

How do we define pointers? Memory allocation. Syntax. Notes. Pointers to variables. Pointers to structures. Pointers to functions. Notes.

Lecture 21 Systems Programming in C

Parents and Children

Lecture 3. Introduction to Unix Systems Programming: Unix File I/O System Calls

Unix System Calls. Gwan-Hwan Hwang Dept. CSIE National Taiwan Normal University

Files and Directories Objectives Additional Features of the File System Properties of a File. Three major functions that return file information:

CMPSC 311- Introduction to Systems Programming Module: Input/Output

Original ACL related man pages

Homework 5. Due Date: Friday, June 7, 2002, at 11:59PM; no late assignments accepted Points: 100

System Calls and I/O Appendix. Copyright : University of Illinois CS 241 Staff 1

Housekeeping (Lecture 12-6/26/2018)

CSC 271 Software I: Utilities and Internals

File and Directories. Advanced Programming in the UNIX Environment

Summer June 15, 2010

Files and directories. Updated by: Dr. Safwan Qasem Spring 2010 Original version created by: Dr. Mohamed El Bachir Menai

CS631 - Advanced Programming in the UNIX Environment

CS140 Operating Systems Final December 12, 2007 OPEN BOOK, OPEN NOTES

TEL2821/IS2150: INTRODUCTION TO SECURITY Lab: Operating Systems and Access Control

Processes are subjects.

ADVANCED OPERATING SYSTEMS UNIT 2 FILE AND DIRECTORY I/O BY MR.PRASAD SAWANT

CptS 360 (System Programming) Unit 6: Files and Directories

Files (review) and Regular Expressions. Todd Kelley CST8207 Todd Kelley 1

CIS Operating Systems File Systems Security. Professor Qiang Zeng Fall 2017

TCSS 422: OPERATING SYSTEMS

CMSC 216 Introduction to Computer Systems Lecture 17 Process Control and System-Level I/O

CS/CIS 249 SP18 - Intro to Information Security

INTRODUCTION TO THE UNIX FILE SYSTEM 1)

Contents. NOTICE & Programming Assignment #1. QnA about last exercise. File IO exercise

UNIX System Calls. Sys Calls versus Library Func

Note: Before using this information and the product it supports, read the information in Notices.

Fall 2017 :: CSE 306. File Systems Basics. Nima Honarmand

Linux Programming Chris Seddon

TEL2821/IS2150: INTRODUCTION TO SECURITY Lab: Operating Systems and Access Control

NAME attr extended attributes on XFS filesystem objects. SYNOPSIS attr [ LRq ] s attrname [ V attrvalue ] pathname

Operating systems fundamentals - B10

Thesis, antithesis, synthesis

System Calls and I/O. CS 241 January 27, Copyright : University of Illinois CS 241 Staff 1

Processes are subjects.

412 Notes: Filesystem

Operating system security models

Introduction. Files. 3. UNIX provides a simple and consistent interface to operating system services and to devices. Directories

CSCI 4500/8506 Operating Systems Some UNIX System Calls, Library, and PThreads Functions

Files and File Systems

Preview. lseek System Calls. A File Copy 9/18/2017. An opened file offset can be explicitly positioned by calling lseek system call.

Systems Programming. COSC Software Tools. Systems Programming. High-Level vs. Low-Level. High-Level vs. Low-Level.

5/8/2012. Encryption-based Protection. Protection based on Access Permission (Contd) File Security, Setting and Using Permissions Chapter 9

Overview. Unix System Programming. Outline. Directory Implementation. Directory Implementation. Directory Structure. Directories & Continuation

File System. Minsoo Ryu. Real-Time Computing and Communications Lab. Hanyang University.

Operating Systems CMPSCI 377 Spring Mark Corner University of Massachusetts Amherst

UNIT I INTRODUCTION TO UNIX & FILE SYSTEM

CSCI 4061: Files, Directories, Standard I/O

Secure Software Programming and Vulnerability Analysis

Last Week: ! Efficiency read/write. ! The File. ! File pointer. ! File control/access. This Week: ! How to program with directories

User Commands chmod ( 1 )

bash startup files Linux/Unix files stty Todd Kelley CST8207 Todd Kelley 1

Operating Systems Lab

Discretionary Access Control

Outline. File Systems. File System Structure. CSCI 4061 Introduction to Operating Systems

UNIX input and output

3/26/2014. Contents. Concepts (1) Disk: Device that stores information (files) Many files x many users: OS management

Process Creation in UNIX

Operating System Labs. Yuanbin Wu

Programmation Systèmes Cours 4 IPC: FIFO

Chapter 4 - Files and Directories. Information about files and directories Management of files and directories

CSCE 313 Introduction to Computer Systems

Access Control. CMPSC Spring 2012 Introduction Computer and Network Security Professor Jaeger.

CSCI 2132 Software Development. Lecture 5: File Permissions

All the scoring jobs will be done by script

Advanced Systems Security: Ordinary Operating Systems

All the scoring jobs will be done by script

File Systems. Jinkyu Jeong Computer Systems Laboratory Sungkyunkwan University

Shared Memory Memory mapped files

UNIX SYSTEM PROGRAMMING

OPERATING SYSTEMS: Lesson 12: Directories

Files and Directories E. Im

Privileges: who can control what

Programmation Système Cours 4 IPC: FIFO

CS240: Programming in C

Operating systems. Lecture 7

which maintain a name to inode mapping which is convenient for people to use. All le objects are

Chapter 10. The UNIX System Interface

Files and the Filesystems. Linux Files

Files and Directories Filesystems from a user s perspective

Transcription:

CSI 402 Lecture 11 (Unix Discussion on Files continued) 11 1 / 19

User and Group IDs Ref: Chapter 3 of [HGS]. Each user is given an ID (integer) called uid. (Most system programs use uid instead of the user s login name.) The special uid 0 (zero) is given to the super user (root). Each user also belongs to at least one group. Each group has an ID (integer) called gid. When a file is created, the uid and the gid of the user who created the file are stored as part of the information regarding the file. 11 2 / 19

More on File Modes Recall: Permissions (modes) can be specified as octal values (for user, group and other). Symbolic form also possible (constants defined in <sys/stat.h>): S_IRUSR S_IWUSR S_IXUSR S_IRWXU S_IRGRP S_IWGRP S_IXGRP S_IRWXG S_IROTH S_IWOTH S_IXOTH S_IRWXO Sample code segment: int fd; mode_t fmode = S_IRWXU S_IRGRP S_IROTH; if ((fd = open("file", O_WRONLY, fmode)) == -1) fprintf(stderr, "Error in open\n"); exit(1); } 11 3 / 19

Permission Bits for Executable Files Three extra permission bits, usually for executable files. Set uid on execution: 04000 S_ISUID Set gid on execution: 02000 S_ISGID Save-text-image ( sticky bit ): 01000 S_ISVTX Purpose of the Setuid Bit: If S_ISUID permission for a file is set, the effective uid of the process executing the file is the uid of the owner of the file (rather than the uid of the user who started the execution). The process is given file access privileges of the file owner. 11 4 / 19

Permission Bits for Executable Files (continued) Example: The passwd program. Program used to change a user s password. Changing password requires permission to write to a file containing password information. (Normally, only root has permission to write to this file.) The executable file for the program is owned by root but has its S_ISUID permission set. When a normal user runs the program, the program is given the effective user id of the owner (root). So, entries in the password file can be modified. 11 5 / 19

Permission Bits for Executable Files (continued) Other examples: The following programs also (generally) have their S_ISUID permission set. Program to print files (usually lpr). Programs that update files containing top scorer information for some games. Setgid bit: Similar to Setuid bit. Sticky Bit: Original purpose: An executable with the sticky bit set is saved in the swap area of the disk (so that it can loaded into memory quickly). With virtual memory, this bit is redundant for executable files. 11 6 / 19

File Creation Mask (umask) When a file is created, permissions specified by mode are always modified using the 9-bit quantity called umask. Generally, value of umask is specified in a start up file such as.bash_profile. Value can be found using the shell command umask. (Usual default value: 022) Actual mode used for the file is given by given_mode & ~umask Setting umask to 022 prevents the open system call from creating a file that can be written by anyone other than the owner. 11 7 / 19

File Creation Mask (continued) System call for umask: mode_t umask (mode_t newmask) The call changes umask to the value specified by the parameter and returns the old value of umask. Reading Assignment: Program example on page 45 of [HGS]. 11 8 / 19

Additional comments regarding open The call to open checks whether the specified access flags satisfy the file permissions. If not, the call returns -1 and errno is set to EACCES ( permission denied error). Special flag O_EXCL prevents creation of file if the file already exists. If flag is given as O_WRONLY O_CREAT O_EXCL and the file exists, open fails and errno is set to EEXIST. 11 9 / 19

Additional System Calls System call chmod: Prototype: int chmod (const char *pathname, mode_t newmode) Can be used to change the permission bits of a file (including the special permission bits for executable files). The parameters represent the file name and the new permission bits respectively. The call succeeds only when issued by the owner of the file or the superuser (root). The value of umask is not used by chmod. Returns 0 if successful and -1 otherwise. 11 10 / 19

Additional System Calls (continued) System call chown: Prototype: int chown (const char *pathname, uid_t newowner, gid_t newgroup) Can be used to change the owner and the group of a file. The parameters represent the file name, the uid of the new owner and the gid of the new group respectively. If the newowner parameter is -1, the owner is not changed. A similar comment applied to the newgroup parameter. 11 11 / 19

Additional System Calls (continued) System call chown (continued): Caution: Once the owner is changed, the change cannot be undone by the previous owner. The call succeeds only when issued by the owner of the file or the superuser (root); otherwise, errno is set to EPERM. Returns 0 if successful and -1 otherwise. 11 12 / 19

Hard Links and Symbolic (Soft) Links Link: A mechanism that allows a file to be referred to by different names. Note: copy. There is only one copy of the file; the links refer to the same Useful in allowing different software teams to work together. Shell command for creating a hard link: % ln oldfile secondname Shell command for creating a symbolic (or soft) link: % ln -s oldfile anothername Caution: If a file is deleted, the symbolic links for the file become dangling pointers. 11 13 / 19

Hard Links and Symbolic Links (continued) Hard Links and Symbolic links: Differences A normal user cannot create a hard link to a directory; however, a user may create a symbolic link to a directory. A hard link must be to a file within the same file system. A soft link may go across file systems. The number of hard links to a file is stored as part of the information regarding the file. The system does not store information about the number of symbolic links. 11 14 / 19

Additional System Calls (continued) System call link: Prototype: int link (const char *original_path, const char *new_path) Creates a hard link to an existing file. The parameters represent the name of an existing file and that of the link respectively. The link may be in a different directory. Returns 0 if successful and -1 otherwise. 11 15 / 19

Additional System Calls (continued) Some comments regarding unlink: The call to unlink only removes the link named in the call. The number of hard links to the file is decremented by 1. The disk space allocated to the file is reclaimed only if both of the following conditions hold: 1 The hard link count has dropped to zero. 2 No process has the file open. 11 16 / 19

Additional System Calls (continued) System call symlink: Prototype: int symlink (const char *real_name, const char *sym_name) Creates a symbolic link to an existing file. The parameters represent the name of an existing file and that of the symbolic link respectively. The link may be in a different file system. Returns 0 if successful and -1 otherwise. 11 17 / 19

Additional System Calls (continued) System calls stat, fstat and lstat: Prototypes: int stat (const char *pathname, struct stat *buf) int fstat (int filedes, struct stat *buf) int lstat (const char *pathname, struct stat *buf) Allow us to obtain information about files. In all three calls, the second parameter is a pointer to the buffer where information about the file can be stored. (The space for the buffer must have been allocated before the call.) stat: The first parameter represents the name of an existing file. fstat: The first parameter represents the file descriptor of an open file. 11 18 / 19

Additional System Calls (continued) System calls stat, fstat and lstat (continued): lstat: The first parameter represents the name of a symbolic link. The call returns information about the link rather than the file pointed to by the link. All the calls return 0 if successful and -1 otherwise. Data Members of struct stat: See Handout 11.1. (Additional discussion appears on page 54 of [HGS].) Program example: Handout 11.2. Reading Assignment: Program example on pages 56 57 of [HGS]. 11 19 / 19