8. Binary Flags and Files

SoftSys Lecture Notes, Monday 3/1/21 #

Hard Drive Operation #

Binary Flags #

Binary Flags in C #

You may remember code from Homework 2.5 like this:

regex_t re;
ret = regcomp(&re, pattern, REG_EXTENDED | REG_NOSUB);

We’re going to do an exercise to better understand what REG_EXTENDED | REG_NOSUB does here.

Do the following as an exercise:

  1. Write a C program that includes regex.h. (The program doesn’t have to do anything useful.)
  2. Use the command gcc -H <file.c> to see where regex.h lives on your system.
  3. In regex.h, what are the values of REG_EXTENDED and REG_NOSUB? Do you see other variables nearby that have similar values? (REG_EXTENDED == 0001) (REG_NOSUB == 0004)
  4. What does REG_EXTENDED | REG_NOSUB equal? Why might you want to calculate this value when calling regcomp?

0001 = 1 0100 = 4 #

0101 = 5 #

“|” -> Bitwise OR operator At a glance, you can see exactly which flags are set to True. You can check to see if a bit is set by using the bitwise AND operator “&”.

Binary Flags in Unix #

Binary flags are used for access control in Unix files.

Read the following zine by the excellent Julia Evans and then answer the question below.

  1. Read about setuid and setgid here: https://en.wikipedia.org/wiki/Setuid. What are the three things that setgid does for different file types?
  2. Read about the sticky bit at https://en.wikipedia.org/wiki/Sticky_bit. Describe one example use case in which you might want to use a sticky bit on a directory.
  3. What is the 4-digit octal code for a file that is (1) sticky but not setuid or setgid, (2) can be read, written, and executed by the owning user, (3) can be read and executed by the owner’s group, and (4) can be read by all other users?
  4. Read about chmod symbolic modes by running man chmod or here: https://en.wikipedia.org/wiki/Chmod#Symbolic_modes. What is the symbolic command to create permission that equal the octal code above, starting from unknown permissions?
  5. Create a file called id.sh with the following contents:
    #!/usr/bin/env bash
    id
    
  6. Make the file executable by adding executable with chmod, and run it. What do you get?
  7. Now change the owner to root by running sudo chown root:root id.sh, and run chmod again to add setuid permissions. Then run the file again. What do you get now?

I/O Performance #

One of the simplest command-line utilities is a program called yes. If you run it, you will see it simply print out y over and over again, once per line. (Use Ctrl-C to stop this if you start running it.)

We’re going to try an exercise to see how it performs.

  1. First, install the program pv on your machine by running sudo apt install pv (or the equivalent command for Mac, probably brew install pv).
  2. Let’s benchmark the actual version of yes to see how it performs. Run yes | pv -r > /dev/null to see how fast yes writes. (Again, use Ctrl-C to stop execution.)
  3. Now write your own version of this utility called yes.c. Compile and run it in the same way as above to measure its speed. How does it compare?
  4. Your implementation is probably a bit slower than the built-in yes program, so let’s try to get it a bit faster. Read about the write system call by running man 2 write (on most systems). Try using this to write y repeatedly.
  5. Define a constant in yes.c called COPIES (you can do this by adding a line like #define COPIES 42 at the top of the file). Then modify your implementation so that you create a string containing COPIES copies of y\n (what yes prints by default), and try measuring the speed of your program again.
  6. If you have time, try playing around with the value of COPIES to see what kinds of values help make the performance faster or slower.