AA RREEAALL--WWOORRLLDD Companion CD LLIINNUU XX AA PPPPLLII AA NNCCEE GGUUIIDDEE TTOO includes a prototype appliance—a home BBUUIILLDDIINNGG LLIINNUUXX alarm system—that supports DDEESSIIGGNN AAPPPPLLIIAANNCCEESS the book’s lessons. CD Inside A H A N D S - O N G U I D E T O B U I L D I N G L I N U X A P P L I A N C E S Modern appliances are complex machines with Learn to: processors, operating systems, and application software. • Separate your user interfaces from your daemons While there are books that will tell you how to run Linux LL BOB SMI T H, JOH N H A R DIN, G R A H A M PH IL L IP S, A ND BIL L PIE RCE on embedded hardware, and books on how to build • Give user interfaces run time access to configuration, II a Linux application, Linux Appliance Design is the first status, and statistics NN book to demonstrate how to merge the two and create UU • Add professional network management capabilities to a Linux appliance. You’ll see for yourself why Linux is your application XX the embedded operating system of choice for low-cost development and a fast time to market. • Use SNMP and build a MIB AA Linux Appliance Design shows how to build better • Build a web-based appliance interface PP appliances—appliances with more types of interfaces, PP • Build a command line interface (CLI) more dynamic interfaces, and better debugged inter- LL faces. You’ll learn how to build backend daemons, • Build a framebuffer interface with an infrared control II handle asynchronous events, and connect various user as input AA interfaces (including web, framebuffers, infrared control, • Manage logs and alarms on an appliance NN SNMP, and front panels) to these processes for remote CC configuration and control. Linux Appliance Design ABOUT THE AUTHORS also introduces the Run-Time Access library, which EE Bob Smith, John Hardin, Graham Phillips, and Bill Pierce provides a uniform mechanism for user interfaces to have experience in embedded systems, commercial Linux DD communicate with daemons. and BSD appliances, network management systems, and EE designing software solutions for business problems. SS II GG NN P H PIERILIPSHARDSMIT THE FINEST IN GEEK ENTERTAINMENT™ $59.95 ($74.95 CDN) CE, ANIN,H, ® www.nostarch.com OPERATINSHELVE IN D This b o o k u“sIe sL RAeYpK FoLveArT—.”a durable binding that won’t snap shut. G SYSTEMS/LIN: ® ® UX No Starch Press, Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce LINUX APPLIANCE DESIGN No Starch Press, Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce No Starch Press, Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce LINUX APPLIANCE DESIGN A Hands-On Guide to Building Linux Appliances by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce ® San Francisco No Starch Press, Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce lad_TITLE_COPY.fm Page iv Thursday, March 15, 2007 2:38 PM LINUX APPLIANCE DESIGN. Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce. All “Tux’s Workshop” illustrations © 2006 by Jon Colton. 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. Printed on recycled paper in the United States of America 11 10 09 08 07 1 2 3 4 5 6 7 8 9 ISBN-10: 1-59327-140-9 ISBN-13: 978-1-59327-140-4 Publisher: William Pollock Production Editor: Elizabeth Campbell Cover and Interior Design: Octopod Studios Developmental Editor: William Pollock Technical Reviewer: Bob Lynch Copyeditor: Megan Dunchak Compositor: Riley Hoffman Proofreader: Publication Services, Inc. Indexer: Publication Services, Inc. For information on book distributors or translations, please contact No Starch Press, Inc. directly: No Starch Press, Inc. 555 De Haro Street, Suite 250, San Francisco, CA 94107 phone: 415.863.9900; fax: 415.863.9950; [email protected]; www.nostarch.com Library of Congress Cataloging-in-Publication Data Linux appliance design : a hands-on guide to building linux appliances / Bob Smith ... [et al.]. p. cm. Includes index. ISBN-13: 978-1-59327-140-4 ISBN-10: 1-59327-140-9 1. Linux. 2. Application software--Development. 3. Electric apparatus and appliances--Design and construction. I. Smith, Robert W. (Robert William), 1952- QA76.76.O63L545115 2007 005.3--dc22 2006020778 No Starch Press and the No Starch Press logo are registered trademarks of No Starch Press, Inc. Other product and company names mentioned herein may be the trademarks of their respective owners. Rather than use a trademark symbol with every occurrence of a trademarked name, we are using the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The information in this book is distributed on an “As Is” basis, without warranty. While every precaution has been taken in the preparation of this work, neither the author nor No Starch Press, Inc. 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 it. No Starch Press, Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce B R I E F C O N T E N T S Acknowledgments..........................................................................................................xv Introduction.................................................................................................................xvii Chapter 1: Appliance Architecture....................................................................................1 Chapter 2: Managing Daemons........................................................................................7 Chapter 3: Using Run-Time Access..................................................................................19 Chapter 4: Building and Securing Daemons.....................................................................43 Chapter 5: The Laddie Alarm System: A Sample Appliance................................................61 Chapter 6: Logging.......................................................................................................77 Chapter 7: Laddie Event Handling...................................................................................91 Chapter 8: Designing a Web Interface..........................................................................105 Chapter 9: Designing a Command Line Interface............................................................135 Chapter 10: Building a Front Panel Interface..................................................................147 Chapter 11: Designing a Framebuffer Interface...............................................................169 Chapter 12: Infrared Remote Control.............................................................................197 Chapter 13: Hands-on Introduction to SNMP..................................................................223 No Starch Press, Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce Chapter 14: Designing an SNMP MIB...........................................................................243 Chapter 15: Implementing Your SNMP MIB....................................................................261 Appendix A: RTA Reference.........................................................................................289 Appendix B: Review of SNMP......................................................................................309 Appendix C: Installing a Framebuffer Device Driver.........................................................325 Appendix D: A DB-to-File Utility.....................................................................................331 Appendix E: The Laddie Appliance Bootable CD............................................................337 Index.........................................................................................................................345 vi Brief Contents No Starch Press, Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce C O N T E N T S I N D E T A I L ACKNOWLEDGMENTS xv INTRODUCTION xvii What This Book Is About ........................................................................................xviii What This Book Is Not About..................................................................................xviii Who Should Read This Book ...................................................................................xix Why Use Linux? .....................................................................................................xix Availability of Source Code ........................................................................xix Range of Hardware Supported ..................................................................xix Availability of Linux Developers ..................................................................xix Reliability ..................................................................................................xx Quality Compilers ......................................................................................xx Good Documentation .................................................................................xx Existing Software Packages .........................................................................xx Low Development Cost ................................................................................xx No Licensing Fees for Deployment ................................................................xx Security ....................................................................................................xxi Linux Appliance Design...........................................................................................xxi 1 APPLIANCE ARCHITECTURE 1 UIs and Daemons .....................................................................................................2 Daemons....................................................................................................2 User Interfaces ............................................................................................3 Interprocess Communication .........................................................................4 The Architecture of the Laddie Appliance ....................................................................5 Summary .................................................................................................................5 2 MANAGING DAEMONS 7 Common Approaches to Managing Daemons..............................................................8 File-Based Management ...............................................................................8 A Daemon-Based Web Interface ...................................................................9 An All-in-One Approach .............................................................................10 Control and Status Protocols ....................................................................................11 Requirements for a Control and Status Protocol .............................................12 Common Control and Status Protocols .........................................................13 Summary ...............................................................................................................18 3 USING RUN-TIME ACCESS 19 RTA Appliance Architecture .....................................................................................19 RTA Daemon Architecture ........................................................................................20 No Starch Press, Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce Telling RTA About Your Columns and Tables..............................................................21 Columns...................................................................................................22 Tables ......................................................................................................24 Building Your First RTA Program ...............................................................................26 Defining the Problem .................................................................................26 Reviewing the Code ..................................................................................26 Installing RTA ............................................................................................31 Building and Linking ..................................................................................32 Testing .....................................................................................................32 A Little SQL ...........................................................................................................34 SELECT ....................................................................................................35 UPDATE ...................................................................................................35 WHERE....................................................................................................36 LIMIT .......................................................................................................36 Introduction to RTA’s Built-in Tables ...........................................................................37 rta_dbg ....................................................................................................37 rta_stat.....................................................................................................37 rta_tables .................................................................................................38 rta_columns ..............................................................................................38 The RTA Table Editor ..............................................................................................38 Summary ...............................................................................................................41 4 BUILDING AND SECURING DAEMONS 43 How to Build a Daemon ..........................................................................................44 Load the Daemon’s Configuration ...............................................................44 Go into the Background .............................................................................45 Become the Process and Session Leader .......................................................46 Set the Working Directory ..........................................................................47 Redirect stdin, stdout, and stderr .................................................................47 Set Up Logging .........................................................................................48 Set Group and User IDs .............................................................................49 Check for a pidfile ....................................................................................50 Set the umask ...........................................................................................52 Set Up Signal Handlers ..............................................................................52 How to Secure a Daemon .......................................................................................53 Designing a Secure Daemon ......................................................................54 Write a Secure Daemon.............................................................................55 Limit Damage in Case of a Breach ..............................................................57 A Prototype Daemon ..............................................................................................59 Summary ...............................................................................................................60 Further Reading .....................................................................................................60 5 THE LADDIE ALARM SYSTEM: A SAMPLE APPLIANCE 61 Introduction to Alarm Systems ..................................................................................62 Sensors ....................................................................................................62 Enabling a Zone .......................................................................................64 viii Contents in Detail No Starch Press, Copyright © 2007 by Bob Smith, John Hardin, Graham Phillips, and Bill Pierce
Description: