The Linux Programmer's Guide
Total Page:16
File Type:pdf, Size:1020Kb
The Linux Programmer’s Guide Sven Goldt Sven van der Meer Scott Burkett Matt Welsh Version 0.4 March 1995 0...Our continuing mission: to seek out knowledge of C, to explore strange unix commands, and to boldly code where no one has man page 4. 2 Contents 1 The Linux operating system 7 2 The Linux kernel 9 3 The Linux libc package 11 4 System calls 13 5 The “swiss army knife” ioctl 15 6 Linux Interprocess Communications 17 6.1 Introduction .................................. 17 6.2 Half-duplex UNIX Pipes ........................... 17 6.2.1 Basic Concepts ............................ 17 6.2.2 Creating Pipes in C .......................... 19 6.2.3 Pipes the Easy Way! ......................... 23 6.2.4 Atomic Operations with Pipes .................... 27 6.2.5 Notes on half-duplex pipes: ..................... 27 6.3 Named Pipes (FIFOs - First In First Out) ................... 27 6.3.1 Basic Concepts ............................ 27 6.3.2 Creating a FIFO ........................... 27 6.3.3 FIFO Operations ........................... 28 6.3.4 Blocking Actions on a FIFO ..................... 30 6.3.5 The Infamous SIGPIPE Signal .................... 30 6.4 System V IPC ................................. 30 6.4.1 Fundamental Concepts ........................ 30 6.4.2 Message Queues ........................... 32 6.4.3 Semaphores .............................. 46 6.4.4 Shared Memory ............................ 62 7 Sound Programming 69 7.1 Programming the internal speaker ...................... 69 7.2 Programming a sound card .......................... 69 8 Character Cell Graphics 71 8.1 I/O Function in libc .............................. 72 8.1.1 Formatted Output ........................... 72 8.1.2 Formatted Input ............................ 73 8.2 The Termcap Library ............................. 74 8.2.1 Introduction .............................. 74 8.2.2 Find a Terminal Description ..................... 75 8.2.3 Look at a Terminal Description ................... 75 8.2.4 Termcap Capabilities ......................... 76 3 4 CONTENTS 8.3 Ncurses - Introduction ............................ 80 8.4 Initializing ................................... 82 8.5 Windows ................................... 82 8.6 Output ..................................... 85 8.6.1 Formatted Output ........................... 86 8.6.2 Insert Characters/Lines ........................ 86 8.6.3 Delete Characters/Lines ....................... 86 8.6.4 Boxes and Lines ........................... 87 8.6.5 Background Character ........................ 88 8.7 Input ...................................... 88 8.7.1 Formated Input ............................ 89 8.8 Options .................................... 89 8.8.1 Input Options ............................. 90 8.8.2 Terminal Attributes .......................... 91 8.8.3 Use Options .............................. 92 8.9 Clear Window and Lines ........................... 93 8.10 Updating the Terminal ............................ 94 8.11 Video Attributes and Color .......................... 95 8.12 Cursor and Window Coordinates ....................... 98 8.13 Scrolling .................................... 99 8.14 Pads ...................................... 100 8.15 Soft-labels ................................... 101 8.16 Miscellaneous ................................. 101 8.17 Low-level Access ............................... 102 8.18 Screen Dump ................................. 102 8.19 Termcap Emulation .............................. 102 8.20 Terminfo Functions .............................. 103 8.21 Debug Function ................................ 104 8.22 Terminfo Capabilities ............................. 104 8.22.1 Boolean Capabilities ......................... 104 8.22.2 Numbers ............................... 105 8.22.3 Strings ................................ 105 8.23 [N]Curses Function Overview ........................ 112 9 Programming I/O ports 115 9.1 Mouse Programming ............................. 116 9.2 Modem Programming ............................. 117 9.3 Printer Programming ............................. 117 9.4 Joystick Programming ............................ 117 10 Porting Applications to Linux 119 10.1 Introduction .................................. 119 10.2 Signal handling ................................ 119 10.2.1 Signals under SVR4, BSD, and POSIX.1 .............. 120 10.2.2 Linux signal options ......................... 120 10.2.3 signal under Linux .......................... 121 10.2.4 Signals supported by Linux ..................... 121 10.3 Terminal I/O .................................. 121 10.4 Process information and control ....................... 122 10.4.1 kvm routines ............................. 122 10.4.2 ptrace and the /proc filesystem .................... 122 10.4.3 Process control under Linux ..................... 122 10.5 Portable conditional compilation ....................... 123 10.6 Additional Comments ............................. 124 CONTENTS 5 11 Systemcalls in alphabetical order 125 12 Abbreviations 131 Copyright • The Linux Programmer’s Guide is c 1994, 1995 by Sven Goldt Sven Goldt, Sachsendamm 47b, 10829 Berlin, Germany < [email protected] berlin.de > . Chapter 8 is c 1994,− 1995 by Sven van der Meer < [email protected] berlin.de > . Chapter 6 is c 1995 Scott Burkett < [email protected] > . − Chapter 10 is c 1994, 1995 Matt Welsh < [email protected] > . Special thanks goes to John D. Harper < [email protected] > for proofreading this guide. Permission to reproduce this document in whole or in part is subject to the following conditions: 1. The copyright notice remains intact and is included. 2. If you make money with it the authors want a share. 3. The authors are not responsible for any harm that might arise by the use of it. Preface • This guide is far from being complete. The first release started at version 0.1 in September 1994. It concentrated on system calls because of lack of manpower and information. Planned are the description of library functions and major kernel changes as well as excursions into important areas like networking, sound, graphics and asynchronous I/O. Maybe some hints about how to build shared libraries and pointers to useful toolkits will later be included. This guide will only be a success with generous help in the form of information or perhaps even submission of whole chapters. Introduction • Once upon a time I installed Linux on my PC to learn more about system administra- tion. I tried to install a slip server but it didn’t work with shadow and mgetty. I had to patch sliplogin and it worked until the new Linux 1.1 releases. No one could tell me what had happened. There was no documentation about changes since the 0.99 kernel except the kernel change summaries from Russ Nelson, but they didn’t help me very much in solving problems. The Linux Programmer’s Guide is meant to do what the name implies— It is to help Linux programmers understand the peculiarities of Linux. By its nature, this also means that it should be useful when porting programs from other operating systems to Linux. Therefore, this guide must describe the system calls and the major kernel changes which have effects on older programs like serial I/O and networking. Sven Goldt The Linux Programmer’s Guide 6 Chapter 1 The Linux operating system In March 1991 Linus Benedict Torvalds bought the multitasking system Minix for his AT 386. He used it to develop his own multitasking system which he called Linux. In Septem- ber 1991 he released the first prototype by e-mail to some other Minix users on the internet, thus beginning the Linux project. Many programmers from that point on have supported Linux. They have added device drivers, developed applications, and aimed for POSIX compliance. Today Linux is very powerful, but what is best is that it’s free. Work is beeing done to port Linux to other platforms. 7 8 CHAPTER 1. THE LINUX OPERATING SYSTEM Chapter 2 The Linux kernel The base of Linux is the kernel. You could replace each and every library, but as long as the Linux kernel remained, it would still be Linux. The kernel contains device drivers, memory management, process management and communication management. The kernel hacker gurus follow POSIX guidelines which sometimes makes programming easier and sometimes harder. If your program behaves differently on a new Linux kernel release, chances are that a new POSIX guideline has been implemented. For programming infor- mation about the Linux kernel, read the Linux Kernel Hacker’s Guide. 9 10 CHAPTER 2. THE LINUX KERNEL Chapter 3 The Linux libc package libc: ISO 8859.1, < linux/param.h >, YP functions, crypt functions, some basic shadow routines (by default not included), ... old routines for compatibility in libcompat (by default not activated), english, french or german error messages, bsd 4.4lite compatible screen handling routines in libcurses, bsd compatible routines in libbsd, screen handling routines in libtermcap, database management routines in libdbm, mathematic routines in libm, entry to execute programs in crt0.o ???, byte sex information in libieee ??? (could someone give some infos instead of laughing ?), user space profiling in libgmon. I wish someone of the Linux libc developers would write this chapter. All i can say now that there is going to be a change from the a.out executable format to the elf (executable and linkable format) which also means a change in building shared libraries. Currently both formats (a.out and elf) are supported. Most parts of the Linux libc package are under the Library GNU Public