You are previewing Professional Linux® Programming.
O'Reilly logo
Professional Linux® Programming

Book Description

  • This book is broken into four primary sections addressing key topics that Linux programmers need to master: Linux nuts and bolts, the Linux kernel, the Linux desktop, and Linux for the Web

  • Effective examples help get readers up to speed with building software on a Linux-based system while using the tools and utilities that contribute to streamlining the software development process

  • Discusses using emulation and virtualization technologies for kernel development and application testing

  • Includes useful insights aimed at helping readers understand how their applications code fits in with the rest of the software stack

  • Examines cross-compilation, dynamic device insertion and removal, key Linux projects (such as Project Utopia), and the internationalization capabilities present in the GNOME desktop

Table of Contents

  1. Copyright
  2. About the Authors
  3. Credits
  4. Acknowledgments
  5. Introduction
    1. Who This Book Is For
    2. What This Book Covers
    3. How This Book Is Structured
    4. Conventions
    5. Source Code
    6. Errata
    7. p2p.wrox.com
  6. 1. Working with Linux
    1. 1.1. A Brief History of Linux
      1. 1.1.1. The GNU Project
      2. 1.1.2. The Linux Kernel
      3. 1.1.3. Linux Distributions
      4. 1.1.4. Free Software vs. Open Source
    2. 1.2. Beginning Development
      1. 1.2.1. Choosing a Linux Distribution
        1. 1.2.1.1. Linux Distributions from Red Hat
        2. 1.2.1.2. Linux Distributions from Novell
        3. 1.2.1.3. Debian and Ubuntu GNU/Linux
        4. 1.2.1.4. Classes of Linux Distribution
          1. 1.2.1.4.1. RPM based Distributions
          2. 1.2.1.4.2. Debian Derivatives
          3. 1.2.1.4.3. Source Distributions
          4. 1.2.1.4.4. Roll Your Own
      2. 1.2.2. Installing a Linux Distribution
        1. 1.2.2.1. Getting Hold of Linux
        2. 1.2.2.2. Determining Install-Time Package Selection
        3. 1.2.2.3. Setting Up Your Development Environment
          1. 1.2.2.3.1. Finding a Terminal
          2. 1.2.2.3.2. Editing Source Files
          3. 1.2.2.3.3. Using the Root Account
        4. 1.2.2.4. Development Releases
        5. 1.2.2.5. Scratch Boxes and Virtualization Technologies
          1. 1.2.2.5.1. VWware
          2. 1.2.2.5.2. Qemu
          3. 1.2.2.5.3. Xen
    3. 1.3. Linux Community
      1. 1.3.1. Linux User Groups
      2. 1.3.2. Mailing lists
      3. 1.3.3. IRC
      4. 1.3.4. Private Communities
    4. 1.4. Key Differences
      1. 1.4.1. Linux Is Modular
      2. 1.4.2. Linux Is Portable
      3. 1.4.3. Linux Is Generic
    5. 1.5. Summary
  7. 2. Toolchains
    1. 2.1. The Linux Development Process
      1. 2.1.1. Working with Sources
      2. 2.1.2. Configuring to the Local Environment
      3. 2.1.3. Building the Sources
    2. 2.2. Components of the GNU Toolchain
      1. 2.2.1. The GNU Compiler Collection
        1. 2.2.1.1. Compiling a Single Source File
        2. 2.2.1.2. Compiling Multiple Source Files
        3. 2.2.1.3. Using External Libraries
          1. 2.2.1.3.1. Shared vs. Static
          2. 2.2.1.3.2. An Example Shared Library
        4. 2.2.1.4. GCC options
          1. 2.2.1.4.1. General Options
          2. 2.2.1.4.2. Language Options
          3. 2.2.1.4.3. Warning Levels
          4. 2.2.1.4.4. Debugging
          5. 2.2.1.4.5. Optimization
          6. 2.2.1.4.6. Hardware Options
          7. 2.2.1.4.7. Further Documentation
    3. 2.3. The GNU binutils
      1. 2.3.1.
        1. 2.3.1.1. The GNU Assembler
        2. 2.3.1.2. The GNU Linker
          1. 2.3.1.2.1. Operation of the Linker
        3. 2.3.1.3. GNU objcopy and objdump
    4. 2.4. GNU Make
    5. 2.5. The GNU Debugger
    6. 2.6. The Linux Kernel and the GNU Toolchain
      1. 2.6.1. Inline Assembly
      2. 2.6.2. Attribute Tags
      3. 2.6.3. Custom Linker Scripts
    7. 2.7. Cross-Compilation
    8. 2.8. Building the GNU Toolchain
    9. 2.9. Summary
  8. 3. Portability
    1. 3.1. The Need for Portability
    2. 3.2. The Portability of Linux
      1. 3.2.1. Layers of Abstraction
      2. 3.2.2. Linux Distributions
        1. 3.2.2.1. Packages
        2. 3.2.2.2. The Linux Standard Base
          1. 3.2.2.2.1. LSB as a Panacea
        3. 3.2.2.3. The Role of the Linux vendor
          1. 3.2.2.3.1. A Word on Quality
        4. 3.2.2.4. RPM-Based Distributions
        5. 3.2.2.5. Debian Derivatives
        6. 3.2.2.6. Source Distributions
        7. 3.2.2.7. Rolling Your Own
      3. 3.2.3. Building Packages
        1. 3.2.3.1. Building RPM Packages
          1. 3.2.3.1.1. Writing Spec Files
          2. 3.2.3.1.2. Building an Example RPM
          3. 3.2.3.1.3. Patching Your Sources
          4. 3.2.3.1.4. Configuring the Build Environment
          5. 3.2.3.1.5. Kicking Off the Build
          6. 3.2.3.1.6. Installing RPMs on Non-RPM-Based Systems
        2. 3.2.3.2. Debian Packages
          1. 3.2.3.2.1. Debian Dependencies
          2. 3.2.3.2.2. Building Debian Packages
          3. 3.2.3.2.3. Debian Developers (DDs)
          4. 3.2.3.2.4. Further Information
      4. 3.2.4. Portable Source Code
        1. 3.2.4.1. GNU Autotools
          1. 3.2.4.1.1. Building Autotools Projects
        2. 3.2.4.2. GNU Autoconf
        3. 3.2.4.3. GNU Automake
        4. 3.2.4.4. GNU Libtool
      5. 3.2.5. Internationalization
        1. 3.2.5.1.
          1. 3.2.5.1.1. Languages and Locales
          2. 3.2.5.1.2. Working with Character Sets
          3. 3.2.5.1.3. Wide Characters
          4. 3.2.5.1.4. Speaking Your User's Language
          5. 3.2.5.1.5. Languages Other Than C/C++
    3. 3.3. Hardware Portability
      1. 3.3.1. 64-Bit Cleanliness
      2. 3.3.2. Endian Neutrality
        1. 3.3.2.1. Endian "Holy Wars"
      3. 3.3.3. Summary
  9. 4. Software Configuration Management
    1. 4.1. The Need for SCM
    2. 4.2. Centralized vs. Decentralized Development
    3. 4.3. Centralized Tools
      1. 4.3.1. The Concurrent Version System
        1. 4.3.1.1. Creating a CVS Repository
        2. 4.3.1.2. Creating a New CVS project
        3. 4.3.1.3. Checking Sources out of CVS
        4. 4.3.1.4. Checking in Modifications
        5. 4.3.1.5. Adding and Removing Files and Directories
        6. 4.3.1.6. Browsing CVS History
        7. 4.3.1.7. Conflict Resolution
        8. 4.3.1.8. Tagging and Branching
        9. 4.3.1.9. Distributed Development
          1. 4.3.1.9.1. CVS over an SSH Connection
          2. 4.3.1.9.2. CVS with a Remote pserver
        10. 4.3.1.10. Limitations
      2. 4.3.2. Subversion
        1. 4.3.2.1. Creating a Subversion Repository
        2. 4.3.2.2. Creating a New Subversion Project
        3. 4.3.2.3. Checking Sources out of Subversion
        4. 4.3.2.4. Checking in Modifications
          1. 4.3.2.4.1. Changesets
        5. 4.3.2.5. Distributed Development
    4. 4.4. Decentralized tools
      1. 4.4.1. Bazaar-NG
        1. 4.4.1.1. Configuring Bazaar
        2. 4.4.1.2. Creating a New Bazaar Branch
        3. 4.4.1.3. Checking in Modifications
        4. 4.4.1.4. Merging Bazaar Branches
      2. 4.4.2. Linux kernel SCM (git)
        1. 4.4.2.1. Everything Is an Object
        2. 4.4.2.2. Git vs. Cogito
        3. 4.4.2.3. Creating a New git Repository
        4. 4.4.2.4. Cloning an Existing git Repository
        5. 4.4.2.5. Managing Sources with git
        6. 4.4.2.6. Git as a Debugging Aid
    5. 4.5. Integrated SCM Tools
      1. 4.5.1. Eclipse
        1. 4.5.1.1. The CVS Repository Perspective
          1. 4.5.1.1.1. Placing a Project under CVS Control
          2. 4.5.1.1.2. Controlling a CVS Repository
    6. 4.6. Summary
  10. 5. Network Programming
    1. 5.1. Linux Socket Programming
      1. 5.1.1. Sockets
      2. 5.1.2. Network Addresses
      3. 5.1.3. Using Connection-Oriented Sockets
        1. 5.1.3.1. The Server Functions
        2. 5.1.3.2. The Client Functions
        3. 5.1.3.3. Closing the Connection
      4. 5.1.4. Using Connectionless Sockets
    2. 5.2. Moving Data
      1. 5.2.1. Datagrams vs. Streams
      2. 5.2.2. Marking Message Boundaries
      3. 5.2.3. Using Message Size
      4. 5.2.4. Using Message Markers
    3. 5.3. Using Network Programming Libraries
      1. 5.3.1. The libCurl Library
      2. 5.3.2. Downloading libCurl
      3. 5.3.3. Installing libCurl
      4. 5.3.4. Using the libCurl Library
      5. 5.3.5. Connecting to Network Resources
      6. 5.3.6. Storing Retrieved Data
    4. 5.4. Summary
  11. 6. Databases
    1. 6.1. Persistent Data Storage
      1. 6.1.1. Using a Standard File
      2. 6.1.2. Using a Database
    2. 6.2. The Berkeley DB Package
      1. 6.2.1. Downloading and Installing
      2. 6.2.2. Building Programs
      3. 6.2.3. Basic Data Handling
        1. 6.2.3.1. Opening and Closing a Database
        2. 6.2.3.2. Adding New Data
        3. 6.2.3.3. Retrieving Data
        4. 6.2.3.4. Deleting Data
        5. 6.2.3.5. Using Cursors
    3. 6.3. The PostgreSQL Database Server
      1. 6.3.1. Downloading and Installing
      2. 6.3.2. Building Programs
      3. 6.3.3. Creating an Application Database
      4. 6.3.4. Connecting to the Server
      5. 6.3.5. Executing SQL Commands
        1. 6.3.5.1. Commands Returning No Data
        2. 6.3.5.2. Commands Returning Data
        3. 6.3.5.3. Handling Column Data
      6. 6.3.6. Using Parameters
    4. 6.4. Summary
  12. 7. Kernel Development
    1. 7.1.
      1. 7.1.1. Starting Out
        1. 7.1.1.1. Background Prerequisites
        2. 7.1.1.2. Kernel Sources
          1. 7.1.1.2.1. A Tour of the Kernel Source
          2. 7.1.1.2.2. Kernel Versioning
          3. 7.1.1.2.3. Vendor Kernels
        3. 7.1.1.3. Configuring the Kernel
          1. 7.1.1.3.1. Vendor Kernel Configurations
          2. 7.1.1.3.2. Configuring for Cross-Compilation
        4. 7.1.1.4. Building the Kernel
        5. 7.1.1.5. The Built Kernel
        6. 7.1.1.6. Testing the Kernel
          1. 7.1.1.6.1. Initial Ramdisks: initrds
        7. 7.1.1.7. Packaging and Installing Kernels
    2. 7.2. Kernel Concepts
      1. 7.2.1. A Word of Warning
      2. 7.2.2. The Task Abstraction
        1. 7.2.2.1. The Scheduler
        2. 7.2.2.2. System Calls
        3. 7.2.2.3. Execution Contexts
          1. 7.2.2.3.1. Process vs. Interrupt Context
        4. 7.2.2.4. Kernel Threads
      3. 7.2.3. Virtual Memory
        1. 7.2.3.1. The Page Abstraction
        2. 7.2.3.2. Memory Mappings
          1. 7.2.3.2.1. Kernel Memory Mapping
        3. 7.2.3.3. Task Address Spaces
      4. 7.2.4. Don't Panic!
    3. 7.3. Kernel Hacking
      1. 7.3.1. Loadable Modules
        1. 7.3.1.1. An Example Module
    4. 7.4. Kernel Development Process
      1. 7.4.1. Git: the "Stupid Content Tracker"
        1. 7.4.1.1. gitk: the Graphical Git Tree Browser
      2. 7.4.2. The Linux Kernel Mailing List
        1. 7.4.2.1. Submitting Linux Kernel Patches
        2. 7.4.2.2. The Kernel Newbies Mailing List
          1. 7.4.2.2.1. Kernel Janitors
      3. 7.4.3. The "mm" Development Tree
      4. 7.4.4. The Stable Kernel Team
      5. 7.4.5. LWN: Linux Weekly News
    5. 7.5. Summary
  13. 8. Kernel Interfaces
    1. 8.1. What Is an Interface?
      1. 8.1.1. Undefined Interfaces
    2. 8.2. External Kernel Interfaces
      1. 8.2.1. System Calls
        1. 8.2.1.1. Tracing System Calls
          1. 8.2.1.1.1. Transferring Control to the Kernel: sys_open
        2. 8.2.1.2. The System Call Table
        3. 8.2.1.3. Implementing Your Own Syscalls
        4. 8.2.1.4. The vsyscall Optimization
      2. 8.2.2. The Device File Abstraction
        1. 8.2.2.1. Character Files
          1. 8.2.2.1.1. Creating a Character Device
          2. 8.2.2.1.2. Building the Driver
          3. 8.2.2.1.3. A Note on Dynamic Devices
          4. 8.2.2.1.4. Driver Initialization
          5. 8.2.2.1.5. Driver Uninitialization
          6. 8.2.2.1.6. Driver payload
        2. 8.2.2.2. Block Devices
        3. 8.2.2.3. Everything Is a File?
        4. 8.2.2.4. Mechanism vs. Policy
        5. 8.2.2.5. Procfs
          1. 8.2.2.5.1. Creating procfs Entries
          2. 8.2.2.5.2. Building the Driver
        6. 8.2.2.6. Sysfs
      3. 8.2.3. Kernel Events
      4. 8.2.4. Ignoring Kernel Protections
    3. 8.3. Internal Kernel Interfaces
      1. 8.3.1. The Kernel API
      2. 8.3.2. The kernel ABI
        1. 8.3.2.1. Kernel Module Packages
    4. 8.4. Summary
  14. 9. Linux Kernel Modules
    1. 9.1. How Modules Work
      1. 9.1.1. Extending the Kernel Namespace
      2. 9.1.2. No Guaranteed Module Compatibility
    2. 9.2. Finding Good Documentation
      1. 9.2.1. Linux Kernel Man Pages
    3. 9.3. Writing Linux Kernel Modules
      1. 9.3.1. Before You Begin
      2. 9.3.2. Essential Module Requirements
        1. 9.3.2.1. module.h
        2. 9.3.2.2. init.h
      3. 9.3.3. Logging
      4. 9.3.4. Exported Symbols
        1. 9.3.4.1. EXPORT_SYMBOL
        2. 9.3.4.2. EXPORT_SYMBOL_GPL
        3. 9.3.4.3. Don't Fight the System
      5. 9.3.5. Allocating Memory
        1. 9.3.5.1. kmalloc
          1. 9.3.5.1.1. GTP_ATOMIC
          2. 9.3.5.1.2. GFP_KERNEL
          3. 9.3.5.1.3. GFP_USER
        2. 9.3.5.2. vmalloc
        3. 9.3.5.3. plp_kmem: a Kernel Memory Buffer
          1. 9.3.5.3.1. Build and Test the Example
          2. 9.3.5.3.2. A Note on udev
      6. 9.3.6. Locking considerations
        1. 9.3.6.1. Semaphores
        2. 9.3.6.2. Spinlocks
        3. 9.3.6.3. plp_klock: Adding User Tracking to plp_kmem
        4. 9.3.6.4. Locking Considerations for plp_klock
      7. 9.3.7. Deferring work
        1. 9.3.7.1. Work Queues
        2. 9.3.7.2. Kernel Threads
          1. 9.3.7.2.1. kthread_create
        3. 9.3.7.3. Kernel Thread Example
      8. 9.3.8. Further Reading
    4. 9.4. Distributing Linux Kernel Modules
      1. 9.4.1. Going Upstream
      2. 9.4.2. Shipping Sources
      3. 9.4.3. Shipping Prebuilt Modules
    5. 9.5. Summary
  15. 10. Debugging
    1. 10.1. Debugging Overview
      1. 10.1.1. A Word about Memory Management
    2. 10.2. Essential Debugging Tools
      1. 10.2.1. The GNU Debugger
        1. 10.2.1.1. Compiling Code for Use with GDB
        2. 10.2.1.2. Starting GDB
          1. 10.2.1.2.1. Setting Breakpoints
        3. 10.2.1.3. Visualizing Data
        4. 10.2.1.4. Backtrace
        5. 10.2.1.5. A Buggy Example
        6. 10.2.1.6. Debugging a Core File
      2. 10.2.2. Valgrind
        1. 10.2.2.1. Automated Code Analysis
    3. 10.3. Graphical Debugging Tools
      1. 10.3.1. DDD
      2. 10.3.2. Eclipse
    4. 10.4. Kernel Debugging
      1. 10.4.1. Don't Panic!
      2. 10.4.2. Making Sense of an oops
        1. 10.4.2.1.
          1. 10.4.2.1.1. Don't Trust Subsequent oopses
        2. 10.4.2.2. Reporting Bugs to Upstream
      3. 10.4.3. Using UML for Debugging
        1. 10.4.3.1. Building a UML Kernel
      4. 10.4.4. An Anecdotal Word
      5. 10.4.5. A Note about In-Kernel Debuggers
    5. 10.5. Summary
  16. 11. The GNOME Developer Platform
    1. 11.1. GNOME Libraries
      1. 11.1.1. Glib
      2. 11.1.2. GObject
      3. 11.1.3. Cairo
      4. 11.1.4. GDK
      5. 11.1.5. Pango
      6. 11.1.6. GTK+
      7. 11.1.7. libglade
      8. 11.1.8. GConf
      9. 11.1.9. GStreamer
    2. 11.2. Building a Music Player
      1. 11.2.1. Requirements
      2. 11.2.2. Getting Started: The Main Window
        1. 11.2.2.1. Compilation
      3. 11.2.3. Building the GUI
        1. 11.2.3.1. The Top-Level Container
        2. 11.2.3.2. Text Labels
        3. 11.2.3.3. The Slider Control
        4. 11.2.3.4. The Control Buttons
        5. 11.2.3.5. Menus
        6. 11.2.3.6. Keyboard Shortcuts
        7. 11.2.3.7. Callbacks
        8. 11.2.3.8. Introducing GStreamer
        9. 11.2.3.9. Opening Files
        10. 11.2.3.10. Metadata
        11. 11.2.3.11. Seeking
      4. 11.2.4. Summary
  17. 12. The FreeDesktop Project
    1. 12.1. D-BUS: The Desktop Bus
      1. 12.1.1. What Is D-Bus?
      2. 12.1.2. Under D-Hood of D-Bus
      3. 12.1.3. D-Bus Methods
    2. 12.2. Hardware Abstraction Layer
      1. 12.2.1. Making Hardware Just Work
      2. 12.2.2. Hal Device Objects
    3. 12.3. The Network Manager
    4. 12.4. Other Freedesktop Projects
    5. 12.5. Summary
  18. 13. Graphics and Audio
    1. 13.1. Linux and Graphics
      1. 13.1.1. X Windows
      2. 13.1.2. Open Graphics Library
      3. 13.1.3. OpenGL Utilities Toolkit
      4. 13.1.4. Simple Directmedia Layer
    2. 13.2. Writing OpenGL Applications
      1. 13.2.1. Downloading and Installing
      2. 13.2.2. Programming Environment
      3. 13.2.3. Using the GLUT Library
        1. 13.2.3.1. Creating a Window
        2. 13.2.3.2. Drawing Objects
        3. 13.2.3.3. Processing User Input
        4. 13.2.3.4. Animation
    3. 13.3. Writing SDL Applications
      1. 13.3.1. Downloading and Installing
      2. 13.3.2. Programming Environment
      3. 13.3.3. Using the SDL Library
        1. 13.3.3.1. Displaying an Image
        2. 13.3.3.2. Playing an Audio File
        3. 13.3.3.3. Accessing the CD-ROM
    4. 13.4. Summary
  19. 14. LAMP
    1. 14.1. What Is LAMP?
      1. 14.1.1. Apache
      2. 14.1.2. MySQL
      3. 14.1.3. PHP
      4. 14.1.4. The Rebel Platform
      5. 14.1.5. Evaluating the LAMP Platform
        1. 14.1.5.1. Cost and Licensing
        2. 14.1.5.2. Certification
        3. 14.1.5.3. Support and Services
        4. 14.1.5.4. Vendor/Developer Community Relationship
        5. 14.1.5.5. Integration with and Support for Other Technologies
    2. 14.2. Apache
      1. 14.2.1. Virtual Hosting
      2. 14.2.2. Installation and Configuration of PHP 5
        1. 14.2.2.1. PHP Configuration
        2. 14.2.2.2. Installation of PHP Modules
      3. 14.2.3. Apache Basic Authentication
      4. 14.2.4. Apache and SSL
      5. 14.2.5. Integrating SSL with HTTP Authentication
    3. 14.3. MySQL
      1. 14.3.1. Installing MySQL
      2. 14.3.2. Configuring and Starting the Database
      3. 14.3.3. Changing the Default Password
      4. 14.3.4. The MySQL Client Interface
      5. 14.3.5. Relational Databases
      6. 14.3.6. SQL
        1. 14.3.6.1. Creating Tables
        2. 14.3.6.2. Inserting a Record
        3. 14.3.6.3. Searching for Records
          1. 14.3.6.3.1. The SELECT Clause
          2. 14.3.6.3.2. The WHERE Clause
        4. 14.3.6.4. Editing a Record
        5. 14.3.6.5. Deleting Records
      7. 14.3.7. The Relational Model
        1. 14.3.7.1. Relations, Tuples, and Attributes
        2. 14.3.7.2. Keys and Relationships
        3. 14.3.7.3. Relational Operators
        4. 14.3.7.4. Data Integrity
        5. 14.3.7.5. Data Modeling and Normalization
    4. 14.4. PHP
      1. 14.4.1. The PHP Language
        1. 14.4.1.1. Language Basics
        2. 14.4.1.2. Requiring/Including Files
        3. 14.4.1.3. Magic Type Casting
        4. 14.4.1.4. Arrays/Lists/Dictionaries
        5. 14.4.1.5. Functions
        6. 14.4.1.6. Scope
        7. 14.4.1.7. Loops and Iterators
        8. 14.4.1.8. Objects
        9. 14.4.1.9. Pass by Reference/Pass by Value?
      2. 14.4.2. Error Handling
      3. 14.4.3. Error-Handling Exceptions
      4. 14.4.4. Optimization Techniques
        1. 14.4.4.1. Strings
        2. 14.4.4.2. Arrays
        3. 14.4.4.3. xdebug: Profiling and/or Debugging PHP Code
      5. 14.4.5. Installing Additional PHP Software
      6. 14.4.6. Logging
      7. 14.4.7. Parameter Handling
        1. 14.4.7.1. Register Globals
      8. 14.4.8. Session Handling
      9. 14.4.9. Unit Testing
      10. 14.4.10. Databases and PHP
      11. 14.4.11. PHP Frameworks
    5. 14.5. The DVD Library
      1. 14.5.1. Version 1: The Developer's Nightmare
      2. 14.5.2. Version 2: Basic Application with DB-Specific Data Layer
      3. 14.5.3. Version 3: Rewriting the Data Layer, Adding Logging and Exceptions
        1. 14.5.3.1. Adding Database Abstraction
        2. 14.5.3.2. Exceptions
        3. 14.5.3.3. Logging
      4. 14.5.4. Version 4: Applying a Templating Framework
    6. 14.6. Summary