\documentclass{ictlab}

\RCS $Revision: 1.3 $

\usepackage{alltt,key}

\ifx\pdftexversion\undefined \else
  \usepackage[pdfpagemode=None,pdfauthor={Nick Urbanik}]{hyperref}
\fi

\newcommand*{\labTitle}{Assignment: Shell Programming}
\renewcommand*{\subject}{Operating Systems and Systems Integration}

\providecommand{\SUID}{\acro{SUID}\xspace}{
\providecommand{\SGID}{\acro{SGID}\xspace}{

\begin{document}

\paragraph{Submission:}

by 8\,pm, Sunday, 16 May 2004

\paragraph{Where:}
\label{pag:submission}

Electronically via this \URL:
\url{http://nicku.org/perl2/submit.cgi} \ You can submit
as many times as you like; a later submission will never overwrite an
earlier submission.

If you are inside the Campus, you can use \SSL and submit via
\url{https//nicku.org/perl2/submit.cgi}

\paragraph{Format:}

Must be \emph{plain text}.  Submissions in proprietary formats
(i.e., Word documents) will be copied straight to \texttt{/dev/null}
and will receive \emph{ZERO MARKS}.

You can put the shell scripts together as a \texttt{.zip} file or as a
tarball.  Do not submit \texttt{.rar} archives.

\paragraph{Add your email address}

Put your email address in a comment on the second line of your
submissions, so that I can return them marked to you. I will not
return assignments that do not have an email address on the second
line of each file.


\paragraph{Weighting:}

This assignment shall provide 25\% of your Continuous Assessment mark.

\paragraph{Marking Criteria:}

Originality is essential.  Efficient implementations get more marks.
Elegant and well-structured solutions get more marks.  Flexible
solutions get more marks (i.e., offer more useful options to the
user).

%% Original submissions that answer the first four of these questions
%% will receive a bonus of 5\% if they are submitted one week before the
%% deadline.


\paragraph{Cheating:}

Your work \emph{must} be original.  Copying will be \emph{severely}
dealt with.


\section{Background}
\label{sec:background}

\subsection{Example Programs and Online Resources}
\label{sec:examples}

I have written a number of example shell scripts---please download
them, run them, examine them, understand them.  Get them from
\url{http://nicku.org/ossi/lectures/shell/bin/}.

Please also remember that there are hundreds of excellent examples in
the freely downloadable book \emph{Advanced Bash Scripting Guide} at
\url{http://tldp.org/LDP/abs/html/index.html}.

There is also an excellent book about the \texttt{awk} programming
language, available from \url{http://www.ssc.com/ssc/eap/}.

Note: I have put one copy of the book, \emph{Learning the Bash Shell},
2nd edition, by Cameron Newham and Bill Rosenblat, in the library open
reserve.  Another copy is on the shelves for loan.  I have also
purchased some other books on shell programming for the library.  One
book you may find helpful is \emph{UNIX Power Tools}, 3rd edition.

\section{Requirements}

A number of shell scripts are required.  A number of simple programs
are required that exercise techniques that you will use in later
programs.  A number of these programs require commands that you may
not have used before.  Use the \texttt{man} command to investigate how
to use them.

\begin{enumerate}
\item Use the command \texttt{basename} and \emph{command
    substitution} to write a simple program that prints its own name.
  This should still work, even if the program is renamed.  Here is an
  example of running it:
  \par\vspace*{-1.5ex}\par
\begin{alltt}
$ \textbf{./myname.sh}
This is myname.sh
$ \textbf{mv myname.sh what-is-my-name}
$ \textbf{./what-is-my-name}
This is what-is-my-name
\end{alltt}%$

\item Use the \texttt{id} program to determine the \emph{effective
    user ID number} of the user that runs the program.  Print the
  message ``I am root'' if the user \ID number is 0; print a different
  message otherwise.
  \begin{explanation}
    Note: you should check the user \ID number, not the user \ID name.
  \end{explanation}
  
\item \label{que:lockfile1}Write a script that prints an error message
  and exits if a \emph{lockfile} exists.  A \emph{lockfile} is a file
  in the \texttt{/tmp} directory, with a name equal to the basename of
  the script, followed by the string, ``\texttt{.lock}'' \ For
  example, if the name of the script is \texttt{lockit}, then the name
  of the lockfile shall be \path{/tmp/lockit.lock}
  
\item \label{que:lockfile2}Modify your answer to
  question~\ref{que:lockfile1} so that the lockfile will be
  \texttt{/var/\allowbreak{}lock/\allowbreak{}subsys/\allowbreak
    \meta{basename of script}.lock} if the user executing it is root,
  \texttt{/tmp/\meta{basename of script}.lock} otherwise.
  
\item \label{que:lockfile3}Modify your answer to
  question~\ref{que:lockfile2} so that:
  \begin{itemize}
  \item if the lockfile exists, the program will print an error
    message and terminate;
  \item if the lockfile doesn't exist, then the program creates it,
    and makes sure that the lockfile was successfully created;
  \item when the program exits normally, it will remove the lockfile.
  \end{itemize}
  
\item \label{que:trapall}Download the script
  \url{http://nicku.org/ossi/lectures/shell/bin/trapall}.
  Make it executable and run it.  Send the executing process all
  signals in the range 1\ldots31 as described in the existing comments
  in the file.  Modify it so that it will still print the message, but
  it will exit when it receives any
  signal.  In the modified program, add comments that:
  \begin{itemize}
  \item explain how you could terminate the original program;
  \item list all signals in the range 1\ldots31 that the original
    program could not trap;
  \item explain \emph{why} the signals you list cannot be trapped by
    the original program.
  \end{itemize}

\clearpage
\item  \label{que:lockfile4}Modify your answer to
  question~\ref{que:lockfile3} so that if the program receives any of
  the following signals: \texttt{INT}, \texttt{TERM}, \texttt{HUP} or
  \texttt{QUIT}, the program will do all of the following:
  \begin{itemize}
  \item print a message;
  \item remove the lockfile;
  \item exit with a non-zero exit status.
  \end{itemize}

\item Configure your system with \texttt{keychain}, as described at
  the web site: \url{http://www.gentoo.org/proj/en/keychain.xml}.
  Does this work for you?  Write a text file
  \texttt{keychain-setup.txt} which lists the steps you used to set up
  \texttt{keychain} in 30 lines or less.  Use your own words.
  
\item \label{que:rsync-1}Modify your answer to
  question~\ref{que:lockfile4} so that your program will:
  \begin{itemize}
  \item \texttt{source} the \texttt{keychain} information about where
    your agent is;
  \item use the \texttt{rsync} program to copy the contents of the
    directory \texttt{\(\sim\)/public\_html} to the directory
    \texttt{\(\sim\)/public\_html-backup} in your own account on
    another computer.
  \end{itemize}
  For example, if your local account contains the file
  \texttt{$\sim$/public\_html/\allowbreak{}important.html}, then the
  file
  \texttt{$\sim$/\allowbreak{}public\_html-backup/\allowbreak{}important.html}
  will be created on the remote computer if it does not exist, and the
  content will be identical on both machines.

\item \label{que:rsync-2}Modify your answer to
  question~\ref{que:rsync-1} so that your program will take a list of
  names in the form \texttt{\meta{computer}:\meta{destination}} on the
  command line, and will make each location contain a copy of
  \texttt{$\sim$/public\_html} on the local machine.

  For example, if the program is called \texttt{rsync-to-websites},
  then if it is executed like this:
\begin{alltt}\small
$ \textbf{rsync-to-websites ictlab:/var/www/html/mystuff \bs
  172.19.64.50:/var/www/backup 172.19.64.51:/var/www/html}
\end{alltt}%$
  and if the local machine has a file
  \texttt{$\sim$/public\_html/important.html}, then on the computer
  \texttt{ictlab}, a file
  \path{/var/www/html/mystuff/important.html} will be created, and
  on the computer \texttt{172.19.64.50}, the file
  \path{/var/www/backup/important.html} will be created, and on the
  computer \texttt{172.19.64.51}, the file
  \path{/var/www/html/important.html} will be created.
  
\item Write a text file \texttt{problems-with-locking.txt} in which
  you answer the following question: What are the limitations of the
  file locking system that you have implemented in
  question~\ref{que:rsync-2}?  Explain how it can it go wrong.  What is
  the technical term for this limitation?
  
\item \label{que:last-question}\label{que:crontab-setup}Write a text
  file \texttt{crontab-setup.txt} in which you explain how you can set
  up the \texttt{cron} program to execute the program you wrote as
  your answer to question~\vref{que:rsync-2} every morning at
  3.24\,am.  It should be executed with a command line shown in the
  previous question.
  \begin{explanation}
    See \texttt{man 5 crontab} and \texttt{man crontab}.
  \end{explanation}
\end{enumerate}

\section{Notes}
\label{sec:notes}

\begin{itemize}
\item You need to submit \ref{que:last-question} text files, most of
  which contain a shell script, but some of which contain plain text
  answering the question.
\item One file, your answer to question~\ref{que:trapall}, will be a
  shell script that contains some text answering the questions.  That
  file will still be executable as a shell script; the text you write
  in that file should be comments; each line of text that is not the
  actual program will begin with the comment character, `\texttt{\#}'.
\item The twelve files should be zipped or tarred together into one
  compressed file, and be submitted online as explained on
  page~\pageref{pag:submission}.
\item Your program that answers question~\vref{que:rsync-2} should
  provide the features required for questions~\ref{que:lockfile4} and
  \ref{que:lockfile2}, and should use \texttt{keychain} to access your
  \SSH agent.
\item Your programs should all provide proper handling of error
  conditions and should all print a suitable error message and exit if
  some error condition prevents the program completing normal
  execution.
\item You may assume that the programs execute on a system
  that complies with the current \POSIX standards.
\item You may ask any questions by email, to the address \texttt{Nick
    Urbanik <nicku@nicku.org>}.  The subject line should contain the
  text, ``\texttt{OSSI ASSIGNMENT}''.  If I answer your question, I
  will send the answer to \emph{all} students, but I will not reveal the
  identity of the person who asked the question.
\item Although I am giving you a long time to hand in this assignment,
  I strongly advise you to begin it immediately.  I am not responsible
  for those who leave it to the deadline and who then disturb their
  own preparation for the examinations.
\end{itemize}
\end{document}
linus
dad
daddy
mummy
happy EASTER
7gffgjdgfydgffgfg
gkggrfufgfgyfyvfygfhffg 70vhhhhhjkjhhjghjhbjbjbbkbjkgjhfg 
gfyggufgvhfvkjhfgvhvgg80rgyfguhygfughjghyfghjggfhfmjhbvjhjhghfhv
jhvhfjhjfhghjfjhghvgdfh90hjfhgjhvghjhjvhvghvgvhchjvhjvjhhjbvjhvhvhjjhvbjvjhvjhvhvgghjgvhvhgchvvbvvbbcvh457ghgjhgfjbgjhbjfhgbjdfghfhhgjgfhhggghghfhhfghg
ggfghjghjgjhghkffuhgghghgh5478hhhhhhghhbvgjbjhfjhfjhfjkhbjbvhjhvbcjhvjh
hfighjkfffffffffffffffffffffffh357jkbhgjbhkjghfgkibuhgjhgijgihghghgjfhf
jhhjhjghjkghkjhjjhjhkljjjjjlkjkljglhkjklghj870hjguyuhghjhfjhjhfghgngjhjjhf
