Freebsd Developers' Handbook
Total Page:16
File Type:pdf, Size:1020Kb
FreeBSD Developers’ Handbook The FreeBSD Documentation Project FreeBSD Developers’ Handbook by The FreeBSD Documentation Project Published August 2000 Copyright © 2000, 2001 by The FreeBSD Documentation Project Welcome to the Developers’ Handbook. This manual is a work in progress and is the work of many individuals. Many sections do not yet exist and some of those that do exist need to be updated. If you are interested in helping with this project, send email to the FreeBSD documentation project mailing list <[email protected]>. The latest version of this document is always available from the FreeBSD World Wide Web server (http://www.FreeBSD.org/). It may also be downloaded in a variety of formats and compression options from the FreeBSD FTP server (ftp://ftp.FreeBSD.org/pub/FreeBSD/doc) or one of the numerous mirror sites (http://www.freebsd.org/handbook/mirrors-ftp.html). Redistribution and use in source (SGML DocBook) and ’compiled’ forms (SGML, HTML, PDF, PostScript, RTF and so forth) with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code (SGML DocBook) must retain the above copyright notice, this list of conditions and the following disclaimer as the first lines of this file unmodified. 2. Redistributions in compiled form (transformed to other DTDs, converted to PDF, PostScript, RTF and other formats) must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Important: THIS DOCUMENTATION IS PROVIDED BY THE FREEBSD DOCUMENTATION PROJECT "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD DOCUMENTATION PROJECT BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Table of Contents I. Basics ....................................................................................................................................................................... vii 1 Introduction ........................................................................................................................................................1 1.1 Developing on FreeBSD........................................................................................................................1 1.2 The BSD Vision.....................................................................................................................................1 1.3 Architectural Guidelines........................................................................................................................1 1.4 The Layout of /usr/src ......................................................................................................................1 2 Programming Tools ............................................................................................................................................3 2.1 Synopsis.................................................................................................................................................3 2.2 Introduction............................................................................................................................................3 2.3 Introduction to Programming ................................................................................................................3 2.4 Compiling with cc.................................................................................................................................5 2.5 Make ....................................................................................................................................................12 2.6 Debugging............................................................................................................................................16 2.7 Using Emacs as a Development Environment.....................................................................................20 2.8 Further Reading ...................................................................................................................................28 3 Secure Programming........................................................................................................................................30 3.1 Synopsis...............................................................................................................................................30 3.2 Secure Design Methodology................................................................................................................30 3.3 Buffer Overflows .................................................................................................................................30 3.4 SetUID issues.......................................................................................................................................32 3.5 Limiting your program’s environment.................................................................................................33 3.6 Trust.....................................................................................................................................................34 3.7 Race Conditions...................................................................................................................................34 4 Localization - I18N ..........................................................................................................................................35 4.1 Programming I18N Compliant Applications.......................................................................................35 II. Interprocess Communication................................................................................................................................36 5 * Signals ...........................................................................................................................................................37 6 Sockets .............................................................................................................................................................38 6.1 Synopsis...............................................................................................................................................38 6.2 Networking and Diversity....................................................................................................................38 6.3 Protocols ..............................................................................................................................................38 6.4 The Sockets Model ..............................................................................................................................41 6.5 Essential Socket Functions ..................................................................................................................41 6.6 Helper Functions..................................................................................................................................55 6.7 Concurrent Servers ..............................................................................................................................57 7 IPv6 Internals ...................................................................................................................................................60 7.1 IPv6/IPsec Implementation..................................................................................................................60 III. Kernel ....................................................................................................................................................................77 8 * History of the Unix Kernel............................................................................................................................78 9 Locking Notes ..................................................................................................................................................79 9.1 Mutexes................................................................................................................................................79 9.2 Lock Manager Locks ...........................................................................................................................82 9.3 Atomically Protected Variables ...........................................................................................................82 10 Kernel Objects................................................................................................................................................83 10.1 Terminology.......................................................................................................................................83 iii 10.2 Kobj Operation ..................................................................................................................................83 10.3 Using Kobj.........................................................................................................................................83