Vysoke´Ucˇenítechnicke´V Brneˇ
Total Page:16
File Type:pdf, Size:1020Kb
VYSOKE´ UCˇ ENI´ TECHNICKE´ V BRNEˇ BRNO UNIVERSITY OF TECHNOLOGY FAKULTA INFORMACˇ NI´CH TECHNOLOGII´ U´ STAV INFORMACˇ NI´CH SYSTE´ MU˚ FACULTY OF INFORMATION TECHNOLOGY DEPARTMENT OF INFORMATION SYSTEMS PLUGINS FOR GETTING INFORMATION ABOUT THE SYSTEM FOR BUSYBOX BAKALA´ Rˇ SKA´ PRA´ CE BACHELOR’S THESIS AUTOR PRA´ CE MAREK POLA´ Cˇ EK AUTHOR BRNO 2011 VYSOKE´ UCˇ ENI´ TECHNICKE´ V BRNEˇ BRNO UNIVERSITY OF TECHNOLOGY FAKULTA INFORMACˇ NI´CH TECHNOLOGII´ U´ STAV INFORMACˇ NI´CH SYSTE´ MU˚ FACULTY OF INFORMATION TECHNOLOGY DEPARTMENT OF INFORMATION SYSTEMS PLUGINY PRO ZI´SKA´VA´ NI´ INFORMACI´ O SYSTE´ MU PRO PROJEKT BUSYBOX PLUGINS FOR GETTING INFORMATION ABOUT THE SYSTEM FOR BUSYBOX BAKALA´ Rˇ SKA´ PRA´ CE BACHELOR’S THESIS AUTOR PRA´ CE MAREK POLA´ Cˇ EK AUTHOR VEDOUCI´ PRA´ CE Doc. Ing. TOMA´ Sˇ VOJNAR, Ph.D. SUPERVISOR BRNO 2011 Abstrakt Tato práce se zabývá implementací nástrojù pro získávání informací o operačním systému pro projekt Busybox. Diskutovány jsou souborové systémy sysfs a procfs v operačním systému Linux. Dále se práce zabývá tím, jak vytváøet co nejmenší programy v jazyce C. Také se věnuje struktuøe programù iostat, mpstat a powertop. V rámci práce byly vytvoøeny minimalistické implementace již existujících nástrojù, zejména z balíku sysstat, který obsahuje například utility iostat a mpstat. Abstract In this thesis, we discuss implementation of tools for getting information from the system. We examine file systems sysfs and procfs in the Linux operating system. Furthermore, we discourse how to write small programs in the C language. Eventually, we take a look at implementation of tools like iostat, mpstat and powertop. These tools were implemented in a minimalistic form suitable for Busybox within this thesis. Klíčová slova Busybox, mpstat, iostat, powertop, sysfs, procfs, informace o systému, gcc optimalizace, Linux, jazyk C, pøekladaèe, knihovny, UNIXové utility. Keywords Busybox, mpstat, iostat, powertop, sysfs, procfs, system stats, gcc optimizations, Linux, C language, compilers, libraries, UNIX utilities. Citace Marek Poláèek: Plugins for Getting Information about the System for BusyBox, bakaláøská práce, Brno, FIT VUT v Brně, 2011 Plugins for Getting Information about the System for BusyBox Statement I hereby state that this thesis is my own work and effort. Where other sources of informa- tion have been used, they have been acknowledged. This thesis was elaborated under the supervision of doc. Ing. Tomá¹ Vojnar, Ph.D. ....................... Marek Poláèek May 13, 2011 Acknowledgements Above all, I would like to thank my colleagues from Red Hat, especially Denys Vlasenko, Petr Müller, and Ivana Vaøeková for their continuous professional and moral support. I would further like to thank very much doc. Ing. Tomá¹ Vojnar, Ph.D. for feedback. Without these people, I would have never finished this thesis. Thank you. c Marek Poláèek, 2011. Tato práce vznikla jako ¹kolní dílo na Vysokém učení technickém v Brně, Fakultě in- formačních technologií. Práce je chráněna autorským zákonem a její užití bez udělení oprávnění autorem je nezákonné, s výjimkou zákonem definovaných případů. Contents 1 Introduction3 1.1 Motivation....................................3 1.2 Goals.......................................3 1.3 Structure of the Thesis..............................4 2 Busybox 5 2.1 What It Is.....................................5 2.2 Design.......................................5 2.2.1 Applets..................................6 2.2.2 libbb ...................................7 2.2.3 NOFORK and NOEXEC Applets.......................7 2.3 A Closer Look at an Applet...........................8 2.3.1 Symbols Visibility............................9 2.3.2 The -fwhole-program Option ..................... 10 2.3.3 The regparm and stdcall Attributes................. 10 2.4 Obtaining Busybox................................ 11 2.5 How To Add a New Applet........................... 12 3 How to Keep Data Small 14 3.1 About Libraries.................................. 14 3.1.1 The GNU C Library........................... 14 3.1.2 Alternate C Libraries.......................... 15 3.2 Using Proper Data Types............................ 16 3.3 Eliminate Unnecessary Strings......................... 17 3.4 Reduce Stack Usage............................... 19 3.4.1 Passing Parameters to Functions.................... 19 3.4.2 Local Arrays............................... 20 3.5 Reduce Global Data Usage........................... 21 3.6 Factor Out Functions.............................. 23 3.7 Use Your Compiler and Linker Well...................... 23 3.7.1 GCC Optimization Options....................... 23 3.7.2 The -ffunction-sections Option................... 23 3.7.3 Data Alignment............................. 24 3.7.4 Inline Functions............................. 25 3.8 Getting Rid of Debug Information....................... 25 1 4 The procfs and sysfs File Systems 29 4.1 The procfs File System............................. 29 4.2 The sysfs File System.............................. 31 5 Busybox Plugins 33 5.1 The mpstat Plugin................................ 33 5.1.1 Modifications............................... 35 5.2 The iostat Plugin................................ 38 5.2.1 Modifications............................... 39 5.3 The powertop Utility.............................. 41 5.3.1 Modifications............................... 44 6 Conclusion 47 2 Chapter 1 Introduction Today, embedded devices are ubiquitous. We use mobile phones, PDAs, music players, cameras, printers, etc. Computer networking would never work without routers and bridges. New microwave ovens, smart fridges, and washing machines also include embedded devices to provide additional features and convenience. There are embedded devices in critical environments like hospitals, plains, trains, and cars. Consequently, their failures may cause serious problems. 1.1 Motivation Embedded devices of course run software. Developers write the code as they would write an ordinary application code. This is often not optimal. For embedded devices, some additional techniques must be applied to produce decent code. Some knowledge is necessary to write optimized code. Here, optimized mainly means \as minimal as possible". This is because of many microprocessors have only a small amount of available RAM memory, often have no hard disks to keep down the power consumption. However, there are other requirements too: The programs must also be stable and secure. Nobody would want to restart his router every other day. Often, it is desirable to not implement all the possible features but instead only pick the really needed ones. This means more straightforward and smaller code, which in turn decreases the number of bugs. 1.2 Goals The goal of this thesis is to provide a minimalist set of utilities for determining information about the operating system for the Busybox project. Subgoals of this main goal are to get acquainted with the philosophy and the structure of the Busybox, get familiar with various techniques how to write as small programs as possible and to summarize these techniques for further use. This requires good knowledge of available C libraries, compilers, assemblers, and linkers. Since the tools for getting information about the Linux operating system to a large extent use the sys/ and proc/ file systems, we need to examine these as well. Last but not least we need to acquaint ourselves with the existing implementation of the mpstat, iostat, and powertop tools. 3 1.3 Structure of the Thesis In this thesis, we will take a close look at one of the best-known open source projects called Busybox which is often used in various embedded devices and minimalist operating systems. In Chapter2, we will examine this project more thoroughly. Chapter3 speaks about how to keep data small. As we said, the size of programs which are intended to run on embedded devices is very important. So we will discuss what the programmer should always bear in mind when writing this kind of applications. Since, in this thesis, we are mainly interested in programs for getting information about the system, we need to examine the proc and sys file systems in detail because these are used heavily. Chapter4 is devoted to them. Chapter5 focuses on implementation of three tools which were created as a part of this thesis. These are: iostat (Section 5.2), mpstat (Section 5.1) and powertop (Section 5.3). In Chapter6, we close this thesis by a general summary. The achieved goals and future work will be discussed therein. 4 Chapter 2 Busybox This chapter discusses the Busybox project. First of all, we will introduce this project: what it actually is, what can it do for us and where it is used. Then we will speak about how to obtain, install, and configure Busybox. Furthermore, we will briefly talk about cross- compiling, which is a technique of creating executables for a different architecture than the one the compiler is currently run. This is often the one and only way for embedded platforms upon which it is not possible to run the compiler. At the end of the chapter, we will demonstrate how one can add a new applet into Busybox. 2.1 What It Is Busybox is very frequently described as \The Swiss Army Knife of Embedded Linux" [3]. This phrase is not chosen accidentally: Busybox is in fact one application that provides a set of standard UNIX utilities. It is meant to be as small as possible, therefore the utilities in Busybox have fewer options than the utilities from GNU Coreutils [10]. Nevertheless, the behavior of various