Your SlideShare is downloading. ×
  • Like
Wrox professional linuxprogramming
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Wrox professional linuxprogramming

  • 2,377 views
Published

 

Published in Education
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
2,377
On SlideShare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
2
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Professional LINUX Programming
  • 2. Table of Contents Professional Linux Programming.....................................................................................................................1 Introduction.........................................................................................................................................................9 Welcome.................................................................................................................................................9 Who is This Book for?............................................................................................................................9 What's Covered in This Book?...............................................................................................................9 What You Need to Use This Book.......................................................................................................12 Source Code..........................................................................................................................................12 Conventions..........................................................................................................................................13 Chapter 1: Application Design.......................................................................................................................14 Overview...............................................................................................................................................14 Development Models............................................................................................................................15 The Waterfall model........................................................................................................................15 Iterative development......................................................................................................................15 'Fast Track' Development...............................................................................................................16 Test Early, Test Often....................................................................................................................16 The DVD Store.....................................................................................................................................17 Initial Requirements.......................................................................................................................18 Analyzing the User Requirements..................................................................................................18 Statement of Requirements...................................................................................................................23 Use Cases.......................................................................................................................................25 Application Architecture.......................................................................................................................26 Detailed Design..............................................................................................................................27 Data Access Functions...................................................................................................................28 Member Functions..........................................................................................................................29 Title Functions................................................................................................................................30 Disk Functions................................................................................................................................31 Rental Functions.............................................................................................................................32 Reference Implementation.............................................................................................................33 Resources..............................................................................................................................................33 Summary...............................................................................................................................................34 Chapter 2: CVS................................................................................................................................................35 Overview...............................................................................................................................................35 Tools for Linux.....................................................................................................................................35 Terminology..........................................................................................................................................36 The Repository......................................................................................................................................36 Single User CVS projects.....................................................................................................................37 CVS Command Format..................................................................................................................37 Environment Variables...................................................................................................................38 Importing a New Project................................................................................................................39 Starting Work on Our Project.........................................................................................................40 Checking Our Changes Against the Repository.............................................................................41 Updating the Repository with Our Changes...................................................................................42 Releasing the Project......................................................................................................................43 Reviewing Changes........................................................................................................................44 Adding and Removing Files from a Project...................................................................................44 Professional LINUX Programming i
  • 3. Table of Contents Chapter 2: CVS Keyword Substitution.....................................................................................................................44 Revisions, Tags and Branches..............................................................................................................45 Revisions........................................................................................................................................45 Tags................................................................................................................................................46 Branches.........................................................................................................................................48 Multi−user CVS....................................................................................................................................53 Working Collaboratively................................................................................................................53 Working with Watches...................................................................................................................55 More Fun with CVS..............................................................................................................................55 Binary Files....................................................................................................................................55 Correcting Bad Annotations...........................................................................................................55 Accessing CVS Across a Network.......................................................................................................55 GUI CVS Clients..................................................................................................................................57 Resources..............................................................................................................................................59 Summary...............................................................................................................................................59 Chapter 3: Databases.......................................................................................................................................61 Overview...............................................................................................................................................61 Choosing a Database.............................................................................................................................61 mSQL.............................................................................................................................................62 MySQL...........................................................................................................................................62 PostgreSQL....................................................................................................................................62 Which is Right for Me?..................................................................................................................62 PostgreSQL...........................................................................................................................................63 Installation and Commissioning.....................................................................................................63 Database Fundamentals........................................................................................................................68 First Normal Form..........................................................................................................................68 Second Normal Form.....................................................................................................................69 Third Normal Form........................................................................................................................69 De−normalization...........................................................................................................................69 A Simple Database.........................................................................................................................69 Using psql.............................................................................................................................................73 Commands to psql..........................................................................................................................73 Data Definition Commands............................................................................................................74 Data Manipulation Commands.......................................................................................................79 Transactions....................................................................................................................................85 Database Design Tips...........................................................................................................................86 Resources..............................................................................................................................................87 Summary...............................................................................................................................................88 Chapter 4: PostgreSQL Interfacing...............................................................................................................89 Accessing PostgreSQL from Code.......................................................................................................89 Libpq..............................................................................................................................................89 ECPG............................................................................................................................................106 Which Method to Use?.................................................................................................................114 The Application..................................................................................................................................114 Summary.............................................................................................................................................120 Professional LINUX Programming ii
  • 4. Table of Contents Chapter 5: MySQL........................................................................................................................................121 Installation and Commissioning.........................................................................................................121 Pre−compiled Packages................................................................................................................121 Building from Source...................................................................................................................122 Post−install Configuration............................................................................................................123 MySQL Administration......................................................................................................................124 Commands....................................................................................................................................124 Creating Users, and Giving Them Permissions............................................................................127 Passwords.....................................................................................................................................129 Creating a Database......................................................................................................................129 SQL Support in PostgreSQL and MySQL..........................................................................................130 Accessing MySQL Data from C.........................................................................................................132 Connection Routines....................................................................................................................132 Error Handling..............................................................................................................................135 Executing SQL Statements...........................................................................................................136 Miscellaneous Functions..............................................................................................................147 Resources............................................................................................................................................147 Summary.............................................................................................................................................148 Chapter 6: Tackling Bugs.............................................................................................................................149 Overview.............................................................................................................................................149 Error Classes.......................................................................................................................................149 Reporting Errors..................................................................................................................................149 Detecting Software Errors...................................................................................................................152 Types of Software Error......................................................................................................................152 Debug Statements...............................................................................................................................153 Assertions.....................................................................................................................................156 Where Are You?.................................................................................................................................158 Backtrace......................................................................................................................................160 Preparing to Debug.............................................................................................................................162 Using the Debugger......................................................................................................................163 Simple GDB Commands..............................................................................................................164 Other GDB Features.....................................................................................................................166 Resources......................................................................................................................................167 Summary.............................................................................................................................................167 Chapter 7: LDAP Directory Services...........................................................................................................169 What is a Directory Service?..............................................................................................................169 X.500 and LDAP..........................................................................................................................169 Structure of a Directory Server...........................................................................................................170 The Naming of Parts.....................................................................................................................171 dn Naming....................................................................................................................................171 Object Components......................................................................................................................172 LDAP Directory Tree...................................................................................................................174 LDIF Files....................................................................................................................................175 Installing and Configuring an LDAP Server......................................................................................176 Steps in Installing OpenLDAP.....................................................................................................176 Configuring OpenLDAP..............................................................................................................177 Professional LINUX Programming iii
  • 5. Table of Contents Chapter 7: LDAP Directory Services Running the Server.............................................................................................................................179 Accessing LDAP from C....................................................................................................................180 Initialize the LDAP Library.........................................................................................................181 Bind to the LDAP Server.............................................................................................................181 LDAP Error Handling..................................................................................................................182 A First LDAP Client Program......................................................................................................183 Searching......................................................................................................................................183 Changing the Data...............................................................................................................................192 Adding a New Entry.....................................................................................................................192 Modifying an Entry......................................................................................................................195 Deleting an Entry..........................................................................................................................197 The Application..................................................................................................................................198 Resources............................................................................................................................................198 Summary.............................................................................................................................................198 Chapter 8: GUI programming with GNOME/GTK+................................................................................200 Overview.............................................................................................................................................200 The GTK+/GNOME libraries.............................................................................................................200 glib.................................................................................................................................................201 GTK+............................................................................................................................................201 GDK..............................................................................................................................................201 Imlib..............................................................................................................................................201 ORBit.............................................................................................................................................201 libGnorba.......................................................................................................................................201 glib......................................................................................................................................................202 Types............................................................................................................................................202 Macros..........................................................................................................................................203 String functions............................................................................................................................205 Memory Allocation......................................................................................................................206 Lists..............................................................................................................................................207 GTK+..................................................................................................................................................209 Widgets.........................................................................................................................................209 gtk_init and gtk_main...................................................................................................................214 Example GTK+ Application........................................................................................................214 GNOME Basics..................................................................................................................................215 gnome_init.....................................................................................................................................216 GnomeApp....................................................................................................................................216 Menus and Toolbars.....................................................................................................................216 Dialogs..........................................................................................................................................219 Creating a GnomeDialog...............................................................................................................220 Example GNOME Application....................................................................................................225 The GNOME Source Tree..................................................................................................................227 configure.in....................................................................................................................................228 Makefile.am...................................................................................................................................229 Configuration Saving...................................................................................................................230 Storing data....................................................................................................................................230 Reading the Stored Data................................................................................................................231 Professional LINUX Programming iv
  • 6. Table of Contents Chapter 8: GUI programming with GNOME/GTK+ Session Management....................................................................................................................232 Command Line Parsing Using popt.............................................................................................234 GNOME/GTK+ Resources..........................................................................................................237 Summary.............................................................................................................................................237 Chapter 9: GUI Building with Glade and GTK+/GNOME.......................................................................238 Overview.............................................................................................................................................238 Overview of Glade..............................................................................................................................238 A Word on GUI Design................................................................................................................239 A Glade Tutorial.................................................................................................................................239 Main Window...............................................................................................................................240 The Palette....................................................................................................................................241 The Properties Window................................................................................................................244 The Glade−built Source Tree.......................................................................................................248 lookup_widget..............................................................................................................................249 Adding Code.................................................................................................................................250 libglade.........................................................................................................................................252 The DVD Store GNOME GUI...........................................................................................................254 Design...........................................................................................................................................255 Compiling and Running dvdstore.................................................................................................255 Structure.......................................................................................................................................260 Code..............................................................................................................................................261 Summary.............................................................................................................................................285 Chapter 10: Flex and Bison...........................................................................................................................286 Overview.............................................................................................................................................286 Input Structure....................................................................................................................................286 Scanners and Parsers....................................................................................................................288 How Generators Work..................................................................................................................288 Scanners..............................................................................................................................................289 A Simple Scanner.........................................................................................................................289 Scanner Specifications.................................................................................................................290 Longest Match Principle..............................................................................................................294 Regular Expressions.....................................................................................................................295 Actions..........................................................................................................................................297 Redirecting Scanner Input and Output.........................................................................................297 Returning Tokens.........................................................................................................................298 Context Sensitive Scanners..........................................................................................................299 Options to flex..............................................................................................................................300 Parsers.................................................................................................................................................300 Generating Parsers........................................................................................................................301 Creating a Syntax Tester..............................................................................................................306 Token Types.................................................................................................................................309 Actions in Rules...........................................................................................................................310 Options to bison............................................................................................................................316 Conflicts in Grammars.................................................................................................................317 Arithmetic Expressions................................................................................................................318 Professional LINUX Programming v
  • 7. Table of Contents Chapter 10: Flex and Bison Resources............................................................................................................................................319 Summary.............................................................................................................................................319 Chapter 11: Testing Tools.............................................................................................................................320 Overview.............................................................................................................................................320 Testing Requirements Types...............................................................................................................320 Application Architecture..............................................................................................................320 Steps.............................................................................................................................................321 General Testing............................................................................................................................321 Regression Testing.......................................................................................................................322 A Test Program............................................................................................................................324 Testing the dvdstore Program.......................................................................................................328 Scripting Tests..............................................................................................................................329 expect............................................................................................................................................329 Memory Problems........................................................................................................................331 Installing mpatrol.........................................................................................................................337 Using mpatrol...............................................................................................................................338 Testing Coverage..........................................................................................................................342 Performance Testing.....................................................................................................................349 Summary.............................................................................................................................................351 Chapter 12: Secure Programming...............................................................................................................352 What is Secure Programming?............................................................................................................352 Why Secure Programming is Hard.....................................................................................................352 Stealthy Bugs.................................................................................................................................352 The Virtue of Paranoia..................................................................................................................353 Filesystem Security......................................................................................................................354 Authenticating Users....................................................................................................................357 Using Cryptography Securely......................................................................................................369 A Short Introduction to Cryptography..........................................................................................369 Public−Key Crypto........................................................................................................................369 Secure Hash Algorithms................................................................................................................370 On Writing Custom/Proprietary Algorithms.................................................................................370 Secure Network Programming.....................................................................................................374 Writing Protocols.........................................................................................................................374 Standard Network Cryptography Tools.......................................................................................378 SSL/TLS........................................................................................................................................378 ssh..................................................................................................................................................378 Problems With the Environment..................................................................................................379 Python...........................................................................................................................................385 PHP...............................................................................................................................................386 Resources............................................................................................................................................386 Internet Information......................................................................................................................386 Summary.............................................................................................................................................387 Professional LINUX Programming vi
  • 8. Table of Contents Chapter 13: GUI Programming with KDE/Qt............................................................................................388 Introduction.........................................................................................................................................388 About Qt.......................................................................................................................................388 About KDE...................................................................................................................................388 Installing Qt..................................................................................................................................389 Installing KDE..............................................................................................................................390 Libraries........................................................................................................................................390 Programming Applications Using Qt.................................................................................................390 Getting Started: Hello World.......................................................................................................391 Simplifying Makefile Management With tmake..........................................................................392 Signals and Slots..........................................................................................................................393 'Hello world' Revisited.................................................................................................................395 Deriving From Base Classes........................................................................................................396 Widgets.........................................................................................................................................398 Layouts.........................................................................................................................................399 Programming Applications Using KDE.............................................................................................403 A Simple Text Editor...................................................................................................................403 Resources............................................................................................................................................409 Summary.............................................................................................................................................410 Chapter 14: Writing the DVD Store GUI Using KDE/Qt..........................................................................411 Overview.............................................................................................................................................411 Application Design.......................................................................................................................411 Main Window...............................................................................................................................413 Member Dialog.............................................................................................................................418 Rent Dialog...................................................................................................................................421 Rental Report Dialog....................................................................................................................423 Search Window............................................................................................................................423 The Settings Manager...................................................................................................................428 Adjusting the Code to KDE................................................................................................................430 KConfig and SettingsManager......................................................................................................435 Resources............................................................................................................................................435 Summary.............................................................................................................................................435 Chapter 15: Python........................................................................................................................................437 Introduction.........................................................................................................................................437 Features........................................................................................................................................438 Python: The Right Tool for the Job..............................................................................................441 ...but not every job!......................................................................................................................441 Installing Python.................................................................................................................................441 Running Python..................................................................................................................................443 The Interactive Interpreter............................................................................................................443 Command Argument....................................................................................................................444 Script Argument...........................................................................................................................444 'Standalone' Executable................................................................................................................444 The Details..........................................................................................................................................445 Interpreter and Byte−Compilation...............................................................................................445 Comment Syntax..........................................................................................................................445 Professional LINUX Programming vii
  • 9. Table of Contents Chapter 15: Python Case Sensitivity............................................................................................................................446 Built−In Data Types and Operators.............................................................................................446 Variables.......................................................................................................................................455 Block Structure Syntax.................................................................................................................455 Statement Syntax..........................................................................................................................456 Functions......................................................................................................................................462 Built−In Functions........................................................................................................................463 Namespaces..................................................................................................................................464 Modules and Packages.................................................................................................................464 Some Modules From The Standard Distribution.........................................................................464 Classes and Objects......................................................................................................................465 Extending Python.........................................................................................................................467 An Example Program: Penny Pinching..............................................................................................467 Online Resources................................................................................................................................472 Summary.............................................................................................................................................472 Chapter 16: Creating Web Interfaces with PHP........................................................................................474 Overview.............................................................................................................................................474 PHP and Server−Side Scripting..........................................................................................................474 Server−side scripting....................................................................................................................474 PHP capabilities...........................................................................................................................476 Installing and Configuring PHP..........................................................................................................476 Building and Installing PHP as a CGI Interpreter.........................................................................477 Building and Installing PHP with Apache as an Apache module.................................................478 Installing PHP from an RPM.........................................................................................................479 Configuring PHP...........................................................................................................................479 Introducing PHP syntax......................................................................................................................481 Variables, Constants and Data types............................................................................................481 Operators in PHP..........................................................................................................................482 Statements....................................................................................................................................483 Functions......................................................................................................................................484 Arrays...........................................................................................................................................485 Using PHP with the DVD project.......................................................................................................485 HTTP, HTML and PHP................................................................................................................486 Application..........................................................................................................................................488 Login............................................................................................................................................488 Reservation status.........................................................................................................................488 Search for titles.............................................................................................................................488 Reserve Titles...............................................................................................................................488 Cancellation..................................................................................................................................488 dvdstorefunctions.php..................................................................................................................489 dvdstorecommon.php...................................................................................................................494 dvdstorelogin.php.........................................................................................................................497 dvdstoresearch.php.......................................................................................................................498 dvdstorestatus.php........................................................................................................................500 dvdstorecancel.php.......................................................................................................................500 dvdstorereserve.php......................................................................................................................501 Professional LINUX Programming viii
  • 10. Table of Contents Chapter 16: Creating Web Interfaces with PHP Summary.............................................................................................................................................502 Resources............................................................................................................................................502 Chapter 17: Embedding and Extending Python with C/C++....................................................................503 Overview.............................................................................................................................................503 Extending Python with a C/C++ extension module.....................................................................503 Embedding Python in a Host Program.........................................................................................504 Developing Extension Modules in C/C++..........................................................................................504 Required Software Tools..............................................................................................................504 Extending Python Using SWIG...................................................................................................505 Extending Python Using the C API....................................................................................................529 Python Object Types....................................................................................................................529 Reference Counting and Ownership.............................................................................................530 Overview of Developing C Extension Modules...........................................................................531 Simple Functions..........................................................................................................................533 A Slightly More Complex Function.............................................................................................534 The Global Interpreter Lock.........................................................................................................535 Creating New Python Object Types.............................................................................................535 Encapsulating C++ Objects Using the C−API.............................................................................542 Embedding Python in C/C++ Programs.............................................................................................544 The Embedding Development Environment................................................................................545 Embedding Python Using High−level Functions.........................................................................545 Statically Linking a Host Program to an Extension Module........................................................547 Embedding Python Using Lower−level Calls..............................................................................548 General Suggestions............................................................................................................................558 Resources............................................................................................................................................559 Summary.............................................................................................................................................559 Chapter 18: Remote Procedure Calls..........................................................................................................560 Overview.............................................................................................................................................560 A Simple Networked DVD Store Database........................................................................................561 BSD Sockets.................................................................................................................................561 Coding Issues Using the BSD Socket Interface...........................................................................565 ONC RPC Architecture and Concepts.........................................................................................566 Why Use RPC in the DVD Store Application?..................................................................................567 RPC Tools and Utilities......................................................................................................................568 rpcgen the RPC Protocol Compiler.............................................................................................568 Applying RPCs to the DVD Store......................................................................................................570 Functions Without Arguments or Return Types..........................................................................570 Functions with Simple Arguments and Simple Return Types.....................................................578 More Complex Examples.............................................................................................................579 Returning Arrays..........................................................................................................................582 Client Timeouts............................................................................................................................585 Authentication.....................................................................................................................................586 AUTH_NONE..............................................................................................................................586 AUTH_UNIX...............................................................................................................................586 Client−Side Authentication Support............................................................................................586 Professional LINUX Programming ix
  • 11. Table of Contents Chapter 18: Remote Procedure Calls Server−Side Authentication Support............................................................................................587 Using RPC Servers with /etc/inetd.conf.............................................................................................589 Other Methods to Simplify Network Programming...........................................................................590 Resources............................................................................................................................................590 Summary.............................................................................................................................................591 Chapter 19: Multimedia and Linux.............................................................................................................592 Overview.............................................................................................................................................592 The Current State of Affairs...............................................................................................................592 Program Integration............................................................................................................................593 Sound..................................................................................................................................................593 Devices.........................................................................................................................................594 Handling Standard Audio Formats...............................................................................................595 Do−It−Yourself............................................................................................................................597 Moving Pictures..................................................................................................................................611 Software Players...........................................................................................................................611 Hardware Players.........................................................................................................................612 Hybrids.........................................................................................................................................613 Political and Legal Issues....................................................................................................................614 References...........................................................................................................................................614 Summary.............................................................................................................................................615 Chapter 20: CORBA......................................................................................................................................616 Overview.............................................................................................................................................616 Interface Definition Language (IDL)..................................................................................................616 Object Request Broker (ORB)............................................................................................................616 Interoperable Object Reference (IOR)................................................................................................617 Object Adapter....................................................................................................................................617 Servers.................................................................................................................................................617 Naming and Trading services.............................................................................................................618 Evaluating CORBA............................................................................................................................618 CORBA and RPC...............................................................................................................................619 CORBA and Sockets..........................................................................................................................620 Systems Similar To CORBA..............................................................................................................621 DCOM or COM+.........................................................................................................................621 Java Remote Method Invocation (RMI).......................................................................................622 Enterprise JavaBeans....................................................................................................................622 IBM MQSeries.............................................................................................................................622 SOAP............................................................................................................................................622 IDL: Defining Interfaces.....................................................................................................................623 Modules........................................................................................................................................623 Interfaces......................................................................................................................................624 Basic Data Types..........................................................................................................................625 Template Types............................................................................................................................625 Example DVD Application..........................................................................................................630 Language Mappings............................................................................................................................633 Language Mapping Components..................................................................................................634 Professional LINUX Programming x
  • 12. Table of Contents Chapter 20: CORBA C Mappings..................................................................................................................................635 An Introductory Example: A Simple Messaging System...................................................................640 Simple Messaging........................................................................................................................640 Using ORBit With The IDL.........................................................................................................641 The Message Client......................................................................................................................641 The Message Server.....................................................................................................................643 Compiling the ORBit Application................................................................................................644 Running The Message Application..............................................................................................645 Resources............................................................................................................................................645 Summary.............................................................................................................................................646 Chapter 21: Implementing CORBA with ORBit........................................................................................647 Overview.............................................................................................................................................647 Using CORBA for the DVD Store Application..................................................................................647 The DVD Client...........................................................................................................................648 The DVD Server...........................................................................................................................648 A Logging Server.........................................................................................................................648 Validation Server..........................................................................................................................648 Client Code...................................................................................................................................649 Log Server....................................................................................................................................649 DVD Server..................................................................................................................................650 Putting It All Together.................................................................................................................663 Using libgnorba...................................................................................................................................664 Configuring ORBit for Multi−Host Use.............................................................................................664 GOAD − GNOME Object Activation Directory................................................................................665 The Use of CORBA in GNOME........................................................................................................665 Advanced CORBA Functionality.......................................................................................................666 Dynamic Interface Invocation......................................................................................................667 CORBAServices...........................................................................................................................667 CORBAFacilities..........................................................................................................................671 Designing and Running Scalable CORBA Services....................................................................671 Resources............................................................................................................................................675 Summary.............................................................................................................................................675 Chapter 22: Diskless Systems.......................................................................................................................676 Overview.............................................................................................................................................676 A Little History...................................................................................................................................676 What, No Disk?...................................................................................................................................677 Why Go Diskless?...............................................................................................................................678 How Does It Work?............................................................................................................................678 Starting a Diskless System...........................................................................................................679 Network Identification for Diskless Systems...............................................................................680 Running an Operating System......................................................................................................681 Server Configuration...........................................................................................................................682 Boot Image Creation...........................................................................................................................684 Diskless Linux Kernel.........................................................................................................................685 Root File Systems...............................................................................................................................688 Professional LINUX Programming xi
  • 13. Table of Contents Chapter 22: Diskless Systems Problems.............................................................................................................................................690 Client Applications.............................................................................................................................691 Summary.............................................................................................................................................692 Chapter 23: XML and libxml.......................................................................................................................694 Overview.............................................................................................................................................694 XML Document Structure..................................................................................................................695 XML Syntax.................................................................................................................................695 Well−formed XML......................................................................................................................696 Valid XML...................................................................................................................................699 XML Parsing.......................................................................................................................................703 DOM.............................................................................................................................................703 SAX..............................................................................................................................................704 libXML a.k.a. gnome−xml...........................................................................................................704 The Complete Parser....................................................................................................................716 main()............................................................................................................................................717 start_document()............................................................................................................................718 end_document().............................................................................................................................718 start_element()...............................................................................................................................718 end_element()................................................................................................................................719 chars_found().................................................................................................................................719 get_event_from_name()................................................................................................................720 state_event_machine()...................................................................................................................721 Resources............................................................................................................................................722 Summary.............................................................................................................................................722 Chapter 24: Beowulf Clusters.......................................................................................................................723 Overview.............................................................................................................................................723 Hardware Setup...................................................................................................................................723 Software Configuration.......................................................................................................................724 Programming a Beowulf Cluster........................................................................................................724 Programming Using MPI....................................................................................................................724 The Basic Functionality of an MPI Program................................................................................726 Compiling and Executing a Simple MPI Program.......................................................................727 A Distributed MP3 Encoder.........................................................................................................728 Communication Performance of a Beowulf Cluster....................................................................730 A Review of Advanced Features of MPI.....................................................................................733 Some MPI Programming Examples:............................................................................................739 Programming with PVM.....................................................................................................................748 Comparison with MPI..................................................................................................................748 Obtaining and Installing PVM.....................................................................................................749 A Review of PVM Library Routines............................................................................................749 A Sample PVM Program..............................................................................................................751 Resources............................................................................................................................................753 Summary.............................................................................................................................................753 Some Useful Beowulf Links.........................................................................................................753 Professional LINUX Programming xii
  • 14. Table of Contents Chapter 25: Documentation..........................................................................................................................754 Overview.............................................................................................................................................754 Defining the Audience..................................................................................................................754 End User Documentation: GUIs...................................................................................................755 Power User/System Administrator Documentation.....................................................................758 It's All About Structure: From Single Program to Distributed Systems......................................768 Documentation Tools...................................................................................................................768 Developer Documentation............................................................................................................775 Summary.............................................................................................................................................781 Chapter 26: Device Drivers...........................................................................................................................783 Overview.............................................................................................................................................783 Execution Context...............................................................................................................................783 Module and Initialization Code..........................................................................................................784 Linker Sections...................................................................................................................................785 Example Module Code.................................................................................................................785 PCI Devices and Drivers.....................................................................................................................786 struct pci_dev...............................................................................................................................786 Finding PCI Devices....................................................................................................................787 PCI Drivers...................................................................................................................................788 PCI Access Functions...................................................................................................................789 Resource Allocation.....................................................................................................................790 Interrupt Handlers.........................................................................................................................791 Access to User Space Memory.....................................................................................................793 The kiobuf Architecture...............................................................................................................795 Locking Primitives.......................................................................................................................798 Scheduling and Wait Queues.......................................................................................................800 Module Use Counts......................................................................................................................805 Making It Build............................................................................................................................806 What to Do with Your New Driver.....................................................................................................808 Submitting a New Driver.............................................................................................................809 Summary.............................................................................................................................................810 Chapter 27: Distributing the Appendix lication.........................................................................................811 Overview.............................................................................................................................................811 RPM Packages....................................................................................................................................812 The RPM User..............................................................................................................................812 What Do I Have Installed?...........................................................................................................813 The RPM Database.......................................................................................................................814 Anatomy of an RPM Package......................................................................................................822 Source Packages...........................................................................................................................823 configure, autoconf and automake......................................................................................................824 Source RPM Packages..................................................................................................................827 Building an RPM Package..................................................................................................................828 Patches................................................................................................................................................832 Making a Patch.............................................................................................................................832 Applying a Patch..........................................................................................................................834 GNATS...............................................................................................................................................834 Professional LINUX Programming xiii
  • 15. Table of Contents Chapter 27: Distributing the Appendix lication Summary.............................................................................................................................................835 Chapter 28: Internationalization..................................................................................................................836 Overview.............................................................................................................................................836 I18N Terminology..............................................................................................................................837 Isn't Unicode the Answer?..................................................................................................................838 Unicode........................................................................................................................................838 The Character Encoding Problem.......................................................................................................843 ISO 2022: Extension Techniques for Coded Character Sets........................................................843 Programming with Unicode.........................................................................................................844 I18N Models and the System Environment........................................................................................849 The POSIX Locale Model............................................................................................................850 Collation........................................................................................................................................851 Character Type..............................................................................................................................852 Messages.......................................................................................................................................852 Monetary.......................................................................................................................................852 Numeric.........................................................................................................................................852 Time...............................................................................................................................................852 The X/Open Portability Guide (XPG)..........................................................................................853 Output Formatting and Input Processing............................................................................................855 The X Window System................................................................................................................855 Practical Considerations of I18N Programming.................................................................................860 I18N and Internal Text Processing...............................................................................................861 Programming with Locales..........................................................................................................862 Category Dimensions...................................................................................................................862 Category Conversions..................................................................................................................863 Category I/O.................................................................................................................................863 Category String formatting...........................................................................................................863 Category Character classification.................................................................................................863 Category Conversions..................................................................................................................863 Category String copying and filling.............................................................................................864 Category String searching............................................................................................................864 Category Collation.......................................................................................................................864 Category Regular expressions......................................................................................................864 Category Locale manipulation.....................................................................................................864 Category Message catalogs..........................................................................................................864 I18N and Xlib Programming........................................................................................................870 I18N and Linux GUIs...................................................................................................................876 Status of I18N for Linux Software Development........................................................................878 I18N in Real Software Development Projects....................................................................................878 Object Oriented Programming and I18N.....................................................................................879 Application Builders and I18N.....................................................................................................880 Where Next for Linux I18N?.......................................................................................................882 Appendix A: GTK+ & GNOME Object Reference....................................................................................884 Overview.............................................................................................................................................884 GTK+ Widgets and Functions............................................................................................................884 Professional LINUX Programming xiv
  • 16. Table of Contents Appendix A: GTK+ & GNOME Object Reference GtkButton.....................................................................................................................................884 GtkCheckButton...........................................................................................................................884 GtkCList.......................................................................................................................................885 GtkCombo....................................................................................................................................889 GtkEntry.......................................................................................................................................890 GtkFrame......................................................................................................................................891 GtkHBox......................................................................................................................................891 GtkHButtonBox............................................................................................................................891 GtkHSeparator..............................................................................................................................892 GtkLabel.......................................................................................................................................892 GtkMenu.......................................................................................................................................893 GtkMenuBar.................................................................................................................................894 GtkMenuItem...............................................................................................................................894 GtkNotebook................................................................................................................................895 GtkOptionMenu............................................................................................................................897 GtkPixmapMenuItem...................................................................................................................898 GtkScrolledWindow.....................................................................................................................898 GtkSpinButton..............................................................................................................................899 GtkTable.......................................................................................................................................900 GtkText.........................................................................................................................................901 GtkVBox......................................................................................................................................902 GtkWindow..................................................................................................................................902 GNOME Widgets & Functions...........................................................................................................903 GnomeAbout................................................................................................................................903 GnomeApp...................................................................................................................................903 GnomeAppBar..............................................................................................................................904 GnomeDateEdit............................................................................................................................905 GnomeDialog...............................................................................................................................905 GnomeDock..................................................................................................................................906 GnomeDockItem..........................................................................................................................907 GnomeEntry.................................................................................................................................908 GnomePropertyBox......................................................................................................................908 References...........................................................................................................................................909 Appendix B: The DVD Store RPC Protocol Definition..............................................................................910 Appendix C: Open Source Licenses.............................................................................................................916 The GNU General Public License......................................................................................................916 GNU GENERAL PUBLIC LICENSE.........................................................................................916 The Lesser GNU Public License.........................................................................................................921 Version 2, June 1991.....................................................................................................................921 Preamble........................................................................................................................................921 TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION.....922 END OF TERMS AND CONDITIONS.......................................................................................927 How to Apply These Terms to Your New Libraries.....................................................................927 The GNU Free Documentation License.............................................................................................928 Version 1.1, March 2000...............................................................................................................928 Professional LINUX Programming xv
  • 17. Table of Contents Appendix C: Open Source Licenses 0. PREAMBLE..............................................................................................................................928 1. APPLICABILITY AND DEFINITIONS..................................................................................929 2. VERBATIM COPYING...........................................................................................................929 3. COPYING IN QUANTITY......................................................................................................930 4. MODIFICATIONS...................................................................................................................930 5. COMBINING DOCUMENTS..................................................................................................931 6. COLLECTIONS OF DOCUMENTS........................................................................................932 7. AGGREGATION WITH INDEPENDENT WORKS..............................................................932 8. TRANSLATION.......................................................................................................................932 9. TERMINATION.......................................................................................................................932 10. FUTURE REVISIONS OF THIS LICENSE..........................................................................932 How to use this License for your documents................................................................................933 The Q Public License..........................................................................................................................933 THE Q PUBLIC LICENSE version 1.0.......................................................................................933 Granted Rights..............................................................................................................................934 Limitations of Liability................................................................................................................934 No Warranty.................................................................................................................................935 Choice of Law..............................................................................................................................935 Appendix D: Customer Support and Feedback..........................................................................................936 Customer Support and Feedback........................................................................................................936 Source Code and Updates.............................................................................................................936 Errata............................................................................................................................................936 forums.apress.com........................................................................................................................936 Symbols & Numbers....................................................................................................................937 A...................................................................................................................................................937 B...................................................................................................................................................939 C...................................................................................................................................................939 D...................................................................................................................................................942 E....................................................................................................................................................942 F....................................................................................................................................................944 G...................................................................................................................................................944 H...................................................................................................................................................951 I.....................................................................................................................................................951 J....................................................................................................................................................961 K...................................................................................................................................................961 L....................................................................................................................................................964 M..................................................................................................................................................964 N...................................................................................................................................................966 O...................................................................................................................................................966 P....................................................................................................................................................974 Q...................................................................................................................................................974 R...................................................................................................................................................975 S....................................................................................................................................................975 T....................................................................................................................................................979 U...................................................................................................................................................979 V...................................................................................................................................................980 Professional LINUX Programming xvi
  • 18. Table of Contents Appendix D: Customer Support and Feedback W..................................................................................................................................................980 X...................................................................................................................................................982 Y−Z..............................................................................................................................................982 Index.................................................................................................................................................................988 Index.................................................................................................................................................................988 Index.................................................................................................................................................................995 Index.................................................................................................................................................................995 Index.................................................................................................................................................................997 Index.................................................................................................................................................................997 Index...............................................................................................................................................................1000 Index...............................................................................................................................................................1000 Index...............................................................................................................................................................1012 Index...............................................................................................................................................................1012 Index...............................................................................................................................................................1015 Index...............................................................................................................................................................1015 Index...............................................................................................................................................................1020 Index...............................................................................................................................................................1020 Index...............................................................................................................................................................1029 Index...............................................................................................................................................................1029 Index...............................................................................................................................................................1032 Index...............................................................................................................................................................1032 Index...............................................................................................................................................................1034 Index...............................................................................................................................................................1034 Index...............................................................................................................................................................1035 Professional LINUX Programming xvii
  • 19. Table of Contents Index...............................................................................................................................................................1035 Index...............................................................................................................................................................1037 Index...............................................................................................................................................................1037 Index...............................................................................................................................................................1039 Index...............................................................................................................................................................1039 Professional LINUX Programming xviii
  • 20. Professional Linux Programming Neil Matthew, Richard Stones Christopher Browne Brad Clements Andrew Froggatt David J. Goodger Ivan Griffin Jeff Licquia Ronald van Loon Harish Rawat Udaya A. Ranawake Marius Sundbakken Deepak Thomas Stephen J. Turnbull David Woodhouse Copyright © 2004 by Apress (This book was originally published by Wrox Press in 2000.) All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN (pbk): 1861003013 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. Distributed to the book trade in the United States by Springer−Verlag New York, Inc., 175 Fifth Avenue, New York, NY, 10010 and outside the United States by Springer−Verlag GmbH & Co. KG, Tiergartenstr. 17, 69112 Heidelberg, Germany. In the United States: phone 1−800−SPRINGER, email orders@springer−ny.com, or visit http://www.springer−ny.com. Outside the United States: fax +49 6221 345229, email orders@springer.de, or visit http://www.springer.de. For information on translations, please contact Apress directly at 2560 Ninth Street, Suite 219, Berkeley, CA 94710. Phone 510−549−5930, fax 510−549−5939, email info@apress.com, or visit http://www.apress.com. The information in this book is distributed on an "as is" basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. Professional Linux Programming 1
  • 21. The source code for this book is available to readers at http://www.apress.com in the Downloads section. Credits Authors Technical Reviewers Neil Matthew Chris Tregenza Richard Stones Ronald van Loon Bruce Varney Contributing Authors Paul Warren Christopher Browne Mark Wilcox Brad Clements Peter Wright Andrew Froggatt David J. Goodger Category Manager Ivan Griffin Viv Emery Jeff Licquia Ronald van Loon Author Agent Udaya Ranawake Lynne Basset Harish Rawat Marius Sundbakken Proof Readers Deepak Thomas Lisa Rutter Stephen J. Turnbull Christopher Smith David Woodhouse Keith Westmoreland Technical Architect Production Manager Louay Fatoohi Laurent Lafon Technical Editors Project Administrator David Mercer Nicola Phillips Dan Squier Production Coordinator Technical Reviewers Tom Bartlett Robert Applebaum Jason Bennett Design/Layout Jonathon Blank Tom Bartlett Michael Boerner Wankyu Choi Illustrations Brad Clements Shabnam Hussain Andrew Froggatt Chris Harshman Chapter Divider Artwork Dave Hudson Fidget Dave Jewel Giles Lean Cover Marty Leisner Chris Morris Professional LINUX Programming Professional Linux Programming 2
  • 22. Bill Moss Shelley Frazier Mike Olson Jonathon Pinnock Index Gavin Smyth Alessandro Ansa Paul Spencer About the Authors Neil Matthew Neil has been programming computers of one sort or another since 1974, but doesn't feel that old. Keen on programming languages and the ways they can be used to solve different problems he has written his fair share of emulators, interpreters, and translators, including ones for Basic, BCPL, FP (Functional Programming), Lisp, Prolog, and the 6502 microprocessor hardware at the heart of the BBC Microcomputer. He graduated from the University of Nottingham, England with a degree in Mathematics, but got stuck into computers straight away. He has used UNIX since 1978, including most academic and commercial variants, some now long forgotten. Highlights include UNIX versions 6 and 7 on PDP 11/34 and 11/70, Xenix on PDP 11/23 and Intel 286 and 386, BSD 4.2 on DEC VAX 11/750, UNIX System V on MicroVAX and Intel 386, Sun SunOS4 on Sparc, and Solaris on Sparc and Intel. He now collects Linux distributions to run on his home network of six PCs. Neil's first Linux was a 0.99.11 kernel based SLS system that was shipped across the Atlantic in boxes and boxes of floppy disks in August 1993. He has been using Linux ever since, both at home and at work, programming mainly in C, C++, Icon, and Tcl. He uses and recommends Linux for Internet connections, usually as a proxy caching server for Windows LANs and also as a file and print server to Windows 9x/NT using SAMBA. He's sold a number of Internet firewall systems to UK companies (including to Wrox in their early days!). Neil says that Linux is a great development environment, as it offers all of the flexibility and power of traditional UNIX systems, but it manages to combine the strengths of just about all of the disparate UNIX variants (such as System V and BSD). Programs written for just about any UNIX will port to Linux with little or no effort. You can also "get under the hood" with Linux as the source code is freely available. As Head of Software and Principal Engineer at Camtec Electronics in the 1980s Neil programmed in C and C++ for real−time embedded systems. Since then he's worked on software development techniques and quality assurance both as a consultant in communications software development with Scientific Generics and as a software QA specialist with GEHE UK. Linux has played an increasing role in the work that he has undertaken over the years, from file servers, through Internet gateways to forming the platform for a distributed radio communications system. Neil is married to Christine and has two children, Alexandra and Adrian. He lives in a converted barn in Northamptonshire, England. His interests include computers, music, science fiction, chess, motor sport, and not doing it yourself. Richard Stones Richard started programming in the early days, when a BBC with 32k on RAM was a serious home computer. He graduated from Nottingham University, England with an Electronics degree, but decided that software was more fun. Professional LINUX Programming Professional Linux Programming 3
  • 23. He has worked for a variety of companies over the years, from the very small with just two dozen employees, to the American multinational EDS. Along the way he has worked on a wide variety of interesting projects. These have ranged from communications network management systems, embedded real time systems, and multi−gigabyte help desk and user management systems, through to more mundane accountancy systems. He has always done his best to get Linux running as part of his projects, and usually finds a niche for Linux somewhere. In many projects, especially those requiring embedded software, Linux has been used as the main development platform. He has also installed Linux as file and print servers and Internet gateways. He first met UNIX style operating systems on a PDP 11/23+, after which BSD 4.2 on a VAX seemed like a big leap forward. He has used many of the various commercial UNIX offerings, and bemoans the unnecessary differences between them. He first discovered Linux when Slackware CDs of the 0.99 kernel became available, and was amazed at how much quicker it ran than the commercial versions of UNIX he had previously worked on, without compromising functionality. He hopes that Linux distributions never fragment in the way the commercial offerings did. He programs mainly in C or Java, but has also worked in C++, SQL, PHP, Perl, various assembly languages, and some proprietary real time languages, and under duress will admit that he's quite familiar with Visual Basic, but claims he only used it because it was a lesser evil than the alternatives available at the time. He is currently employed as a systems architect for GEHE, who are the UK's largest pharmaceutical wholesaler and retailer, as well as the largest pharmaceutical wholesaler in both France and Germany, and active in many other European countries. Rick lives in a Leicestershire village, in England, with his wife Ann, children Jenny and Andrew, and two cats. Outside computers his passion is for classical music, especially early church music. He tries to find time to practice the piano, but it always seems to be last on the list of things to do. Rick and Neil co−authored Instant UNIX, and Beginning Linux Programming and have contributed chapters to one or two other books. They also spoke at the first Bang!inux conference in Bangalore in February 2000. AUTHORS' ACKNOWLEDGMENTS We, Richard and Neil, would like to thank our families: Neil's wife Christine for her unfailing support and understanding, and his children Alexandra and Adrian for thinking that it's cool to have a Dad who can write books. Rick's wife Ann, and children, Jenny and Andrew, for their patience during the many evenings and weekends while the book was written. He would also like to thank them for being so understanding about the decision to do more writing. We would also like to thank the many people who made this book possible. Firstly the people who enjoyed Beginning Linux Programming, making it the success it has been, providing useful feedback and spurring us on to write a sequel. We have taken their suggestions for ways to extend and improve BLP, and this is the result a book that we hope takes Linux application development to the next level. We have tried to introduce some more advanced topics and show how programs can be made robust, flexible, secure, and extensible, ready to be distributed and maintained in a professional manner. We would like to thank the team at Wrox for their hard work on the book, especially Louay, David M, Dan S, Richard, James, Nicola, Lynne, Rob, Dan M, Andrew P, and last, but not least, John for buying the pizza, as Professional LINUX Programming Professional Linux Programming 4
  • 24. well as the others who worked behind the scenes. We would also like to thank the people who have contributed additional material to the book; they provided some excellent material. Special thanks are also due to the team of reviewers who worked on our chapters. They provided comments and suggestions of the highest quality, and went to efforts above and beyond the call of duty to improve the book. Thank you very much one and all. Any errors that have slipped through are, of course, entirely our own fault. We, Neil and Rick, would also like to thank our employers, GEHE, for their support while we were writing this book. We would also like to pay homage to Linus for the Linux platform, RMS for the excellent set of GNU tools and the GPL, and the ever expanding throng of unsung heroes who choose to make their software freely available. Christopher Browne Christopher is a consultant with Sabre Inc., in the Human Resources and Payroll Systems organization supporting these systems for AMR (American Airlines). He has been involved since 1996 with conversions at AMR to use SAP R/3 for financial accounting and for HR and payroll systems. He was previously a Systems Engineer with SHL Systemhouse (now EDS) in their SAP R/3 practice. He is also the treasurer of the North Texas Linux Users Group (NTLUG). Chris holds a Bachelor of Mathematics degree from the University of Waterloo, Joint Honors Co−op Chartered Accountancy and Computer Science, and a Master of Science degree in Systems Science from the University of Ottawa, Canada. Brad Clements Brad Clements is the president of MurkWorks, Inc., a software consulting company based in Potsdam, New York, USA. He has over two decades experience developing software for a wide variety of operating systems, handheld gauges, and embedded devices. A firm believer of "use the best tool for the job", he has found Python to be an elegant, powerful solution for an increasing number of projects. While attending Clarkson University as a Physics major, he caught the entrepreneurial spirit and ran off to join a startup firm developing an airport bomb detection device. Later, he worked as a Sr. Network Engineer for five years before forming his own consulting business. Although he spends much of his time managing the company, he still finds time to develop new software, study emerging technologies, and collaborate with other open source developers. An avid horseman, dog lover, and pilot, he enjoys show jumping and flying. He has recently begun building his own airplane a Sonex. Brad lives in the Adirondack mountain region of northern New York with his wife, Marsha and two lovely daughters Rachael and Rhiannon. You can reach him on the Internet at bkc+plip@MurkWorks.com Andrew Froggatt Professional LINUX Programming Professional Linux Programming 5
  • 25. Andrew is a student at Cambridge University, England, reading Experimental and Theoretical physics. His first encounter with computers was with a trusty BBC Micro at the age of six, with which he first programmed with BBC BASIC (the best language ever). He also learned the important life skill of how to adjust the cassette player volume level to load games such as Elite or Stryker's Run first time. Andrew discovered Linux around ten years later, and immediately took to it because he thought it was really cool to enter a password to use your own computer. Now he can say he's written dozens of large and small programs with C, Java, ML, Perl, and even Fortran, all on various platforms. Having gone to Cambridge and studied a little Computer Science, he found himself one summer working for Wrox Press, and they've pestered him several times to write and technically review for them since something that he's very happy to do. Despite all this, Andrew does not know what to do after he graduates. David J. Goodger David is a programmer, systems administrator, and consultant. He collects programming languages but loves Python best. He has worked in education (teaching English in Japan), government (two years as an embassy employee in Tokyo), and industry. His hobbies include Go, puzzles, bicycling, reading (and aspiring to writing), good science fiction, and poker. He helped his wife produce two beautiful children, and they all live happily in Kitchener, Ontario, Canada. This is his first professionally published work. Ivan Griffin Ivan works for Parthus Technologies plc, on some really crazy bleeding edge technology. His most recent project has been embedded development on Bluetooth, the 2.4 GHz short range wireless radio system where lower power, low MIPs and low RAM requirements are essential. Previously, he worked on a research project for the University of Limerick, Ireland. This project involved dynamically reconfigurable telecomms systems through the use of migratable CORBA/Java agents. Ivan has developed on many different platforms from various flavors of UNIX to Windows to Z80 and ARM7 embedded systems. He stumbled across Linux 0.99 sometime in '92/93 as an undergraduate, and has been hooked on the environment ever since. Aside from computers, Ivan has keen interests in swimming, skiing, and mountain−biking. Jeff Licquia Jeff has been working in the information industry for over 10 years in many diverse roles. He discovered Linux in its early days (before it could boot multi−user), and has professionally deployed Linux since 1993. Currently, he is the network administrator for Springfield Clinic, a health clinic in his home town of Springfield, Illinois, USA. Outside of work, he enjoys being active in the Debian project, as well as spending time with his wife and two children. Ronald van Loon Professional LINUX Programming Professional Linux Programming 6
  • 26. Ronald is currently working as an IT Architect for IBM Global Services in Amsterdam, The Netherlands. A neighbor (who had built a computer for himself) first introduced him, at the age of 12, to computers. Soon after that he owned his first computer (a Commodore 64) and started to experiment with it. He now has a Masters degree in Computer Science and has about 12 years of work experience in several fields, ranging from medical imaging to video−on−demand applications. Ronald has broad interests with a weak spot for multimedia and route planning. He was the developer of the TMF (a Dutch commercial broadcasting channel) Cyberchoice program, a standalone interactive play−out video system that works without human intervention. In his leisure time he sings in a choir, goes to the theatre, looks after Bas (a little teddy bear), and lives and loves together with his girlfriend Marjolijn in their combined apartments in Amersfoort. You can reach him by email: mail@rvl.nu Udaya A. Ranawake Udaya is a research scientist at Goddard Earth Sciences and Technology (GEST) Center at NASA GSFC, USA. He has more than ten years of experience developing software for parallel computers. Currently, he is working on the Hive project at NASA GSFC the goal of which is to build a low cost high performance parallel computer using commodity hardware and freely available software packages. Udaya holds a BSc degree in Electrical Engineering from University of Moratuwa, Sri Lanka, and MSc and PhD degrees in Electrical & Computer Engineering from Oregon State University, USA. Harish Rawat Harish is a Software Developer at the Oracle Corporation, USA. He has eight years of experience in systems programming. His technical areas of interest include XML, Java, and Network Protocols. Marius Sundbakken Marius received a Software Engineering degree from the college of Buskerud in Kongsberg, Norway. After a year of study at Washington State University, USA, he received a Bachelor of Computer Science degree. He plans to get his Masters in Computer Science, in the near future. His main interest in computing is object−oriented software design, especially using Qt. C++ is his favorite language, although he uses C and Java if he has to. He bought his first computer, an Amiga, at the age of sixteen, and learned a wide variety of languages, ranging from C, AREXX, 680x0 assembly, to C++. Marius first noticed Qt in 1996, and has been programming Linux applications using Qt in his spare time. He made QtVu, an image viewer based on Qt (www.qtvu.org), and is currently writing an email client called Mailliam (www.mailliam.org) , which is also Qt based. Thanks to Jan Borsodi at eZ Systems for technical assistance. Deepak Thomas Deepak Thomas works for Oracle corporation at Redwood Shores, CA, USA. His areas of interest include PHP, Linux, and several Java related technologies. He co−authored Professional PHP Programming. Professional LINUX Programming Professional Linux Programming 7
  • 27. Stephen J. Turnbull Stephen daylights as an economist. He moved to Japan in 1990, and discovered that the Japanese have four different ways to encode ASCII, let alone the multiple ways they encode the three native character sets. That left him no alternative to learning about internationalization of software in detail. Steve was dual booting Linux and DESQview/X in the months before January 1995; he started leaving Linux running 24x7 on January 17, and for the next four days his web page was the Internet's main broadband window on the Kobe earthquake disaster. Now he lives a quieter life, occasionally working on multilingual features of Xemacs, and advocating better internationalization for Linux. David Woodhouse David is a Linux kernel hacker, working for Red Hat on embedded Linux technology. He is responsible for the Memory Technology Device drivers in the Linux kernel, which handle solid state storage devices such as Flash chips. He encountered Linux while studying Computer Science at the University of Cambridge, England, and hasn't done any "real work" since then. He is often suspected of being schizophrenic long periods of languishing in front of a computer in the dark are punctuated with a violent desire to get outside and climb mountains. David lives near Cambridge, which is a shame because there are no mountains there. Professional LINUX Programming Professional Linux Programming 8
  • 28. Introduction Welcome Welcome to the exciting world of Linux Programming. If you are one of the many readers of our authoritative book Beginning Linux Programming then be prepared for another enjoyable and informative journey into the world of Linux. If this is your first encounter with our Linux programming book series, then you'll shortly be convinced that you have got the right book. Who is This Book for? This book is for experienced Linux programmers and those aspiring to become developers for one of the most exciting Operating Systems around. This book covers topics that have been carefully chosen, based on the knowledge of what professional developers usually encounter during their careers. This includes practical information on libraries, techniques, tools and applications for Linux programmers. Versatility, and breadth of choice, ensure that you are more than likely to find something that is of particular interest to you. Depth of coverage is what professional developers can expect to find when consulting this book, and we have made every effort to strike the right balance between the type of topics that we cover and the depth of our coverage. Whether an experienced Linux programmer or on your way to be so, this book is for you. What's Covered in This Book? In both editions of our first Linux programming book, Beginning Linux Programming (ISBN 1861002971), we covered many tools, libraries and techniques that every Linux programmer should be familiar with. In this book, we tackle new, more advanced topics that professional Linux programmers are bound to deal with. Professional Linux Programming is the natural sequel to Beginning Linux Programming. Maintaining the style that we followed in Beginning Linux Programming, this book takes a practical approach. Whenever necessary, examples are called upon to support and explain theory. Again, following in the path of Beginning Linux Programming, this book adopts a central application example that is developed as the book progresses. To be precise, we use a DVD rental store application to introduce the various tools, libraries and techniques. We have divided the chapters into two categories: theme chapters, which discuss topics that progress the DVD store application theme, and take−a−break chapters. The latter are standalone chapters that tackle a variety of topics of interest to professional developers. Rather than having a continuous flow of 17 theme chapters followed by 11 non−theme chapters, we have used the take−a−break chapters as "stopping stations" between the theme chapters. The distinction between the two types of chapter does not imply that the topics covered by one group of chapters are more important. Additionally, the theme chapters differ to an extent in how much they revolve around the DVD store application theme. Both kinds of chapter are practical tutorials that use examples to put the theory into practice. So, what are those chapters about? Introduction 9
  • 29. We start off Chapter 1 with an overview of issues involved in application design. Next, we discuss the DVD store application that is developed and used in the theme chapters. We explain how to determine and formalize the requirements of our application. The objective of the chapter is achieved when we translate those requirements into APIs. When working with a project of any size, there is always a need to track changes to our code. While it is possible to do this manually when we work on our own and when our project is small, we certainly need a better and more efficient way of doing this when managing large projects and/or working within a team. Chapter 2 introduces us to a powerful source control system: the Concurrent Versions System (CVS). We show how to install and use CVS, and we invest