func()

in sigar_linux_common.go [160:222]


func (self *ProcState) Get(pid int) error {
	data, err := readProcFile(pid, "stat")
	if err != nil {
		return err
	}

	// Extract the comm value with is surrounded by parentheses.
	lIdx := bytes.Index(data, []byte("("))
	rIdx := bytes.LastIndex(data, []byte(")"))
	if lIdx < 0 || rIdx < 0 || lIdx >= rIdx || rIdx+2 >= len(data) {
		return fmt.Errorf("failed to extract comm for pid %d from '%v'", pid, string(data))
	}
	self.Name = string(data[lIdx+1 : rIdx])

	// Extract the rest of the fields that we are interested in.
	fields := bytes.Fields(data[rIdx+2:])
	if len(fields) <= 36 {
		return fmt.Errorf("expected more stat fields for pid %d from '%v'", pid, string(data))
	}

	interests := bytes.Join([][]byte{
		fields[0],  // state
		fields[1],  // ppid
		fields[2],  // pgrp
		fields[4],  // tty_nr
		fields[15], // priority
		fields[16], // nice
		fields[36], // processor (last processor executed on)
	}, []byte(" "))

	var state string
	_, err = fmt.Fscan(bytes.NewBuffer(interests),
		&state,
		&self.Ppid,
		&self.Pgid,
		&self.Tty,
		&self.Priority,
		&self.Nice,
		&self.Processor,
	)
	if err != nil {
		return fmt.Errorf("failed to parse stat fields for pid %d from '%v': %v", pid, string(data), err)
	}
	self.State = RunState(state[0])

	// Read /proc/[pid]/status to get the uid, then lookup uid to get username.
	status, err := getProcStatus(pid)
	if err != nil {
		return fmt.Errorf("failed to read process status for pid %d: %v", pid, err)
	}
	uids, err := getUIDs(status)
	if err != nil {
		return fmt.Errorf("failed to read process status for pid %d: %v", pid, err)
	}
	user, err := user.LookupId(uids[0])
	if err == nil {
		self.Username = user.Username
	} else {
		self.Username = uids[0]
	}

	return nil
}