Advanced Linux Programming - GitHub Pages

2y ago
140 Views
3 Downloads
1.21 MB
24 Pages
Last View : 2m ago
Last Download : 2m ago
Upload by : Maxton Kershaw
Transcription

00 0430 FM5/22/012:32 PMPage iContents At a GlanceI Advanced UNIX Programmingwith LinuxAdvancedLinuxProgramming1 Getting Started32 Writing Good GNU/LinuxSoftware 173 Processes 454 Threads 615 Interprocess Communication 95II Mastering Linux6 Devices 1297 The /proc File System1478 Linux System Calls 1679 Inline Assembly Code 18910 Security 19711 A Sample GNU/LinuxApplication 219III AppendixesA Other Development Tools 259B Low-Level I/O 281C Table of Signals 301D Online Resources 303E Open Publication LicenseVersion 1.0 305F GNU General Public License 309

00 0430 FM5/22/012:32 PMPage ii

00 0430 FM5/22/012:32 PMPage iiiAdvanced LinuxProgrammingMark Mitchell, Jeffrey Oldham,and Alex Samuelwww.newriders.com201 West 103rd Street, Indianapolis, Indiana 46290An Imprint of Pearson EducationBoston Indianapolis London Munich New York San Francisco

00 0430 FM5/22/012:32 PMPage ivAdvanced Linux ProgrammingCopyright 2001 by New Riders PublishingFIRST EDITION: June, 2001All rights reserved. No part of this book may be reproducedor transmitted in any form or by any means, electronic ormechanical, including photocopying, recording, or by anyinformation storage and retrieval system, without writtenpermission from the publisher, except for the inclusion ofbrief quotations in a review.International Standard Book Number: 0-7357-1043-0Library of Congress Catalog Card Number: 00-10534305 04 03 02 01 7 6 5 4 3 2 1Interpretation of the printing code:The rightmost doubledigit number is the year of the book’s printing; the rightmost single-digit number is the number of the book’sprinting. For example, the printing code 01-1 shows that thefirst printing of the book occurred in 2001.Composed in Bembo and MCPdigital by New RidersPublishing.Printed in the United States of America.TrademarksAll terms mentioned in this book that are known to betrademarks or service marks have been appropriately capitalized. New Riders Publishing cannot attest to the accuracy ofthis information. Use of a term in this book should not beregarded as affecting the validity of any trademark or servicemark.PostScript is a trademark of Adobe Systems, Inc.Linux is a trademark of Linus Torvalds.Warning and DisclaimerThis book is designed to provide information aboutAdvanced Linux Programming. Every effort has been made tomake this book as complete and as accurate as possible, butno warranty or fitness is implied.The information is provided on an as-is basis.The authorsand New Riders Publishing shall have neither liability norresponsibility to any person or entity with respect to any lossor damages arising from the information contained in thisbook or from the use of the discs or programs that mayaccompany it.PublisherDavid DwyerAssociate PublisherAl ValvanoExecutive EditorStephanie WallManaging EditorGina BrownAcquisitions EditorAnn QuinnDevelopment EditorLaura LoveallProduct MarketingManagerStephanie LaytonPublicity ManagerSusan PetroProject EditorCaroline WiseCopy EditorKrista HansingSenior IndexerCheryl LenserManufacturingCoordinatorJim ConwayBook DesignerLouisa KlucznikCover DesignerBrainstorm Design, Inc.Cover ProductionAren HowellProofreaderDebra NeelCompositionAmy Parker

00 0430 FM5/22/012:32 PMPage v

00 0430 FM5/22/012:32 PMPage viTable of ContentsI Advanced UNIX Programmingwith Linux 11 Getting Started31.1 Editing with Emacs 41.2 Compiling with GCC 61.3 Automating the Process with GNUMake 91.4 Debugging with GNU Debugger(GDB) 111.5 Finding More Information 132 Writing Good GNU/LinuxSoftware 172.1 Interaction With the ExecutionEnvironment 172.2 Coding Defensively 302.3 Writing and Using Libraries 363 Processes3.13.23.33.44 Threads4.14.24.34.44.54.645Looking at Processes 45Creating Processes 48Signals 52Process Termination 5561Thread Creation 62Thread Cancellation 69Thread-Specific Data 72Synchronization and Critical Sections 77GNU/Linux Thread Implementation 92Processes Vs.Threads 94

00 0430 FM5/22/012:32 PMPage viiContents5 Interprocess Communication5.15.25.35.45.595Shared Memory 96Processes Semaphores 101Mapped Memory 105Pipes 110Sockets 116II Mastering Linux 1276 Devices6.16.26.36.46.56.66.7129Device Types 130Device Numbers 130Device Entries 131Hardware Devices 133Special Devices 136PTYs 142ioctl 1447 The /proc File System7.17.27.37.47.57.6147Extracting Information from /proc 148Process Entries 150Hardware Information 158Kernel Information 160Drives, Mounts, and File Systems 161System Statistics 1658 Linux System Calls1678.1 Using strace 1688.2 access:Testing File Permissions 1698.3 fcntl: Locks and Other FileOperations 1718.4 fsync and fdatasync: Flushing DiskBuffers 1738.5 getrlimit and setrlimit: ResourceLimits 1748.6 getrusage: Process Statistics 1758.7 gettimeofday:Wall-Clock Time 176vii

00 0430 FM5/22/01viiiContents2:32 PMPage viii8.8 The mlock Family: Locking PhysicalMemory 1778.9 mprotect: Setting MemoryPermissions 1798.10 nanosleep: High-Precision Sleeping 1818.11 readlink: Reading Symbolic Links 1828.12 sendfile: Fast Data Transfers 1838.13 setitimer: Setting Interval Timers 1858.14 sysinfo: Obtaining System Statistics 1868.15 uname 1879 Inline Assembly Code9.19.29.39.49.59.6189When to Use Assembly Code 190Simple Inline Assembly 191Extended Assembly Syntax 192Example 194Optimization Issues 196Maintenance and Portability Issues 19610 Security19710.1 Users and Groups 19810.2 Process User IDs and ProcessGroup IDs 19910.3 File System Permissions 20010.4 Real and Effective IDs 20510.5 Authenticating Users 20810.6 More Security Holes 21111 A Sample GNU/LinuxApplication 21911.111.211.311.411.5Overview 219Implementation 221Modules 239Using the Server 252Finishing Up 255

00 0430 FM5/22/013:18 PMPage ixContentsIII Appendixes257A Other Development Tools259A.1 Static Program Analysis 259A.2 Finding Dynamic Memory Errors 261A.3 Profiling 269B Low-Level I/O281B.1B.2B.3B.4Reading and Writing Data 282stat 291Vector Reads and Writes 293Relation to Standard C Library I/OFunctions 295B.5 Other File Operations 296B.6 Reading Directory Contents 296C Table of SignalsD Online Resources301303D.1 General Information 303D.2 Information About GNU/LinuxSoftware 304D.3 Other Sites 304E Open Publication LicenseVersion 1.0 305I. Requirement on Both Unmodified andModified Versions 305II. Copyright 306III. Scope of License 306IV. Requirements on Modified Works 306V. Good-Practice Recommendations 306VI. License Options 307Open Publication Policy Appendix 307ix

00 0430 FMx5/22/012:32 PMPage xContentsF GNU General Public License309Preamble 309Terms and Conditions for Copying,Distribution and Modification 310End of Terms and Conditions 315How to Apply These Terms to Your NewPrograms 315Index317

00 0430 FM5/22/012:32 PMPage xi

00 0430 FM5/22/012:32 PMPage xiiTable of Program Listings1.11.21.32.12.22.3main.c (C source file), 6reciprocal.cpp (C source file), 6reciprocal.hpp (header file), 7arglist.c (argc and argv parameters), 18getopt long.c (getopt long function), 21print env.c (printing executionenvironment), 262.4 client.c (network client program), 262.5 temp file.c (mkstemp function), 282.6 readfile.c (resource allocation during errorchecking), 352.7 test.c (library contents), 372.8 app.c (program with library functions), 372.9 tifftest.c (libtiff library), 403.1 print-pid.c (printing process IDs), 463.2 system.c (system function), 483.3 fork.c (fork function), 493.4 fork-exec.c (fork and exec functions), 513.5 sigusr1.c (signal handlers), 543.6 zombie.c (zombie processes), 583.7 sigchld.c (cleaning up child processes), 604.1 thread-create.c (creating threads), 634.2 thread-create2 (creating two threads),644.3 thread-create2.c (revised main function), 654.4 primes.c (prime number computation in athread), 674.5 detached.c (creating detached threads), 694.6 critical-section.c (critical sections), 714.7 tsd.c (thread-specific data), 734.8 cleanup.c (cleanup handlers), 754.9 cxx-exit.cpp (C thread cleanup), 764.10 job-queue1.c (thread race conditions), 784.11 job-queue2.c (mutexes), 804.12 job-queue3.c (semaphores), 844.13 spin-condvar.c (condition variables), 87

00 0430 FM5/22/012:32 PMPage xiiiProgram Listings4.14 condvar.c (condition variables), 904.15 thread-pid (printing thread process IDs), 925.1 shm.c (shared memory), 995.2 sem all deall.c (semaphore allocation anddeallocation), 1025.3 sem init.c (semaphore initialization), 1025.4 sem pv.c (semaphore wait and postoperations), 1045.5 mmap-write.c (mapped memory), 1065.6 mmap-read.c (mapped memory), 1075.7 pipe.c (parent-child processcommunication), 1115.8 dup2.c (output redirection), 1135.9 popen.c (popen command), 1145.10 socket-server.c (local sockets), 1205.11 socket-client.c (local sockets), 1215.12 socket-inet.c (Internet-domain sockets), 1246.1 random number.c (random numbergeneration), 1386.2 cdrom-eject.c (ioctl example), 1447.1 clock-speed.c (cpu clock speed from/proc/cpuinfo), 1497.2 get-pid.c (process ID from /proc/self),1517.3 print-arg-list.c (printing process argumentlists), 1537.4 print-environment.c (process environment),1547.5 get-exe-path.c (program executable path), 1557.6 open-and-spin.c (opening files), 1577.7 print-uptime.c (system uptime and idle time),1658.1 check-access.c (file access permissions), 1708.2 lock-file.c (write locks), 1718.3 write journal entry.c (data bufferflushing), 1738.4 limit-cpu.c (resource limits), 1758.5 print-cpu-times.c (process statistics), 176xiii

00 0430 FMxiv5/22/012:32 PMPage xivProgram Listings8.6 print-time.c (date/time printing), 1778.7 mprotect.c (memory access), 1808.8 better sleep.c (high-precision sleep), 1828.9 print-symlink.c (symbolic links), 1838.10 copy.c (sendfile system call), 1848.11 itemer.c (interal timers), 1858.12 sysinfo.c (system statistics), 1878.13 print-uname (version number andhardware information), 1889.1 bit-pos-loop.c (bit position with loop),1949.2 bit-pos-asm.c (bit position with bsrl), 19510.1 simpleid.c (printing user andgroup IDs), 20010.2 stat-perm.c (viewing file permissions withstat system call), 20210.3 setuid-test.c (setuid programs), 20710.4 pam.c (PAM example), 20910.5 temp-file.c (temporary file creation),21410.6 grep-dictionary.c (word search), 21611.1 server.h (function and variabledeclarations), 22211.2 common.c (utility functions), 22311.3 module.c (loading server modules),22611.4 server.c (server implementation), 22811.5 main.c (main server program), 23511.6 time.c (show wall-clock time), 23911.7 issue.c (GNU/Linux distributioninformation), 24011.8 diskfree.c (free disk space information), 24211.9 processes.c (summarizing runningprocesses), 24411.10 Makefile (Makefile for sample applicationprogram), 252

00 0430 FM5/22/012:32 PMPage xvProgram ListingsA.1 hello.c (Hello World), 260A.2 malloc-use.c (dynamic memory allocation),267A.3 calculator.c (main calculator program),274A.4 number.c (unary number implementation),276A.5 stack.c (unary number stack), 279A.6 definitions.h (header file for calculatorprogram), 280B.1 create-file.c (create a new file), 284B.2 timestamp.c (append a timestamp), 285B.3 write-all.c (write all buffered data), 286B.4 hexdump.c (print a hexadecimal file dump),287B.5 lseek-huge.c (creating large files), 289B.6 read-file.c (reading files into buffers),292B.7 write-args.c (writev function), 294B.8 listdir.c (printing directory listings), 297xv

00 0430 FM5/22/012:32 PMPage xviAbout the AuthorsMark Mitchell received a bachelor of arts degree in computerscience from Harvard in 1994 and a master of science degree fromStanford in 1999. His research interests centered on computationalcomplexity and computer security. Mark has participated substantiallyin the development of the GNU Compiler Collection, and he has astrong interest in developing quality software.Jeffrey Oldham received a bachelor of arts degree in computerscience from Rice University in 1991. After working at the Center forResearch on Parallel Computation, he obtained a doctor of philosophy degree from Stanford in 2000. His research interests center onalgorithm engineering, concentrating on flow and other combinatorial algorithms. He works on GCC and scientific computing software.Alex Samuel graduated from Harvard in 1995 with a degree inphysics. He worked as a software engineer at BBN before returningto study physics at Caltech and the Stanford Linear AcceleratorCenter. Alex administers the Software Carpentry project and workson various other projects, such as optimizations in GCC.Mark and Alex founded CodeSourcery LLC together in 1999.Jeffrey joined the company in 2000. CodeSourcery’s mission is toprovide development tools for GNU/Linux and other operatingsystems; to make the GNU tool chain a commercial-quality,standards-conforming development tool set; and to provide generalconsulting and engineering services. CodeSourcery’s Web site ishttp://www.codesourcery.com.xvi

00 0430 FM5/22/012:32 PMPage xviiAbout the Technical ReviewersThese reviewers contributed their considerable hands-on expertise to the entire development process for Advanced Linux Programming. As the book was being written, thesededicated professionals reviewed all the material for technical content, organization,and flow.Their feedback was critical to ensuring that Advanced Linux Programming fitsour reader’s need for the highest quality technical information.Glenn Becker has many degrees, all in theatre. He presently works as anonline producer for SCIFI.COM, the online component of the SCI FIchannel, in New York City. At home he runs Debian GNU/Linux andobsesses about such topics as system administration, security, softwareinternationalization, and XML.John Dean received a BSc(Hons) from the University of Sheffield in1974, in pure science. As an undergraduate at Sheffield, John developedhis interest in computing. In 1986 he received a MSc from CranfieldInstitute of Science and Technology in Control Engineering.While working for Roll Royce and Associates, John became involved in developingcontrol software for computer-aided inspection equipment of nuclearsteam-raising plants. Since leaving RR&A in 1978, he has worked in thepetrochemical industry developing and maintaining process control software. John worked a volunteer software developer for MySQL from 1996until May 2000, when he joined MySQL as a full-time employee. John’s area ofresponsibility is MySQL on MS Windows and developing a new MySQL GUIclient using Trolltech’s Qt GUI application toolkit on both Windows andplatforms that run X-11.xvii

00 0430 FM5/22/012:32 PMPage xviiiAcknowledgmentsWe greatly appreciate the pioneering work of Richard Stallman, without whomthere would never have been the GNU Project, and of Linus Torvalds, withoutwhom there would never have been the Linux kernel. Countless others have workedon parts of the GNU/Linux operating system, and we thank them all.We thank the faculties of Harvard and Rice for our undergraduate educations, andCaltech and Stanford for our graduate training. Without all who taught us, we wouldnever have dared to teach others!W. Richard Stevens wrote three excellent books on UNIX programming, and we haveconsulted them extensively. Roland McGrath, Ulrich Drepper, and many others wrotethe GNU C library and its outstanding documentation.Robert Brazile and Sam Kendall reviewed early outlines of this book and made wonderful suggestions about tone and content. Our technical editors and reviewers (especially Glenn Becker and John Dean) pointed out errors, made suggestions, and providedcontinuous encouragement. Of course, any errors that remain are no fault of theirs!Thanks to Ann Quinn, of New Riders, for handling all the details involved in publishing a book; Laura Loveall, also of New Riders, for not letting us fall too far behind onour deadlines; and Stephanie Wall, also of New Riders, for encouraging us to writethis book in the first place!xviii

00 0430 FM5/22/012:32 PMPage xixTell Us What You ThinkAs the reader of this book, you are the most important critic and commentator.Wevalue your opinion and want to know what we’re doing right, what we could do better, what areas you’d like to see us publish in, and any other words of wisdom you’rewilling to pass our way.As the Executive Editor for the Web Development team at New Riders Publishing, Iwelcome your comments.You can fax, email, or write me directly to let me knowwhat you did or didn’t like about this book—as well as what we can do to make ourbooks stronger.Please note that I cannot help you with technical problems related to the topic of this book, andthat due to the high volume of mail I receive, I might not be able to reply to every message.When you write, please be sure to include this book’s title and author, as well as yourname and phone or fax number. I will carefully review your comments and sharethem with the author and editors who worked on the s.comMail:Stephanie WallExecutive EditorNew Riders Publishing201 West 103rd StreetIndianapolis, IN 46290 USAxix

00 0430 FM5/22/012:32 PMPage xxIntroductionGNU/Linux has taken the world of computers by storm. At one time, personal computer users were forced to choose among proprietary operating environments andapplications. Users had no way of fixing or improving these programs, could not look“under the hood,” and were often forced to accept restrictive licenses. GNU/Linuxand other open source systems have changed that—now PC users, administrators, anddevelopers can choose a free operating environment complete with tools, applications,and full source code.A great deal of the success of GNU/Linux is owed to its open source nature.Because the source code for programs is publicly available, everyone can take part indevelopment, whether by fixing a small bug or by developing and distributing a complete major application.This opportunity has enticed thousands of capable developersworldwide to contribute new components and improvements to GNU/Linux, to thepoint that modern GNU/Linux systems rival the features of any proprietary system,and distributions include thousands of programs and applications spanning many CDROMs or DVDs.The success of GNU/Linux has also validated much of the UNIX philosophy.Many of the application programming interfaces (APIs) introduced in AT&T and BSDUNIX variants survive in Linux and form the foundation on which programs arebuilt.The UNIX philosophy of many small command line-oriented programs workingtogether is the organizational principle that makes GNU/Linux so powerful. Evenwhen these programs are wrapped in easy-to-use graphical user interfaces, the underlying commands are still available for power users and automated scripts.A powerful GNU/Linux application harnesses the power of these APIs and commands in its inner workings. GNU/Linux’s APIs provide access to sophisticated features such as interprocess communication, multithreading, and high-performancenetworking. And many problems can be solved simply by assembling existing commands and programs using simple scripts.GNU and LinuxWhere did the name GNU/Liux come from? You’ve certainly heard of Linux before,and you may have heard of the GNU Project.You may not have heard the nameGNU/Linux, although you’re probably familiar with the system it refers to.Linux is named after Linus Torvalds, the creator and original author of the kernelthat runs a GNU/Linux system.The kernel is the program that performs the mostbasic functions of an operating system: It controls and interfaces with the computer’shardware, handles allocation of memory and other resources, allows multiple programsto run at the same time, manages the file system, and so on.xx

00 0430 FM5/22/012:32 PMPage xxiThe kernel by itself doesn’t provide features that are useful to users. It can’t evenprovide a simple prompt for users to enter basic commands. It provides no way forusers to manage or edit files, communicate with other computers, or write other programs.These tasks require the use of a wid

Advanced Linux Programming Contents At a Glance I Advanced UNIX Programming with Linux 1 Getting Started 3 2 Writing Good GNU/Linux Software 17 3 Processes 45 4 Threads 61 5 Interprocess Communication95 II Mastering Linux 6 Devices 129 7 The /proc File System 147 8 Linux System Calls 167 9 Inline Assembly Code 189 10

Related Documents:

Linux in a Nutshell Linux Network Administrator’s Guide Linux Pocket Guide Linux Security Cookbook Linux Server Hacks Linux Server Security Running Linux SELinux Understanding Linux Network Internals Linux Books Resource Center linux.oreilly.comis a complete catalog of O’Reilly’s books on Linux and Unix and related technologies .

Advanced Linux Programming Contents At a Glance I Advanced UNIX Programming with Linux 1 Getting Started 3 2 Writing Good GNU/Linux Software 17 3 Processes 45 4 Threads 61 5 Interprocess Communication95 II Mastering Linux 6 Devices 129 7 The /proc File System 147 8 Linux System Calls 167 9 Inline Assembly Code 189 10 Security 197 11 A Sample GNU/Linux Application 219 III

Other Linux resources from O’Reilly Related titles Building Embedded Linux Systems Linux Device Drivers Linux in a Nutshell Linux Pocket Guide Running Linux Understanding Linux Network Internals Understanding the Linux Kernel Linux Books Resource Center linu

Perfection PC Perfection PC Inc. Philips Philips Electronics Planar Planar Systems Inc PLEXON Plexon, Inc. Pogo Linux Pogo Linux, Inc. Pogo Linux Altura M2 Pogo Linux, Inc. Pogo Linux Velocity -D50 Pogo Linux, Inc. Pogo Linux Verona 330 Pogo Linux, Inc. Pogo Linux Vor

Official Kali Linux Documentation This PDF has been autogenerated on docs.kali.org - Apr 7, 2013 00. Introduction to Kali Linux What is Kali Linux ? Kali Linux is an advanced Penetration Testing and Security Auditing Linux distribution. Kali Linux Features Kali is a complete re-build of BackTrack Linux, adhering completely to Debian development .

Yes. Oracle Autonomous Linux, which is based on Oracle Linux, is 100% application binary compatible with IBM's Red Hat Enterprise Linux. This means that applications certified to run on Red Hat Enterprise Linux can run on Oracle Autonomous Linux unmodified. Oracle Linux binaries are provided for patching and updating Red Hat Enterprise Linux

2 LXC DOCKER MICHAEL LESSARD A bit of history - Virtualization and containers Chroot (version 7 Unix, 1979) FreeBSD Jails (FreeBSD 4, 2000) Linux vserver (Linux, Oct 2001) Para-virtualization Xen (Linux, 2003) Solaris zones (Solaris 10, 2004) OpenVZ (Linux, 2005) Full virtualization KVM (Linux, 2007) Linux Containers - LXC (Linux 2.6.29 2009)

This booklet offers practice for BEC Higher exam with a strong tie-in to Market Leader Advanced. It provides Reading and Writing tasks that will help familiarise students with the different task types and give practice for the exam, as well as act as revision practice for non-exam students. Each unit has one Reading and one Writing task, which are closely aligned to the vocabulary, grammar and .