100% found this document useful (2 votes)
6 views

Software Development with Go: Cloud-Native Programming using Golang with Linux and Docker Nanik Tolaram pdf download

The document is a promotional and informational piece for the book 'Software Development with Go: Cloud-Native Programming using Golang with Linux and Docker' by Nanik Tolaram. It includes details about the book's content, structure, and additional resources for related eBooks. The document also provides links for purchasing and downloading the book and other related titles.

Uploaded by

erdincrisdan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
6 views

Software Development with Go: Cloud-Native Programming using Golang with Linux and Docker Nanik Tolaram pdf download

The document is a promotional and informational piece for the book 'Software Development with Go: Cloud-Native Programming using Golang with Linux and Docker' by Nanik Tolaram. It includes details about the book's content, structure, and additional resources for related eBooks. The document also provides links for purchasing and downloading the book and other related titles.

Uploaded by

erdincrisdan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 72

Software Development with Go: Cloud-Native

Programming using Golang with Linux and Docker


Nanik Tolaram install download

https://ebookmeta.com/product/software-development-with-go-cloud-
native-programming-using-golang-with-linux-and-docker-nanik-
tolaram/

Download more ebook from https://ebookmeta.com


We believe these products will be a great fit for you. Click
the link to download now, or visit ebookmeta.com
to discover even more!

Software Development with Go Cloud Native Programming


using Golang with Linux and Docker 1st Edition Nanik
Tolaram

https://ebookmeta.com/product/software-development-with-go-cloud-
native-programming-using-golang-with-linux-and-docker-1st-
edition-nanik-tolaram/

Network Programming with Go Language: Essential Skills


for Programming, Using and Securing Networks with Open
Source Google Golang 2nd Edition Jan Newmarch

https://ebookmeta.com/product/network-programming-with-go-
language-essential-skills-for-programming-using-and-securing-
networks-with-open-source-google-golang-2nd-edition-jan-
newmarch-2/

Network Programming with Go Language : Essential Skills


for Programming, Using and Securing Networks with Open
Source Google Golang 2nd Edition Jan Newmarch

https://ebookmeta.com/product/network-programming-with-go-
language-essential-skills-for-programming-using-and-securing-
networks-with-open-source-google-golang-2nd-edition-jan-newmarch/

Fit for a Duke Dangerous Dukes 10 1st Edition Wendy


Soliman

https://ebookmeta.com/product/fit-for-a-duke-dangerous-
dukes-10-1st-edition-wendy-soliman/
Going Global Transnational Perspectives on
Globalization Language and Education 1st Edition Leslie
Seawright

https://ebookmeta.com/product/going-global-transnational-
perspectives-on-globalization-language-and-education-1st-edition-
leslie-seawright/

PHP and MySQL 174 Web Development Luke Welling

https://ebookmeta.com/product/php-and-mysql-174-web-development-
luke-welling/

If Looks Could Kill 80 s Baby Series 2 1st Edition


Tamrin Banks

https://ebookmeta.com/product/if-looks-could-kill-80-s-baby-
series-2-1st-edition-tamrin-banks/

Her Special Alpha Hot SEALs X OPS 3 5 Paige Tyler

https://ebookmeta.com/product/her-special-alpha-hot-seals-x-
ops-3-5-paige-tyler/

Ours to Share 1st Edition Ar Taboo

https://ebookmeta.com/product/ours-to-share-1st-edition-ar-taboo/
Brand Strategy in Three Steps: A Purpose-Driven
Approach to Branding 1st Edition Jay Mandel

https://ebookmeta.com/product/brand-strategy-in-three-steps-a-
purpose-driven-approach-to-branding-1st-edition-jay-mandel/
Software Development
with Go
Cloud-Native Programming
using Golang with Linux
and Docker

Nanik Tolaram
Software Development with Go: Cloud-Native Programming using Golang
with Linux and Docker

Nanik Tolaram
Sydney, NSW, Australia

ISBN-13 (pbk): 978-1-4842-8730-9 ISBN-13 (electronic): 978-1-4842-8731-6


https://doi.org/10.1007/978-1-4842-8731-6
Copyright © 2023 by Nanik Tolaram
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or
part of the material is concerned, specifically the rights of translation, reprinting, reuse of
illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way,
and transmission or information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark
symbol with every occurrence of a trademarked name, logo, or image we use the names, logos,
and images only in an editorial fashion and to the benefit of the trademark owner, with no
intention of infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if
they are not identified as such, is not to be taken as an expression of opinion as to whether or not
they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of
publication, neither the authors nor the editors nor the publisher can accept any legal
responsibility for any errors or omissions that may be made. The publisher makes no warranty,
express or implied, with respect to the material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: James Robinson-Prior
Development Editor: James Markham
Coordinating Editor: Gryffin Winkler
Copy Editor: Mary Behr
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1
New York Plaza, Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201)
348-4505, e-mail orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media,
LLC is a California LLC and the sole member (owner) is Springer Science + Business Media
Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for
reprint, paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook
versions and licenses are also available for most titles. For more information, reference our Print
and eBook Bulk Sales web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is
available to readers on GitHub (https://github.com/Apress). For more detailed information,
please visit www.apress.com/source-code.
Printed on acid-free paper
I would like to dedicate this book to my late Dad who stood
by me and encouraged me to write my very first book when
I was 17 years old. To my dearest Mum who always
supported me in pursuing my dreams and encouraged me
to keep on going no matter what life brings. To my beautiful
wife and best friend for allowing me the time to write the
book and supporting me in every step of our life. To both my
sons, Rahul and Manav, for allowing me to spend time in
front of the computer on weekends to chase my dream and
passion. Last but not least, to God for giving me this life and
opportunity to be where I am in this world.
Table of Contents
About the Author�������������������������������������������������������������������������������xiii

About the Technical Reviewer������������������������������������������������������������xv

Acknowledgments����������������������������������������������������������������������������xvii

Introduction���������������������������������������������������������������������������������������xix

Part I: System Programming������������������������������������������������1


Chapter 1: System Calls�����������������������������������������������������������������������3
Source Code����������������������������������������������������������������������������������������������������������3
What Is a System Call?�����������������������������������������������������������������������������������������4
C System Call��������������������������������������������������������������������������������������������������������5
sys/unix Package��������������������������������������������������������������������������������������������������8
System Call in Go������������������������������������������������������������������������������������������������������� 10
Summary������������������������������������������������������������������������������������������������������������14

Chapter 2: System Calls Using Go������������������������������������������������������15


Source Code��������������������������������������������������������������������������������������������������������15
Syscall Package��������������������������������������������������������������������������������������������������16
syscall Application����������������������������������������������������������������������������������������������������� 16
Checking Disk Space������������������������������������������������������������������������������������������������� 18
Webserver with syscall���������������������������������������������������������������������������������������������� 20

v
Table of Contents

ELF Package�������������������������������������������������������������������������������������������������������22
High-Level ELF Format���������������������������������������������������������������������������������������������� 23
Dump Example���������������������������������������������������������������������������������������������������������� 24

/sys Filesystem���������������������������������������������������������������������������������������������������28
Reading AppArmor����������������������������������������������������������������������������������������������������� 29

Summary������������������������������������������������������������������������������������������������������������31

Chapter 3: Accessing proc File System����������������������������������������������33


Source Code��������������������������������������������������������������������������������������������������������33
Peeking Inside procfs������������������������������������������������������������������������������������������34
Reading Memory Information�����������������������������������������������������������������������������38
Peeking Network Information������������������������������������������������������������������������������������ 41

Using the procfs Library��������������������������������������������������������������������������������������44


Code Sample������������������������������������������������������������������������������������������������������������� 44
Inside the procfs Library�������������������������������������������������������������������������������������������� 46

Summary������������������������������������������������������������������������������������������������������������48

Part II: Containers���������������������������������������������������������������49


Chapter 4: Simple Containers�������������������������������������������������������������51
Linux Namespace������������������������������������������������������������������������������������������������51
cgroups���������������������������������������������������������������������������������������������������������������55
rootfs�������������������������������������������������������������������������������������������������������������������58
Gontainer Project������������������������������������������������������������������������������������������������63
Summary������������������������������������������������������������������������������������������������������������70

vi
Table of Contents

Chapter 5: Containers with Networking���������������������������������������������71


Source Code��������������������������������������������������������������������������������������������������������71
Network Namespace������������������������������������������������������������������������������������������������� 72
Setting Up with the ip Tool����������������������������������������������������������������������������������������� 74
Containers with Networks����������������������������������������������������������������������������������������� 79
Summary������������������������������������������������������������������������������������������������������������������� 88

Chapter 6: Docker Security�����������������������������������������������������������������89


Source Code��������������������������������������������������������������������������������������������������������89
seccomp Profiles������������������������������������������������������������������������������������������������89
libseccomp���������������������������������������������������������������������������������������������������������������� 91
Docker seccomp�������������������������������������������������������������������������������������������������������� 95

Docker Proxy�������������������������������������������������������������������������������������������������������98
Container Attack Surface����������������������������������������������������������������������������������105
Summary����������������������������������������������������������������������������������������������������������106

Part III: Application Security��������������������������������������������109


Chapter 7: Gosec and AST����������������������������������������������������������������111
Source Code������������������������������������������������������������������������������������������������������111
Abstract Syntax Tree�����������������������������������������������������������������������������������������111
Modules������������������������������������������������������������������������������������������������������������������� 115
Sample Code����������������������������������������������������������������������������������������������������������� 116

gosec����������������������������������������������������������������������������������������������������������������122
Inside gosec������������������������������������������������������������������������������������������������������������ 123
Rules������������������������������������������������������������������������������������������������������������������������ 128

Summary����������������������������������������������������������������������������������������������������������130

vii
Table of Contents

Chapter 8: Scorecard������������������������������������������������������������������������131
Source Code������������������������������������������������������������������������������������������������������131
What Is Scorecard?�������������������������������������������������������������������������������������������131
Setting Up Scorecard����������������������������������������������������������������������������������������������� 133
Running Scorecard�������������������������������������������������������������������������������������������������� 137
High-Level Flow������������������������������������������������������������������������������������������������������� 139

GitHub���������������������������������������������������������������������������������������������������������������145
GitHub API���������������������������������������������������������������������������������������������������������������� 145
GitHub Explorer�������������������������������������������������������������������������������������������������������� 156

Summary����������������������������������������������������������������������������������������������������������159

Part IV: Networking����������������������������������������������������������161


Chapter 9: Simple Networking����������������������������������������������������������163
Source Code������������������������������������������������������������������������������������������������������163
TCP Networking������������������������������������������������������������������������������������������������163
TCP Client���������������������������������������������������������������������������������������������������������������� 164
TCP Server��������������������������������������������������������������������������������������������������������������� 166

UDP Networking������������������������������������������������������������������������������������������������168
UDP Client���������������������������������������������������������������������������������������������������������������� 169
UDP Server�������������������������������������������������������������������������������������������������������������� 172
Concurrent Servers�������������������������������������������������������������������������������������������������� 174

Load Testing������������������������������������������������������������������������������������������������������175
Summary����������������������������������������������������������������������������������������������������������179

Chapter 10: System Networking�������������������������������������������������������181


Source Code������������������������������������������������������������������������������������������������������181
Ping Utility���������������������������������������������������������������������������������������������������������181
Code Walkthrough��������������������������������������������������������������������������������������������������� 182

viii
Table of Contents

DNS Server��������������������������������������������������������������������������������������������������������188
Running a DNS Server��������������������������������������������������������������������������������������������� 188
DNS Forwarder�������������������������������������������������������������������������������������������������������� 189
Pack and Unpack����������������������������������������������������������������������������������������������������� 193
Summary����������������������������������������������������������������������������������������������������������196

Chapter 11: Google gopacket������������������������������������������������������������199


Source Code������������������������������������������������������������������������������������������������������199
gopacket�����������������������������������������������������������������������������������������������������������200
Layer������������������������������������������������������������������������������������������������������������������������ 200
Packet���������������������������������������������������������������������������������������������������������������������� 204

Using gopacket�������������������������������������������������������������������������������������������������205
pcap������������������������������������������������������������������������������������������������������������������������� 205
Networking Sniffer�������������������������������������������������������������������������������������������������� 206
Capturing With BPF�������������������������������������������������������������������������������������������������� 217

Summary����������������������������������������������������������������������������������������������������������222

Chapter 12: Epoll Library������������������������������������������������������������������223


Source Code������������������������������������������������������������������������������������������������������224
Understanding epoll������������������������������������������������������������������������������������������224
epoll in Golang��������������������������������������������������������������������������������������������������226
Epoll Registration���������������������������������������������������������������������������������������������������� 227
Epoll Wait����������������������������������������������������������������������������������������������������������������� 229

Epoll Library������������������������������������������������������������������������������������������������������232
Summary����������������������������������������������������������������������������������������������������������235

ix
Table of Contents

Part V: Securing Linux������������������������������������������������������239


Chapter 13: Vulnerability Scanner����������������������������������������������������239
Source Code������������������������������������������������������������������������������������������������������239
Vulnerability Scanners��������������������������������������������������������������������������������������240
Using Vuls���������������������������������������������������������������������������������������������������������241
Checking Out the Code�������������������������������������������������������������������������������������������� 241
Running Scan���������������������������������������������������������������������������������������������������������� 243

Learning From Vuls�������������������������������������������������������������������������������������������248


Port Scan����������������������������������������������������������������������������������������������������������������� 248
Exec������������������������������������������������������������������������������������������������������������������������� 255
SQLite���������������������������������������������������������������������������������������������������������������������� 257

Summary����������������������������������������������������������������������������������������������������������263

Chapter 14: CrowdSec����������������������������������������������������������������������265


Source Code������������������������������������������������������������������������������������������������������265
CrowdSec Project����������������������������������������������������������������������������������������������266
Using CrowdSec������������������������������������������������������������������������������������������������������ 266
crowdsec.db������������������������������������������������������������������������������������������������������������ 270

Learning From CrowdSec���������������������������������������������������������������������������������273


System Signal Handling������������������������������������������������������������������������������������������� 274
Handling Service Dependencies������������������������������������������������������������������������������ 280
GeoIP Database������������������������������������������������������������������������������������������������������� 285

Summary����������������������������������������������������������������������������������������������������������290

x
Table of Contents

Part VI: Terminal User Interface���������������������������������������293


Chapter 15: ANSI and UI�������������������������������������������������������������������293
Source Code������������������������������������������������������������������������������������������������������293
ANSI Escape Code���������������������������������������������������������������������������������������������294
ANSI-Based UI���������������������������������������������������������������������������������������������������296
Color Table��������������������������������������������������������������������������������������������������������������� 296
Styling Text�������������������������������������������������������������������������������������������������������������� 299

Open Source Library�����������������������������������������������������������������������������������������300


Gookit���������������������������������������������������������������������������������������������������������������������� 300
Spinner�������������������������������������������������������������������������������������������������������������������� 303

Summary����������������������������������������������������������������������������������������������������������306

Chapter 16: TUI Framework��������������������������������������������������������������307


uiprogress���������������������������������������������������������������������������������������������������������307
Code Flow���������������������������������������������������������������������������������������������������������������� 309
Updating Progress��������������������������������������������������������������������������������������������������� 310

Bubbletea����������������������������������������������������������������������������������������������������������313
Init��������������������������������������������������������������������������������������������������������������������������� 315
Update��������������������������������������������������������������������������������������������������������������������� 318
View������������������������������������������������������������������������������������������������������������������������� 319

Summary����������������������������������������������������������������������������������������������������������321

Part VII: Linux System������������������������������������������������������325


Chapter 17: systemd�������������������������������������������������������������������������325
Source Code������������������������������������������������������������������������������������������������������325
systemd������������������������������������������������������������������������������������������������������������������� 325
systemctl����������������������������������������������������������������������������������������������������������������� 329
Hello Server systemd���������������������������������������������������������������������������������������������� 332

xi
Table of Contents

go-systemd Library������������������������������������������������������������������������������������������������� 334


Summary����������������������������������������������������������������������������������������������������������������� 345

Chapter 18: cadvisor������������������������������������������������������������������������347


Source Code������������������������������������������������������������������������������������������������������347
Running cAdvisor����������������������������������������������������������������������������������������������347
Web User Interface��������������������������������������������������������������������������������������������352
Architecture������������������������������������������������������������������������������������������������������355
Initialization�������������������������������������������������������������������������������������������������������357
Manager������������������������������������������������������������������������������������������������������������361
Monitoring Filesystem��������������������������������������������������������������������������������������368
Information from /sys and /proc�����������������������������������������������������������������������371
Client Library�����������������������������������������������������������������������������������������������������375
Summary����������������������������������������������������������������������������������������������������������375

Index�������������������������������������������������������������������������������������������������377

xii
About the Author
Nanik Tolaram is a big proponent of open source software with over 20
years of industry experience. He has dabbled in different programming
languages like Java, JavaScript, C, and C++. He has developed different
products from the ground up while working in start-up companies. He is
a software engineer at heart, but he loves to write technical articles and
share his knowledge with others. He learned to program with Go during
the COVID-19 pandemic and hasn’t looked back.

xiii
About the Technical Reviewer
Fabio Claudio Ferracchiati is a senior consultant and a senior
analyst/developer using Microsoft technologies. He works for BluArancio
(www.bluarancio.com). He is a Microsoft Certified Solution Developer for
.NET, a Microsoft Certified Application Developer for .NET, a Microsoft
Certified Professional, and a prolific author and technical reviewer.
Over the past ten years, he’s written articles for Italian and international
magazines and coauthored more than ten books on a variety of
computer topics.

xv
Acknowledgments
Thanks to everyone on the Apress team who helped and guided me so
much. Special thanks to James Robinson-Prior who guided me through
the writing process and to Nirmal Selvaraj who made sure everything was
done correctly and things were on track.
Thanks to the technical reviewers for taking time from their busy
schedules to review my book and provide great feedback.
Finally, thanks to you, the reader, for spending time reading this book
and spreading the love of Go.

xvii
Introduction
Go has been out for more than 10 years, and open source projects were
developed using Go. The aim of this book is to show you the way to use Go
to write a variety of applications that are useful in cloud-based systems.
Deploying applications into the cloud is a normal process that
developers do every day. There are many questions that developers ask
themselves about the cloud, like

• How do containers work in a cloud environment?

• How do cloud monitoring applications knows how


much memory is left for my virtual machines?

• How can I build a high performance networking server


in Linux environment?

• How do I scan code before deploying to the cloud


to stop code deployment if it contains related
information?

and many other cloud-relevant questions.


The book talk about different topics that are relevant in today’s cloud
environment. The approach is to explain each topic at a high level and
then help you understand it by going through the details with the code.
The book uses combination of open source projects hosted in GitHub and
sample code. The open source projects chosen are relevant to the topic.
You will get a good grasp about the tool and also how the code works
internally.

xix
CHAPTER 1

System Calls
Linux provides a lot of features and provides applications access to
everything that the operating system has access to. When discussing
system calls, most people will turn their attention to using C because
it is the most common language to use when interfacing with the
operating system.
In this chapter, you will explore what system calls are and how you can
program in Go to make system calls. By the end of this chapter, you will
have learned the following:

• What a system call looks like in C

• Understanding the sys/unix Go package

• Exploring a project using system calls

If you are using Go for the first time, refer to the online documentation
at https://go.dev/doc/install. The online documentation will walk you
through the steps to install Go on your local computer. Go through the Go
tutorial that the Go documentation provides at https://go.dev/doc/.

Source Code
The source code for this chapter is available from the https://github.
com/Apress/Software-Development-Go repository.

© Nanik Tolaram 2023 3


N. Tolaram, Software Development with Go,
https://doi.org/10.1007/978-1-4842-8731-6_1
Chapter 1 System Calls

What Is a System Call?


A system call is the interface provided by the underlying operating system
that your application is currently running on. Using this interface, your
application can communicate with the operating system to perform an
operation. In general, the operating system provides numerous services
that applications can take advantage of.
Figure 1-1 shows at a high level how an application uses system calls
to request some service operation to the operating system. The user
app will make a call to the provided system library, which in this case is
the Go library, and it will call the operating system service through the
provided interface. Data transfer flows in both directions for the different
components.

Figure 1-1. High-level view of a system call

Operating systems provide a large number of system calls that


applications can use. Figure 1-2 shows a snapshot list of system calls. For a
complete available Linux system call list, you can visit ­https://man7.org/
linux/man-pages/man2/syscalls.2.html.

4
Chapter 1 System Calls

Figure 1-2. Snapshot of a Linux system call

C System Call
In this section, you will briefly look at how system calls normally work
inside a C program. This will give you an idea of how system calls are done
in C compared to how they are done in Go.
You will see a simple example of using a socket to connect to a server
and read the response. The code can be found inside the chapter1/c
directory. The code creates a socket and uses it to connect to a public
website named httpbin.org and print the response it receives to the
screen. Listing 1-1 shows the sample code.

Listing 1-1. Sample Code

#include<stdio.h>
#include<string.h>
#include<sys/socket.h>
#include<arpa/inet.h>

5
Chapter 1 System Calls

#include<netdb.h>

int main(int argc, char * argv[]) {


int socket_desc;
struct sockaddr_in server;
char * message, server_reply[2000];
struct hostent * host;
const char * hostname = "httpbin.org";
//Create socket
socket_desc = socket(AF_INET, SOCK_STREAM, 0);
if (socket_desc == -1) {
   printf("Could not create socket");
}

if ((server.sin_addr.s_addr = inet_addr(hostname)) ==


0xffffffff) {
   if ((host = gethostbyname(hostname)) == NULL) {
     return -1;
   }

   memcpy( & server.sin_addr, host -> h_addr, host ->


h_length);
}

server.sin_family = AF_INET;
server.sin_port = htons(80);

if (connect(socket_desc, (struct sockaddr * ) & server,


sizeof(server)) < 0) {
   puts("connect error");
   return 1;
}
puts("Connected\n");
//Send some data

6
Chapter 1 System Calls

message = "GET / HTTP/1.0\n\n";


if (send(socket_desc, message, strlen(message), 0) < 0) {
   puts("Send failed");
   return 1;
}
puts("Data Send\n");
//Receive a reply from the server
if (recv(socket_desc, server_reply, 2000, 0) < 0) {
   puts("recv failed");
}
puts("Reply received\n");
puts(server_reply);
return 0;
}

To test the code, make sure you have a C compiler installed in your
machine. Follow the instructions outlined on the GCC website to install
the compiler and tools (https://gcc.gnu.org/). Use the following
command to compile the code:

cc sample.c -o sample

The code will be compiled to an executable named sample, and it can


be run by just typing ./sample on the command line. After a successful
run, it will print out the following:

Connected

Data Send

Reply received

HTTP/1.1 200 OK

7
Chapter 1 System Calls

Date: Tue, 01 Mar 2022 10:21:13 GMT


Content-Type: text/html; charset=utf-8
Content-Length: 9593
Connection: close
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true

The code sample shows the system call that it uses to resolve the
address of httpbin.org to an IP address by using the gethostbyname
function. It also uses the connect function to use the newly created socket
to connect to the server.
In the next section, you will start exploring Go by using the standard
library to write code using system calls.

sys/unix Package
The sys/unix package is a package provided by the Go language that
provides a system-level interface to interact with the operating system. Go
can run on a variety of operating systems, which means that it provides
different interfaces to applications for different operating systems.
Complete package documentation can be found at https://pkg.go.dev/
golang.org/x/sys/unix. Figure 1-3 shows different system calls in
different operating systems, in this case between Darwin and Linux.

8
Chapter 1 System Calls

Figure 1-3. System calls in Linux vs. Darwin

Listing 1-2 shows how to use system calls using the sys/unix package.

Listing 1-2. Go System Call

package main

import (
  u "golang.org/x/sys/unix"
  "log"
)

func main() {
  c := make([]byte, 512)

  log.Println("Getpid : ", u.Getpid())


  log.Println("Getpgrp : ", u.Getpgrp())
  log.Println("Getppid : ", u.Getppid())
  log.Println("Gettid : ", u.Gettid())

  _, err := u.Getcwd(c)

9
Chapter 1 System Calls

  if err != nil {


     log.Fatalln(err)
  }

  log.Println(string(c))
}

The code prints out information that it obtained by calling the


following system calls:

Getpid Obtains the process id of the current running sample


app
Getpgrp Obtains the group process id of the current running
app
Getppid Obtains the parent process id of the current running
app
Gettid Obtains the caller’s thread it

Running the app on a Linux machine will result in output something


like the following:

2022/02/19 21:25:59 Getpid :  12057


2022/02/19 21:25:59 Getpgrp :  12057
2022/02/19 21:25:59 Getpgrp :  29162
2022/02/19 21:25:59 Gettid :  12057
2022/02/19 21:25:59 /home/nanik/

The other system call that the application uses is to get the current
working directory using the Getcwd function.

System Call in Go
In the previous section, you looked at a simple example of using the sys/
unix package. In this section, you will explore more on system calls by

10
Chapter 1 System Calls

looking at an open source project. The project can be found at https://


github.com/tklauser/statx. This project works similarly to the stat
command in Linux for printing out statistical information about a
particular file.
Change your directory to the statx project and compile and run the
app as follows:

go run statx.go ./README.md

You will see output as follows:

  File: ./README.md
  Size: 476                Blocks: 8        
IO Block: 4096   regular file
Device: fd01h/64769d      Inode: 2637168   
Links:    1
Access: (0644/-rw-r--r--) Uid:    (1000/     nanik)  
Gid: (1000/   nanik)
Access: 2022-02-19 18:10:29.919351223 +1100 AEDT
Modify: 2022-02-19 18:10:29.919351223 +1100 AEDT
Change: 2022-02-19 18:10:29.919351223 +1100 AEDT
Birth: 2022-02-19 18:10:29.919351223 +1100 AEDT
Attrs: 0000000000000000 (-----....)

How does the application get all this information about the file? It
obtains the information from the operating system by making a system
call. Let's take a look at the code in Listing 1-3.

Listing 1-3. Code Using statx

import (
   ....

   "golang.org/x/sys/unix"
)

11
Chapter 1 System Calls

   ....
func main() {
   log.SetFlags(0)
   flag.Parse()

   if len(flag.Args()) < 1 {


       flag.Usage()
       os.Exit(1)
   }
   ....
   for _, arg := range flag.Args() {
       var statx unix.Statx_t
       if err := unix.Statx(unix.AT_FDCWD, arg, flags, mask,
&statx); err != nil {
   ....
       dev := unix.Mkdev(statx.Dev_major, statx.Dev_minor)
   ....
}

As seen in the snippet, the application uses a unix.Statx system


call and it passes filename and other relevant arguments. The system
call is provided as part of the golang.org/x/sys/unix package, which is
declared as follows:

func Statx(dirfd int, path string, flags int, mask int,


stat *Statx_t) (err error)

Declaration and documentation of the Statx function system call can


be found in the following link: https://pkg.go.dev/golang.org/x/sys/
unix. Going through the documentation, there is not much information
about the parameters. As an alternative, you can take a look at the same
system call defined for Linux, which can be found at https://man7.org/
linux/man-pages/man2/statx.2.html. Figure 1-4 shows information about
the different parameters that the function call accepts and what they mean.

12
Chapter 1 System Calls

Figure 1-4. Linux statx

On successful return from calling the unix.Statx function, the


application processes the information that is inside the statx variable
to extract information. The variable is of type Statx_t, which is defined
as follows in the sys/unix package. The struct contains a fair amount of
data pertaining to the file that the application has access to. Using this
information, the application will print out information such as file size,
type of file, user id, and group id.

13
Chapter 1 System Calls

type Statx_t struct {


  Mask            uint32
  Blksize         uint32
  Attributes      uint64
  Nlink           uint32
  Uid             uint32
  Gid             uint32
  Mode            uint16
  _               [1]uint16
  Ino             uint64
  Blocks          uint64
  Attributes_mask uint64
  Atime           StatxTimestamp
  ...
  Dev_major       uint32
  Dev_minor       uint32
  ...
}

Summary
In this chapter, you learned what system calls are and how to write a
simple application to interface with the operating system by using the
sys/unix package. You dug deeper into system calls by looking at an open
source project to learn how it uses the system calls to provide statistical
information about a particular file.
In the next chapters, you will explore system calls more and you will
look at various ways to interface with the operating system using Go.

14
CHAPTER 2

System Calls Using


Go
In this chapter, you will explore writing applications that perform system-­
level operations using system calls. The operating system provides a lot of
ways for applications to extract information and perform operations. You
will look at the different ways to extract system-level information and use
both the Go standard library and system files.
In this chapter, you will learn the following:

• How to use syscall packages

• How to understand and read ELF format files

• How to use the /sys filesystem


• How to write a simple application to read disk statistics

Source Code
The source code for this chapter is available from the https://github.
com/Apress/Software-Development-Go repository.

© Nanik Tolaram 2023 15


N. Tolaram, Software Development with Go,
https://doi.org/10.1007/978-1-4842-8731-6_2
Chapter 2 System Calls Using Go

Syscall Package
The syscall package is the standard library provided by Go that provides
function calls that interface with the log-level operating system. The
following are some of the functionalities provided by the package:
• Change directory

• Duplicate file descriptor

• Get current working directory

• …and many more

syscall Application
Let’s take the existing application from Chapter 1 and convert it to use
the syscall package. The app can be seen inside the chapter2/syscalls
directory. Open terminal and run the sample as follows:

go run main.go

You will see the following output:

2022/07/17 19:20:42 Getpid :  23815


2022/07/17 19:20:42 Getpgrp :  23712
2022/07/17 19:20:42 Getpgrp :  23712
2022/07/17 19:20:42 Gettid :  23815
2022/07/17 19:20:42 /home/nanik/go/chapter2/syscal

The sample code uses system calls to get information about itself such
as the process id assigned by the operating system for itself, the parent id,
and others. The following shows how it uses the syscall package:

package main

import (

16
Chapter 2 System Calls Using Go

  "log"
  s "syscall"
)

func main() {
  ...

  log.Println("Getpid : ", s.Getpid())


  ...

  _, err := s.Getcwd(c)

  ...
}

The code is the same except for replacing the golang.org/x/sys/


unix package with the syscall package, while the function call remains
the same.
Figure 2-1 shows the comparison between the sys/unix and syscall
packages. As you can see, there are functions providing the same
functionality available in both packages.

17
Chapter 2 System Calls Using Go

Figure 2-1. sys/unix vs. syscall

Checking Disk Space


You are going to take a look at an example application that can be found
inside the chapter2/diskspace directory. The application uses the
syscall package to obtain hard disk information such as free space, total
space, and such.
Open terminal and run the sample as follows:

go run main.go

You will see the following output:

Total Disk Space : 460.1 GB


Total Disk Used  : 322.4 GB
Total Disk Free  : 137.7 GB

18
Chapter 2 System Calls Using Go

The output shows in gigabytes the total size of the drive, total amount
of disk used, and total amount of disk free. The following code snippet
shows how the disk information is obtained using the syscall package:

func main() {
  var statfs = syscall.Statfs_t{}
  var total uint64
  var used uint64
  var free uint64
  err := syscall.Statfs("/", &statfs)
  if err != nil {
     fmt.Printf("[ERROR]: %s\n", err)
  } else {
     total = statfs.Blocks * uint64(statfs.Bsize)
     free = statfs.Bfree * uint64(statfs.Bsize)
     used = total - free
  }

  ...
}

As seen in the above code snippet, the application uses the syscall.
Statfs function call to get information about the path. In this case, it’s the
root directory. The result is populated into the statfs variable, which is of
type Statfs_t. The Statfs_t struct declaration looks like the following:

type Statfs_t struct {


  Type    int64
  Bsize   int64
  Blocks  uint64
  Bfree   uint64
  Bavail  uint64
  Files   uint64
  Ffree   uint64

19
Chapter 2 System Calls Using Go

  Fsid    Fsid
  Namelen int64
  Frsize  int64
  Flags   int64
  Spare   [4]int64
}

Webserver with syscall


Let’s take a look at another example using the syscall package, which can
be found inside the chapter2/webserversyscall directory. The sample
code is a web server that uses the syscall package to create a socket
connection.
Open terminal and run the sample as follows:

go run main.go

You will see the following output:

2022/07/17 19:27:49 Listening on  127.0.0.1 : 8888

The web server is now ready to accept connection on port 8888. Open
your browser and type in http://localhost:8888. You will get a response
in your browser: Server with syscall
The following code snippet shows the function that takes care of
starting up the server that listens on port 8888:

func startServer(host string, port int) (int, error) {


  fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_
STREAM, 0)
  if err != nil {
     log.Fatal("error (listen) : ", err)
  }

20
Chapter 2 System Calls Using Go

  sa := &syscall.SockaddrInet4{Port: port}


  addrs, err := net.LookupHost(host)
  ...
  for _, addr := range addrs {
  ...
  }
  ...
  return fd, nil
}

The code performs the following process:


• Creates a socket
• Binds a socket to port 8888
• Listens for an incoming request
The code use syscall.Socket to create a socket. Once it is able to
create a socket, it will bind it to the specified port 8888 by calling syscall.
Bind, as shown in the following code snippet:

for _, addr := range addrs {


  ...
  if err = syscall.Bind(fd, srv); err != nil {
     log.Fatal("error (bind) : ", err)
  }
}

On successful completion of the binding process, the code starts


listening for incoming requests, as shown here:

if err = syscall.Listen(fd, syscall.SOMAXCONN); err != nil {


  log.Fatal("error (listening) : ", err)
} else {
  log.Println("Listening on ", host, ":", port)
}

21
Chapter 2 System Calls Using Go

The syscall.Listen is called, passing syscall.SOMAXCONN as the


parameter. This instructs the operating system that the code wants to have
the maximum queue allocated to take care of pending connections when
they happen. Now the server is ready to accept connections.
The next part of the code accepts and processes incoming requests,
which can be seen in the following code snippet:

for {
  cSock, cAddr, err := syscall.Accept(fd)

  if err != nil {


    ...
  }

  go func(clientSocket int, clientAddress syscall.Sockaddr) {


     err := syscall.Sendmsg(clientSocket, []byte(message),
[]byte{}, clientAddress, 0)
     ...
     syscall.Close(clientSocket)
  }(cSock, cAddr)
}

The code uses syscall.Accept to start accepting incoming requests,


as can be seen in the for{} loop. On every accepted request, the code
processes the request by processing it in a separate go routine. This allows
the server to be able to process incoming requests without being blocked.

ELF Package
The standard library provides different packages that can be used to
interact with different parts of the operating system. In the previous
sections, you looked at interacting on a system level by using the different
standard library packages. In this section, you will look at the debug/elf
package.

22
Chapter 2 System Calls Using Go

This package provides interfaces for applications to interact with


ELF files. ELF stands for the Executable Linkable Format, which means
that an ELF file can be an executable or object file that is used for linking
processes to create an executable file. I will not go into detail on ELF; more
information can be found at https://linux.die.net/man/5/elf.

High-Level ELF Format


ELF is a common standard file format for executable files, object code,
shared libraries, and core dumps; it is cross platform. Figure 2-2 shows at
high level the structure of an ELF file.

Figure 2-2. ELF file structure

23
Chapter 2 System Calls Using Go

Figure 2-3 shows output of the header section of a sample application


compiled on my local machine.

Figure 2-3. ELF file header section

Dump Example
In this section, you will take a look at an open source project named
GoPlay, which is hosted at https://github.com/n4ss/GoPlay. It can also
be found inside the chapter2/GoPlay directory. This is a simple app that
dumps the contents of a Go ELF executable file. You will look at how the
application uses the Go library to read the ELF file
Compile the GoPlay application to create an executable using the
following command:

go build main.go

Now compile GoPlay and run it as follows:

./goplay -action=dump -filename=./goplay

24
Chapter 2 System Calls Using Go

You are instructing GoPlay to dump the contents of the goplay


executable, which will give you output something like the following:

Tracing program : "[path]goplay".


Action : "dump".
DynStrings:
Symbols:
      go.go
      runtime.text
      cmpbody
      countbody
      memeqbody
      indexbody
      indexbytebody
      gogo
      callRet
      gosave_systemstack_switch
      setg_gcc
      aeshashbody
      debugCall32
      debugCall64

      ....
      runtime.(*cpuProfile).addNonGo
      ....
       _cgo_init
       runtime.mainPC
       go.itab.syscall.Errno,error
       runtime.defaultGOROOT.str
       runtime.buildVersion.str
       type.*
       runtime.textsectionmap
   ....

25
Chapter 2 System Calls Using Go

Let’s start analyzing how the code works and what system calls it is
using to get what information out from the executable file.

func main() {
   ....
       file, err := os.Stat(*filename)
   ....
       f, err := os.Open(*filename)
   ....
       switch *action {
   ....
       case "dump": os.Exit(dump_elf(*filename))
       }
   } else {
       goto Usage
   }
   ....
}

On startup, the application uses the os.Stat system call to check


whether the executable file specified as the parameter exists and opens
it using os.Open if it does exist. Once open, it will use the function dump_
elf(..) to dump the file contents. The following is a snippet of the function:

func dump_elf(filename string) int {


   file, err := elf.Open(filename)
   if err != nil {
       fmt.Printf("Couldn't open file : \"%s\" as an ELF.\n")
       return 2
   }
   dump_dynstr(file)
   dump_symbols(file)
   return 0
}

26
Chapter 2 System Calls Using Go

The function uses another system call named elf.Open, which is


available inside the debug/elf package. This is similar to the os.Open
function but with the additional functionality that the opened file is
prepared to be read as an ELF file. On returning from calling elf.Open,
the returned file variable will be populated with information about the
internals of the ELF file.
Once the file is open, it calls dump_symbols to dump the file contents.
The dump_symbols function dumps all symbols information from the file,
which is made available by calling the file.Symbols() function. The
application just prints the Name field.

func dump_symbols(file *elf.File) {


   fmt.Printf("Symbols:\n")
   symbols, _ := file.Symbols()
   for _, e := range symbols {
       if !strings.EqualFold(e.Name, "") {
           fmt.Printf("\t%s\n", e.Name)
       }
   }
}

The following is the struct definition of the Symbol struct. As you can
see, it contains useful information.

type Symbol struct {


  Name        string
  Info, Other byte
  Section     SectionIndex
  Value, Size uint64

27
Chapter 2 System Calls Using Go

  // Version and Library are present only for the


dynamic symbol
  // table.
  Version string
  Library string
}

The other function called to dump ELF information is dump_dynstr:

func dump_dynstr(file *elf.File) {


  fmt.Printf("DynStrings:\n")
  dynstrs, _ := file.DynString(elf.DT_NEEDED)
  ...
  dynstrs, _ = file.DynString(elf.DT_SONAME)
  ...
  dynstrs, _ = file.DynString(elf.DT_RPATH)
  ...
  dynstrs, _ = file.DynString(elf.DT_RUNPATH)
  ...
}

This function is used to obtain certain parts of the ELF file, which are
passed as parameters when calling the file.DynString function. For
example, when calling

dynstrs, _ = file.DynString(elf.DT_SONAME)

the code will get information about the shared library name of the file.

/sys Filesystem
In this section, you will look at a different way of reading system-level information.
You will not use a function to read system information; rather, you will use system
directories that are made available by the operating system for user applications.

28
Chapter 2 System Calls Using Go

The directory that you want to read is the /sys directory, which is a
virtual filesystem containing device drivers, device information, and other
kernel features. Figure 2-4 shows what the /sys directory contains on a
Linux machine.

Figure 2-4. Inside the /sys directory

Reading AppArmor
Some of the information that is provided by Linux inside the /sys
directory is related to AppArmor (short for Application Armor). What is
AppArmor? It is a kernel security module that gives system administrators
the ability to restrict application capabilities with a profile. This gives
system administrators the power to select which resources a particular
application can have access to. For example, a system administrator can
define Application A to have network access or raw socket access, while
Application B does not have access to network capabilities.
Let’s look at an example application to read AppArmor information
from the /sys filesystem, specifically whether AppArmor is enabled and
whether it is enforced. The following is the sample code that can be found
inside the chapter2/apparmor directory:

import (
  "fmt"

29
Chapter 2 System Calls Using Go

   ...
)

const (
  appArmorEnabledPath = "/sys/module/apparmor/parameters/
enabled"
  appArmorModePath    = "/sys/module/apparmor/parameters/mode"
)

func appArmorMode() (mode string) {


  content, err := ioutil.ReadFile(appArmorModePath)
  ...
  return strings.TrimSpace(string(content))
}

func appArmorEnabled() (support bool) {


  content, err := ioutil.ReadFile(appArmorEnabledPath)
  ...
  return strings.TrimSpace(string(content)) == "Y"
}

func main() {
  fmt.Println("AppArmor mode : ", appArmorMode())
  fmt.Println("AppArmor is enabled : ", appArmorEnabled())
}

Since the code is accessing a system filesystem, you must run it using
root. Compile the code and run it as follows:

sudo ./apparmor

The code reads the information from the directory using the standard
library ioUtil.ReadFile, which is just like reading a file, so it’s simpler
than using the function calls that you looked at in the previous sections.

30
Chapter 2 System Calls Using Go

Summary
In this chapter, you looked at using system calls to interface with the
operating system. You looked at using the syscall standard library that
provides a lot of function calls to interface with the operating system
and wrote a sample application to print out disk space information.
You looked at how the debug/elf standard library is used to read Go
ELF file information. Lastly, you looked at the /sys filesystem to extract
information that you want to read to understand whether the operating
system supports AppArmor.

31
Discovering Diverse Content Through
Random Scribd Documents
Catlin, 281.
Cayuse, 44.
Cedar Lake, 121.
Chadwick, Mr., 238, 244.
Chardon, Mr., 304, 306, 315.
Cheyennes, 131, 154, 157, 161, 188, 329, 335.
Chihuahua, 194, 203.
Chihuahua, game of, 203.
Chinooke, 8.
Chinook Point, 9.
Chinooks, 9.
Chipita, 184, 185.
Chouteau County (Mont.), 328.
Chouteau, Jr. & Co., Pierre, 238.
Chouteaus, 130.
Cimarron River, 167.
Clarke, John, 35.
Clark’s Fork, 17.
Cochran, J. V., 358.
Columbia, Forks of, 26.
Columbia River, 3, et seq.
Comanches, attack by, 260.
Comanches, 138, 154, 163, 176, 188, 260.
Come Comly, 37.
Comeloups, 27.
Coolidge, Mr., 264, 266, 267.
Council at Fort Benton, The, 323, 325.
Council Bluffs, 282.
Council Grove, Kan., 241.
Cowlitz River, 72.
Cox, Ross, 5, 24.
Creeks (see River).
Crees, 100.
Crooks, Ramsey, 24, 25, 27.
Crows, 116, 312, 329, 332, 335.
Cuba, 194.

Daughters of the American Revolution, 190.


Davis, Charles, 179.
Dawson, James, 320.
Day, John, 25, 27.
Day’s, John, Valley, 104, 105.
Dease, J. W., 94.
Deep Creek, 334.
Delawares, 171, 175.
De Lisle, Frank, 239.
Diamond Springs, 142.
Diaz, Porfirio, 195.
Dog flesh as food, 253.
“Dolly” (schooner), 23.
Doniphan, Colonel A. W., 142, 204.
Doniphan’s Expedition, 132, 136.
Drinker, Mr., 238.

Eagle Creek, 350.


Eagle Eye, 330.
Eagle Tail Feathers, 138.
Edmonton, 121.
El Paso, 194, 205.
Emigrant trail, 183.
Eyacktana, 48, 50.
Eyakema Valley, 44.

Famasi, 349, 358.


Farnham, R., 24.
Farnham, Thos. J., 158.
Father of all Children, 352.
Feast, Indian, 60.
Fernandez, 145.
Fight at Arroyo Hondo, 147.
Fight with Blackfeet, 341.
Fisher, 179.
Fitzpatrick, Thos., 140, 172.
Flathead House, 117.
Flathead River, 102.
Flathead River Post, 100.
Flatheads, 100.
Flat Willow Creek, 336, 338.
Fontaine-qui-bouille, 208.
Forest and Stream, 326.
Fort Adobe, 138, 139, 153.
Assiniboine, 121.
Bent, 127, 130, 248.
Benton, 326.
Berthold, 316.
Fauntleroy, 158.
Flathead River Post, 100.
Garry, 5.
George, 5, 42.
Jasper House, 121.
Larpenteur’s Post, 304.
Leavenworth, 132.
Lyon, 158.
McKenzie, 301.
Minitaree, 304.
Minitaree Post, 320.
Nez Percés, 71, 72, 75, 94.
Okanagan, 43, 53.
Pierre, 290.
Rocky Mountain House, 121.
St. Vrain, 138, 139, 153.
Spokane, 27, 28, 32.
Spokane House, 95, 100.
Union, 290, 306, 307, 309, 320, 346.
Vermilion, 289.
White River Post, 303, 304.
William (Bent’s), 130, 171, 248.
William (N. W. Co.), 42.
Wise, 158.
Fourchette Creek, 348.
Francisco, 131.
Franklin, Captain John, 121.
Fraser River, 23.
Fur Hunters of the Far West, 4, 41.

Game of Chihuahua, 203.


Garrard, Lewis H., 132, 145, 237.
Garry, Fort, 5.
George, Fort, 5, 42.
“Gibraltar of Columbia,” 76.
Goat, white, 19.
Goddin River, 104, 105, 116.
Governor Charles Bent, 141, 259.
Grand Detour, 304.
Greeley, Colo., 154.
Green, Andrew, 137, 184.
Green, Dick, 138.
Greenhorn River, 152, 208.
Grizzly bear, 64.
Gros Ventres, 329, 332, 349, 352, 353.

Hallock, Charles, 176.


Hamilton, Wm. T., 325.
Hatcher, 179, 180, 182, 184.
Hawkins, John, 208.
Hē-hĭm´nĭ-ho-năh´, Freckled Hand, 179.
Hell’s Gate, 101.
Highwood Mountains, 333.
Hĭs´sī-o-mē´tă-nē, 167.
Hodgens, 88, 89, 90.
Ho-nīh´, Wolf, 179.
Horses recovered from Crows, 165.
Horse-taking by Comanches, 164.
Hudson Bay Company, 3, et seq.
Hudson Bay Company, With the, 91.
Huerfano River, 208.
Hughes, J. T., 132.
Hunt, Wilson Price, 5, 24, 35.

Independence, Mo., 281.


Indian feast, 60.
Indian tribes:
Abenakis, 58;
Apache, 138, 154, 188;
Arapahoes, 154, 161, 188, 260, 329, 335;
Ban-at-tees, 88, 111;
Bannocks, 88;
Blackfeet, 101, 103, 104, 116, 329, 335, 352;
Bloods, 329, 352;
Calispels, 100;
Cath-le-yach-é-yach, 42;
Cayuse, 44;
Cheyennes, 131, 154, 157, 161, 188, 329, 335, 362;
Chinooke or Chinooks, 9;
Comanches, 138, 154, 163, 176, 188, 260;
Crees, 100;
Crows, 116, 312, 329, 332, 335;
Delawares, 171, 175;
Flatheads, 100;
Gros Ventres, 329, 332, 349, 352, 353;
Iroquois, 58, 65, 72, 73, 100, 101, 115;
Kiowas, 138, 154, 155, 161, 188;
Kutenais, 100;
Minitarees, 309;
Navajo, 142;
Nez Percés, 16, 27, 32, 44, 88, 102, 116;
Okanagan, 54;
Palouse, 100;
Piegans, 95, 98, 99, 100, 101, 104, 105, 106, 329, 352, 355;
Pisscows, 44, 53;
Saulteaux, 100;
Shahaptin, 16, 17;
Shaw-ha-ap-tens (See Shahaptin);
Shawnees, 171, 175;
She-Whaps, 23, 27, 61, 64;
Shoshoni, 88, 178;
Sioux, 329, 335;
Snakes, 25, 72, 81, 84, 85, 88, 90, 100;
Spokanes, 100;
Suhtai, 167;
Walla Walla, 17, 82;
War-are-ree-kas (Shoshoni), 89, 111;
Wy-am-pams, 16;
Wyandottes, 239.
Indians and Their Battles, 75.
International boundary, 321.
Iroquois, 58, 65, 72, 73, 100, 101, 115.
Ishmah, 301, 308.

Jack, 330, 358.


Jasper House, 121.
Juarez, 195.
Judith Mountains, 335, 336.
Judith River, 326, 327, 335.

Kamloops, 27.
Kansas City, Mo., 141.
Kearny, General S. W., 142, 161, 162, 204.
Keith, James, 42, 43, 58.
Kiowas, 138, 154, 161, 188.
Kiowa woman, 168.
Kipp, James, 281, 289, 320.
Kipp, Joe, 328.
Kittson, Mr., 77, 78, 81.
Knife River, 304.
Kutenais, 100.

La Bonté, 233.
La Cañada, 148.
Lake Bourbon, 121.
Larocque, Joseph Felix, 121.
Larpenteur’s Post, 304.
Leavenworth, Fort, 132.
Lee, General, 238.
Lewis and Clark, 71, 103.
Lewis Fork, 17, 90.
Life at Bent’s Fort, 170.
Little Blackfoot River, 356.
Little Dog, 330, et seq.
Little Missouri River, 316, 317.
Little Mountain, 138.
Little Rocky Mountains, 348, 349.
Little White Man, 128.
Long Narrows, 24, 26.

Madeira Islands, 194.


Maguey, 197.
Malades River, 105, 112.
Mammoth Cave, Ky., 280.
Marias River, 356.
“Martha,” 320.
Maxwell, L., 179.
McDonald, Finan, 94, 98, 99, 103.
McDonald, John (Bras Croche), 43.
McDougall, Duncan, 6, 10, 36.
McKay, Alexander, 6, 24.
McKay, young, 44.
McKenzie, Alexander, 3.
McKenzie, Donald, 24, 25, 26, 33, 58, 69, 73, 78.
McKenzie, Kenneth, 238.
McLellan, Donald, 5, 24.
McTavish, J. G., 32.
Meagher, General Francis, 326, 327, 328, 353.
Medicine Springs, 332, 335.
Měn-ěs-tō´-kōs, 352.
Minitaree Fort, 304.
Minitaree Post, 320.
Minitarees, 309.
Missoula, Mont., 327.
Missouri River, 332.
Moccasin Mountains, 332.
Mora, N. M., 158.
Mormons, 282.
Mountain Chief, 357.
Mountain sheep, 221.
Mountains:
Bear Paw, 346, 350;
Big Snowy, 338, 340;
Bull, 336, 337, 340;
Highwood, 333;
Judith, 335, 336;
Little Rocky, 348, 349;
Moccasin, 332;
Turtle, 316.
Mourning, Indian, 82, 347.
Munson, Judge, 328.
Murray, 178, 179, 180.
Musselshell River, 336, 337, 339, 348, 357.
My Sixty Years on the Plains, 326.

Navajo Indians, 142.


New Caledonia, 25.
Nez Percés, 16, 27, 32, 44, 88, 102, 116.
Nez Percés Fort, 71, 72, 75, 94.
Nō-mă-nĭh´, Fish, 179.
North Platte River, 183.
Northwest Company, 3, et seq.
Northwesters, 4, et seq.
Northwest Fur Co., 41.
Norway House, 121, 122.

Oakinacken River, 19.


Ogden, Peter, 73.
Okanagan, Fort, 43, 53.
Okanagan Indians, 54.
Okanagan River, 19, et seq.
Okanogan, 20.
Okinagan, 20.
“Old Bark,” 259.
One-eyed Juan, 183.
Otero Co. (Colo.), 190.
Outfit for prairie travel, 330.
Owl Woman, 129.

Pacific Fur Co., 5, et seq.


Paint, 239.
Palliser, Colonel Wray, 277.
Palliser, John, 277.
Palouse, 100.
Paquenode, 306.
Parkman, 237.
Pau-ē-sīh´, Flat Nose, 179.
Pawnee Fork, 176, 260.
“Pawnee” (Kiowa chief), 155.
Payette River, 107.
Peacock’s Ranch, 155.
Peck, R. M., 154.
Pérey, 306, 308.
Piegans, 98, 99, 100, 101, 104, 105, 106, 329, 352.
Piegans, battle with, 95.
Pierre, Fort, 290.
Pierre, S. D., 290.
“Pilot Knobs,” 90.
Pisscow River, 19.
Pisscows, 44, 53.
Plum Creek, 335.
Point Canoe, 113.
Point Chinook, 9.
Point George, 9.
Point Vancouver, 14.
Pŏ-ō-om´măts, Gray Blanket, 179.
Poor (Lean) Bear, 138.
Porcupine Creek, 340, 343.
Poshett Creek, 348.
Power, T. C., 328.
Prairie fire, 284.
Prairie traveller, 277.
Priest Rapids, 19.
Prince, 85.
Pueblo, Colo., 129, 130, 157.
Pulque, 197.
Purgatoire River, 129, 130, 157 171.

Quarrel, Indian, 86.

“Raccoon,” 37.
Raids by Indians, 198.
Rattling Buttes, 335.
Red Coat Land, 356.
Red River (of North,) 121, 122, 123.
Red River (of Texas), 167.
Red River settlement, 4.
Red Sleeves, 176.
Red Sleeves Creek, 176.
Reed, John, 33.
Reid (See Reed, J.), 88.
Reid’s River (see Payette River, 107).
Reynolds, A. E., 189.
Richardson, Dr., 121.
Rio del Norte, 205.
Rio Grande, 205.
Rivers:
Arkansas, 128, 129, 130, 189;
Arkansor, 278;
Arrow, 333;
Bear, 112;
Beaver, 349;
Box Elder, 336;
Canadian, 138;
Cimarron, 167;
Clark’s Fork, 17;
Columbia, 3, et seq.;
Columbia, Forks of, 26;
Cowlitz, 72;
Deep, 334;
Eagle, 350;
Flathead, 102;
Flat Willow, 336, 338;
Fontaine-qui-bouille, 208;
Fourchette, 348;
Fraser, 23;
Goddin, 104, 105, 116;
Greenhorn, 152, 208;
Huerfano, 208;
Judith, 326, 327, 335;
Knife, 304;
Lewis Fork, 17, 90;
Little Blackfoot, 356;
Little Missouri, 316, 317;
Malades, 105, 112;
Marias, 356;
Missouri, 332;
Musselshell, 336, 337, 339, 348, 357;
North Platte, 183;
Oakinacken, 19;
Okanagan, 19, et seq.;
Oregon, 4;
Pawnee Fork, 176, 260;
Payette, 107;
Pisscow, 19;
Plum, 335;
Porcupine, 340, 343;
Poshett, 348;
Purgatoire, 129, 130, 157, 171;
Red (of North), 121, 122, 123;
Red (of Texas), 167;
Red Sleeves, 176;
Reid’s (See Payette), 107;
Rio del Norte, 205;
Rio Grande, 205;
Rivière aux Malades, 105, 112;
Saint Vrain’s Fork, 138;
Salmon, 104;
Sa-mick-a-meigh, 31;
Skam-naugh, 76;
Smilkameen, 31;
Snake, 25;
Teton, 356;
Walnut, 155;
Willamette, 58;
Willow, 334;
Wolf, 334;
Yellowstone, 281, 306.
Rivière aux Malades, 105, 112.
Rocky Mountain House, 121.
Ross, Alexander, 3, et passim.
Rowan, Mr., 121.
Running down a calf, 314.
Russell, Colonel, 264.
Russian America, 5.
Ruxton, George Frederick, 191, 193.

Sagacity of wolves, 208.


Sage grouse, 64.
St. Louis, 194.
St. Vrain, Ceran, 130.
St. Vrain (death of C.), 158.
St. Vrain, Felix, 158.
St. Vrain’s Fork, 138.
Salmon River, 104.
Sa-mick-a-meigh River, 31.
Sand Creek massacre, 249.
Sandwich Islanders, 58.
Sandwich Islands, 7.
San Fernandez, 145.
San Fernando, 142.
Santa Anna, General, 195.
Santa Fé, 129, et seq.
Santa Fé trail, 127.
Saulteaux, 100.
Scalp dance, 256.
Seaton, Alfred, 33.
Sedgwick, Major, 155.
Shahaptin, 16, 17.
Shaved Head, 138.
Shaw-ha-ap-tens (see Shahaptin).
Shawnees, 171, 175.
She-Whaps, 23, 27, 61, 64.
“Short Man, The,” 326.
Shoshoni, 88, 178.
Simplicity of Indians, 19.
Simpson, Governor, 95, 117, 121.
Sioux, 329, 335.
Skam-naugh River, 76.
Skunk, adventure with, 20.
Small Robe band (Piegan), 355.
Smallpox at Bent’s Fort, 131.
Smilkameen River, 31.
Smith, Green Clay, 327.
Smith, John, 179, 248, 252, 253, 254, 255.
Snake Indians, 25, 72, 81, 84, 85, 88, 90, 100.
Snake River, 25.
Solitary Hunter, The, 275, 277, 321.
Spokane House, 95, 100.
Spokanes, 100.
Spotted Horse, 344.
Staked Plains, 167.
Star Robe, 350, 358.
Stevens, Governor I. I., 326.
Stuart, Alexander, 42, 43.
Stuart, David, 6, 22, 25, 26, 27.
Stuart, Robert, 24, 25, 27.
Suhtai, 167.

Tailing the bull, 200.


Taos, 129, 137, 145.
Teton River, 356.
Thompson, David, 13, 15, 17.
Thorn, Captain, 7, 9, 24.
Three Tetons, 90, 115.
Thunder Birds, 187.
“Tod Issac,” 36.
To´hau sen, 138, 155.
“Tonquin,” 7, 13, 23.
Tonquin Point, 9.
Trade for horses, 182.
Trade for liquor, 181.
Trade for robes, 182.
Trade, winter’s, 23.
Trails of the Pathfinders, 24, 31.
Train-wrecking by Cheyennes, 362.
Trapper’s earnings, 219.
Trappers’ methods, 79.
Trapper’s outfit, 216.
Traps stolen, 107.
Travel by ox train, 172.
Travois dog, 300.
Tummatapam, 17.
Tunica, 353.
Turley, 147.
Turley’s Ranch, 147.
Turtle Mountains, 316.
“Twins, The,” 71.
Union, Fort, 290, 306, 307, 309, 320, 346.
Upson, Gad. E., 357.

Valley, John Day’s, 104, 105.


Vasquez, Benito, 130.
Vera Cruz, 194.
Vermilion, Fort, 289.
Vide Poche, 242.
Vigil, 144.
Vī-hiu-nĭs´, Little Chief, 179.

Wah-To-Yah and the Taos Trail, 237.


Walla Walla, 27.
Walla Walla Indians, 17, 82.
Walnut Creek, 155.
War-are-ree-kas, 89, 111.
War of 1812, 36.
Wa-si´cha-chischi´-la, 128.
Westport, Mo., 141, 171, 238.
White Cow Woman, 168.
White goat, 19.
White River Post, 303, 304.
White Thunder, 130.
Willamette River, 58.
William, Fort (Bent’s), 130, 171, 248.
William, Fort (N. W. Co.), 42.
Williams, Bill, 228, 294, 325.
Willow Creek, 334.
Winnipeg, 5.
Winter storm, 211.
Wŏhk´po-hŭm´, White Horse, 179.
Wolf Creek, 334.
Wolf-shooting, 68.
Wolverine, 229.
Wolves, sagacity of, 208.
Wolves, sleeping, 300.
Wolves, ways of, 54.
Work of a fur trader, 54.
Wounded bull, 246.
Wō-wĭhph´pai-ī-sīh´, Big Nostrils, 179.
Wy-am-pams, 16.
Wyandottes, 239.

Yellow Woman, 130.


Yellowstone National Park, 90.
Yellowstone River, 281, 306.
ADVENTURE AND
EXPLORATION

Beyond the Old Frontier


Adventures of Indian Fighters,
Hunters, and Fur Traders
By GEORGE BIRD GRINNELL
A series of personal narratives of hunting, Indian fighting,
and exploration in the early pioneer days.

Missionary Explorers
Among the American
Indians
By MARY GAY HUMPHREYS
The stories of the first and greatest of American
missionaries to the American Indians, told largely in their own
words.

True Tales of Arctic Heroism


in the New World
By MAJOR-GENERAL A. W. GREELY, U. S. A.
The true stories of the most heroic adventures on the Arctic
expeditions from the earliest explorers to our own day.

The Boy’s Story of Zebulon


M. Pike
Explorer of the Great Southwest
Edited by MARY GAY HUMPHREYS
“A brilliant story of adventure and achievement.”—
Washington Star.

Trails of the Pathfinders


By GEORGE BIRD GRINNELL
Accounts taken from the journals of Henry, Lewis and Clark,
Pike, Frémont, and others.
The Boy’s Catlin
My Life Among the Indians
Edited with Biographical Introduction by
MARY GAY HUMPHREYS
“As interesting a story of Indians as was ever written and
has the merit of being true.”—New York Sun.

The Boy’s Drake


By EDWIN M. BACON
“Much of the story is told in the words of old records, and
interesting old maps and pictures make it still more valuable.”—
The Bookman.

The Boy’s Hakluyt


By EDWIN M. BACON
The voyages of Hawkins, Drake, Gilbert, and others, retold
from Hakluyt’s chronicles.

Each Illustrated. 12mo. $1.50 net


The Adventures of James
Capen Adams
Mountaineer and Grizzly Bear
Hunter of California
By THEODORE H. HITTELL
Illustrated. $1.50 net. Postpaid, $1.65
The story of the life and thrilling adventures of one of the
most famous of American hunters and one of the first great
tamers of wild animals.
The narrative is given just as it first appeared in the simple,
direct language in which “Grizzly” Adams told it to Mr. Hittell—a
style that bears all the marks of absolute truth.

The Grizzly Bear


By WILLIAM H. WRIGHT

Illustrated from Photographs by the


Author and J. B. Kerfoot. $1.50 net
“Full of the atmosphere of the big game woods and vibrant
with hazards of the chase.”—Boston Globe.
“The very spirit of the grizzly is in subtle fashion brought
near us. The book will long hold a high place in the literature of
sport.”—New York Tribune.

You might also like