FreeBSD-DevelopersHandbook.pdf

(1312 KB) Pobierz
FreeBSD Developers’ Handbook
The FreeBSD Documentation Project
FreeBSD Developers’ Handbook
by The FreeBSD Documentation Project
Published August 2000
Copyright © 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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
(http://lists.FreeBSD.org/mailman/listinfo/freebsd-doc).
The latest version of this document is always available from the FreeBSD World Wide Web server
(http://www.FreeBSD.org/index.html). 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/doc/en_US.ISO8859-1/books/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.
FreeBSD is a registered trademark of the FreeBSD Foundation.
Apple, AirPort, FireWire, Mac, Macintosh, Mac OS, Quicktime, and TrueType are trademarks of Apple Computer, Inc., registered in the United
States and other countries.
IBM, AIX, EtherJet, Netfinity, OS/2, PowerPC, PS/2, S/390, and ThinkPad are trademarks of International Business Machines Corporation in the
United States, other countries, or both.
IEEE, POSIX, and 802 are registered trademarks of Institute of Electrical and Electronics Engineers, Inc. in the United States.
Intel, Celeron, EtherExpress, i386, i486, Itanium, Pentium, and Xeon are trademarks or registered trademarks of Intel Corporation or its
subsidiaries in the United States and other countries.
Linux is a registered trademark of Linus Torvalds.
Microsoft, IntelliMouse, MS-DOS, Outlook, Windows, Windows Media and Windows NT are either registered trademarks or trademarks of
Microsoft Corporation in the United States and/or other countries.
Motif, OSF/1, and UNIX are registered trademarks and IT DialTone and The Open Group are trademarks of The Open Group in the United States
and other countries.
Sun, Sun Microsystems, Java, Java Virtual Machine, JavaServer Pages, JDK, JRE, JSP, JVM, Netra, Solaris, StarOffice, Sun Blade, Sun
Enterprise, Sun Fire, SunOS, and Ultra are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations
appear in this document, and the FreeBSD Project was aware of the trademark claim, the designations have been followed by the “™” or the “®”
symbol.
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
.................................................................................................................................6
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 and Internationalization - L10N and I18N ..................................................................................35
4.1 Programming I18N Compliant Applications.......................................................................................35
4.2 Localized Messages with POSIX.1 Native Language Support (NLS) ................................................35
5 Source Tree Guidelines and Policies................................................................................................................39
5.1
MAINTAINER
on Makefiles ..................................................................................................................39
5.2 Contributed Software...........................................................................................................................39
5.3 Encumbered Files ................................................................................................................................44
5.4 Shared Libraries...................................................................................................................................45
6 Regression and Performance Testing ...............................................................................................................47
6.1. Micro Benchmark Checklist ...............................................................................................................47
II. Interprocess Communication................................................................................................................................49
7 Sockets .............................................................................................................................................................50
7.1 Synopsis...............................................................................................................................................50
7.2 Networking and Diversity....................................................................................................................50
7.3 Protocols ..............................................................................................................................................50
7.4 The Sockets Model ..............................................................................................................................53
7.5 Essential Socket Functions ..................................................................................................................53
7.6 Helper Functions..................................................................................................................................66
7.7 Concurrent Servers ..............................................................................................................................68
8 IPv6 Internals ...................................................................................................................................................71
8.1 IPv6/IPsec Implementation..................................................................................................................71
iv
III. Kernel ....................................................................................................................................................................88
9 Building and Installing a FreeBSD Kernel.......................................................................................................89
9.1 Building a Kernel the “Traditional” Way ............................................................................................89
9.2 Building a Kernel the “New” Way.......................................................................................................89
10 Kernel Debugging ..........................................................................................................................................90
10.1 Obtaining a Kernel Crash Dump .......................................................................................................90
10.2 Debugging a Kernel Crash Dump with
kgdb
....................................................................................91
10.3 Debugging a Crash Dump with DDD................................................................................................95
10.4 Post-Mortem Analysis of a Dump .....................................................................................................96
10.5 On-Line Kernel Debugging Using DDB ...........................................................................................96
10.6 On-Line Kernel Debugging Using Remote GDB..............................................................................99
10.7 Debugging Loadable Modules Using GDB.....................................................................................100
10.8 Debugging a Console Driver ...........................................................................................................101
10.9 Debugging Deadlocks......................................................................................................................101
10.10 Glossary of Kernel Options for Debugging ...................................................................................102
IV. Architectures.......................................................................................................................................................104
11 x86 Assembly Language Programming .......................................................................................................105
11.1 Synopsis...........................................................................................................................................105
11.2 The Tools .........................................................................................................................................105
11.3 System Calls ....................................................................................................................................106
11.4 Return Values...................................................................................................................................108
11.5 Creating Portable Code....................................................................................................................109
11.6 Our First Program ............................................................................................................................113
11.7 Writing UNIX® Filters....................................................................................................................114
11.8 Buffered Input and Output...............................................................................................................117
11.9 Command Line Arguments..............................................................................................................124
11.10 UNIX Environment........................................................................................................................128
11.11 Working with Files.........................................................................................................................132
11.12 One-Pointed Mind .........................................................................................................................143
11.13 Using the FPU................................................................................................................................151
11.14 Caveats...........................................................................................................................................178
11.15 Acknowledgements........................................................................................................................180
V. Appendices ............................................................................................................................................................181
Bibliography......................................................................................................................................................182
Index...........................................................................................................................................................................182
v
Zgłoś jeśli naruszono regulamin