0%

Find solutions to all your problems related to Linux system programming using practical recipes for developing your own system programs

Key Features

  • Develop a deeper understanding of how Linux system programming works
  • Gain hands-on experience of working with different Linux projects with the help of practical examples
  • Learn how to develop your own programs for Linux

Book Description

Linux is the world's most popular open source operating system (OS). Linux System Programming Techniques will enable you to extend the Linux OS with your own system programs and communicate with other programs on the system.

The book begins by exploring the Linux filesystem, its basic commands, built-in manual pages, the GNU compiler collection (GCC), and Linux system calls. You'll then discover how to handle errors in your programs and will learn to catch errors and print relevant information about them. The book takes you through multiple recipes on how to read and write files on the system, using both streams and file descriptors. As you advance, you'll delve into forking, creating zombie processes, and daemons, along with recipes on how to handle daemons using systemd. After this, you'll find out how to create shared libraries and start exploring different types of interprocess communication (IPC). In the later chapters, recipes on how to write programs using POSIX threads and how to debug your programs using the GNU debugger (GDB) and Valgrind will also be covered.

By the end of this Linux book, you will be able to develop your own system programs for Linux, including daemons, tools, clients, and filters.

What you will learn

  • Discover how to write programs for the Linux system using a wide variety of system calls
  • Delve into the working of POSIX functions
  • Understand and use key concepts such as signals, pipes, IPC, and process management
  • Find out how to integrate programs with a Linux system
  • Explore advanced topics such as filesystem operations, creating shared libraries, and debugging your programs
  • Gain an overall understanding of how to debug your programs using Valgrind

Who this book is for

This book is for anyone who wants to develop system programs for Linux and gain a deeper understanding of the Linux system. The book is beneficial for anyone who is facing issues related to a particular part of Linux system programming and is looking for specific recipes or solutions.

Table of Contents

  1. Linux System Programming Techniques
  2. Contributors
  3. About the author
  4. About the reviewer
  5. Preface
    1. Who this book is for
    2. What this book covers
    3. To get the most out of this book
    4. Download the example code files
    5. Code in Action
    6. Download the color images
    7. Conventions used
    8. Sections
    9. Getting ready
    10. How to do it…
    11. How it works…
    12. There's more…
    13. See also
    14. Get in touch
    15. Reviews
  6. Chapter 1: Getting the Necessary Tools and Writing Our First Linux Programs
    1. Technical requirements
    2. Installing Git to download the code repository
    3. Installing GCC and GNU Make
    4. Getting ready
    5. How to do it…
    6. How it works…
    7. Installing GDB and Valgrind
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. Writing a simple C program for Linux
    12. Getting ready
    13. How to do it…
    14. How it works…
    15. There's more…
    16. Writing a program that parses command-line options
    17. Getting ready
    18. How to do it…
    19. How it works…
    20. Looking up information in the built-in manual page
    21. Getting ready
    22. How to do it…
    23. How it works…
    24. There's more…
    25. Searching the manual for information
    26. Getting ready
    27. How to do it…
    28. How it works…
    29. There's more…
  7. Chapter 2: Making Your Programs Easy to Script
    1. Technical requirements
    2. Return values and how to read them
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. There's more…
    7. See also
    8. Exiting a program with a relevant return value
    9. Getting ready
    10. How to do it…
    11. How it works…
    12. There's more…
    13. Redirecting stdin, stdout, and stderr
    14. Getting ready
    15. How to do it…
    16. How it works…
    17. There's more…
    18. Connecting programs using pipes
    19. Getting ready
    20. How to do it…
    21. How it works…
    22. There's more…
    23. See also
    24. Writing to stdout and stderr
    25. How to do it…
    26. How it works…
    27. There's more…
    28. Reading from stdin
    29. Getting ready
    30. How to do it…
    31. How it works…
    32. There's more…
    33. Writing a pipe-friendly program
    34. Getting ready
    35. How to do it…
    36. How it works…
    37. There's more…
    38. Redirecting the result to a file
    39. Getting ready
    40. How to do it…
    41. How it works…
    42. There's more…
    43. Reading environment variables
    44. Getting ready
    45. How to do it…
    46. How it works…
  8. Chapter 3: Diving Deep into C in Linux
    1. Technical requirements
    2. Linking against libraries using GCC
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. There's more…
    7. Changing C standards
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. There's more…
    12. Using system calls – and when not to use them
    13. Getting ready
    14. How to do it…
    15. How it works…
    16. There's more…
    17. Getting information about Linux- and Unix-specific header files
    18. Getting ready
    19. How to do it…
    20. How it works…
    21. There's more…
    22. Defining feature test macros
    23. Getting ready
    24. How to do it…
    25. How it works…
    26. There's more…
    27. Looking at the four stages of compilation
    28. Getting ready
    29. How to do it…
    30. How it works…
    31. Compiling with Make
    32. Getting ready
    33. How to do it…
    34. How it works…
    35. Writing a generic Makefile with GCC options
    36. Getting ready
    37. How to do it…
    38. How it works…
    39. There's more…
    40. Writing a simple Makefile
    41. Getting ready
    42. How to do it…
    43. How it works…
    44. Writing a more advanced Makefile
    45. Getting ready
    46. How to do it…
    47. How it works…
  9. Chapter 4: Handling Errors in Your Programs
    1. Technical requirements
    2. Why error handling is important in system programming
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Handling some common errors
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. Error handling and errno
    11. Getting ready
    12. How to do it…
    13. How it works…
    14. Handling more errno macros
    15. Getting ready
    16. How to do it…
    17. How it works…
    18. There's more…
    19. Using errno with strerror()
    20. Getting ready
    21. How to do it…
    22. There's more…
    23. Using errno with perror()
    24. Getting ready
    25. How to do it…
    26. There's more…
    27. Returning an error value
    28. Getting ready
    29. How to do it…
    30. How it works…
    31. There's more…
  10. Chapter 5: Working with File I/O and Filesystem Operations
    1. Technical requirements
    2. Reading inode information and learning the filesystem
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. Creating soft links and hard links
    7. Getting ready
    8. How to do it…
    9. How it works…
    10. There's more…
    11. Creating files and updating the timestamp
    12. Getting ready
    13. How to do it…
    14. How it works…
    15. There's more…
    16. Deleting files
    17. Getting ready
    18. How to do it…
    19. How it works…
    20. Getting access rights and ownership
    21. Getting ready
    22. How to do it…
    23. How it works…
    24. Setting access rights and ownership
    25. Getting ready
    26. How to do it…
    27. How it works…
    28. There's more…
    29. Writing to files with file descriptors
    30. Getting ready
    31. How to do it…
    32. How it works…
    33. There's more…
    34. Reading from files with file descriptors
    35. Getting ready
    36. How to do it…
    37. How it works…
    38. There's more…
    39. Writing to files with streams
    40. Getting ready
    41. How to do it…
    42. How it works…
    43. See also
    44. Reading from files with streams
    45. Getting ready
    46. How to do it…
    47. How it works…
    48. There's more…
    49. Reading and writing binary data with streams
    50. Getting ready
    51. How to do it…
    52. How it works…
    53. There's more…
    54. Moving around inside a file with lseek()
    55. Getting ready
    56. How to do it…
    57. How it works…
    58. Moving around inside a file with fseek()
    59. Getting ready
    60. How to do it…
    61. How it works…
    62. There's more…
  11. Chapter 6: Spawning Processes and Using Job Control
    1. Technical requirements
    2. Exploring how processes are created
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. There's more…
    7. Using job control in Bash
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. Controlling and terminating processes using signals
    12. Getting ready
    13. How to do it…
    14. How it works…
    15. See also
    16. Replacing the program in a process with execl()
    17. Getting ready
    18. How to do it…
    19. How it works…
    20. See also
    21. Forking a process
    22. Getting ready
    23. How to do it…
    24. How it works…
    25. There's more…
    26. Executing a new program in a forked process
    27. Getting ready
    28. How to do it…
    29. How it works…
    30. Starting a new process with system()
    31. Getting ready
    32. How to do it…
    33. How it works…
    34. Creating a zombie process
    35. Getting ready
    36. How to do it…
    37. How it works…
    38. There's more…
    39. Learning about what orphans are
    40. Getting ready
    41. How to do it…
    42. How it works…
    43. See also
    44. Creating a daemon
    45. Getting ready
    46. How to do it…
    47. How it works…
    48. There's more…
    49. Implementing a signal handler
    50. Getting ready
    51. How to do it…
    52. How it works…
    53. There's more…
  12. Chapter 7: Using systemd to Handle Your Daemons
    1. Technical requirements
    2. Getting to know systemd
    3. Getting ready
    4. How it works...
    5. There's more...
    6. See also
    7. Writing a unit file for a daemon
    8. Getting ready
    9. How to do it...
    10. How it works...
    11. There's more...
    12. See also
    13. Enabling and disabling a service – and starting and stopping it
    14. Getting ready
    15. How to do it...
    16. How it works...
    17. Creating a more modern daemon for systemd
    18. Getting ready
    19. How to do it...
    20. How it works...
    21. See also
    22. Making the new daemon a systemd service
    23. Getting ready
    24. How to do it...
    25. How it works...
    26. Reading the journal
    27. Getting ready
    28. How to do it...
    29. How it works...
    30. See also
  13. Chapter 8: Creating Shared Libraries
    1. Technical requirements
    2. The what and why of libraries
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. There's more…
    7. Creating a static library
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. See also
    12. Using a static library
    13. Getting ready
    14. How to do it…
    15. How it works…
    16. Creating a dynamic library
    17. Getting ready
    18. How to do it…
    19. How it works…
    20. There's more…
    21. See also
    22. Installing the dynamic library on the system
    23. Getting ready
    24. How to do it…
    25. How it works…
    26. Using the dynamic library in a program
    27. Getting ready
    28. How to do it…
    29. How it works…
    30. See also
    31. Compiling a statically linked program
    32. Getting ready
    33. How to do it…
    34. How it works…
  14. Chapter 9: Terminal I/O and Changing Terminal Behavior
    1. Technical requirements
    2. Viewing terminal information
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. See also
    7. Changing terminal settings with stty
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. Investigating TTYs and PTYs and writing to them
    12. How to do it…
    13. How it works…
    14. There's more…
    15. See also
    16. Checking if it's a TTY
    17. Getting ready
    18. How to do it…
    19. How it works…
    20. See also
    21. Creating a PTY
    22. Getting ready
    23. How to do it…
    24. How it works…
    25. See also
    26. Disabling echo for password prompts
    27. Getting ready
    28. How to do it…
    29. How it works…
    30. There's more…
    31. Reading the terminal size
    32. Getting ready
    33. How to do it…
    34. How it works…
    35. There's more…
    36. See also
  15. Chapter 10: Using Different Kinds of IPC
    1. Technical requirements
    2. Using signals for IPC – building a client for the daemon
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. See also
    7. Communicating with a pipe
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. See also
    12. FIFO – using it in the shell
    13. Getting ready
    14. How to do it…
    15. How it works…
    16. See also
    17. FIFO – building the sender
    18. Getting ready
    19. How to do it…
    20. How it works…
    21. See also
    22. FIFO – building the receiver
    23. Getting ready
    24. How to do it…
    25. How it works…
    26. See also
    27. Message queues – creating the sender
    28. Getting ready
    29. How to do it…
    30. How it works…
    31. See also
    32. Message queues – creating the receiver
    33. Getting ready
    34. How to do it…
    35. How it works…
    36. See also
    37. Communicating between child and parent with shared memory
    38. Getting ready
    39. How to do it…
    40. How it works…
    41. See also
    42. Using shared memory between unrelated processes
    43. Getting ready
    44. How to do it…
    45. How it works…
    46. See also
    47. Unix socket – creating the server
    48. Getting ready
    49. How to do it…
    50. How it works…
    51. See also
    52. Unix socket – creating the client
    53. Getting ready
    54. How to do it…
    55. How it works…
    56. See also
  16. Chapter 11: Using Threads in Your Programs
    1. Technical requirements
    2. Writing your first threaded program
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. There's more…
    7. See also
    8. Reading return values from threads
    9. Getting ready
    10. How to do it…
    11. How it works…
    12. Causing a race condition
    13. Getting ready
    14. How to do it…
    15. How it works…
    16. Avoiding race conditions with mutexes
    17. Getting ready
    18. How to do it…
    19. How it works...
    20. See also
    21. Making the mutex program more efficient
    22. Getting ready
    23. How to do it…
    24. How it works…
    25. Using condition variables
    26. Getting ready
    27. How it works…
    28. See also
  17. Chapter 12: Debugging Your Programs
    1. Technical requirements
    2. Starting GDB
    3. Getting ready
    4. How to do it…
    5. How it works…
    6. There's more…
    7. Stepping inside a function with GDB
    8. Getting ready
    9. How to do it…
    10. How it works…
    11. Investigating memory with GDB
    12. Getting ready
    13. How to do it…
    14. How it works…
    15. There's more...
    16. See also
    17. Modifying variables during runtime
    18. Getting ready
    19. How to do it…
    20. How it works…
    21. Using GDB on a program that forks
    22. Getting ready
    23. How to do it…
    24. How it works…
    25. There's more…
    26. Debugging programs with multiple threads
    27. Getting ready
    28. How to do it…
    29. How it works…
    30. There's more…
    31. See also
    32. Finding a simple memory leak with Valgrind
    33. Getting started
    34. How to do it…
    35. How it works…
    36. See also
    37. Finding buffer overflows with Valgrind
    38. Getting ready
    39. How to do it…
    40. How it works…
    41. There's more...
    42. Why subscribe?
  18. Other Books You May Enjoy
    1. Packt is searching for authors like you
    2. Leave a review - let other readers know what you think
18.117.186.92