~ubuntu-branches/ubuntu/precise/ledmon/precise-backports

« back to all changes in this revision

Viewing changes to src/slave.c

  • Committer: Package Import Robot
  • Author(s): Daniel Jared Dominguez
  • Date: 2012-01-23 13:45:41 UTC
  • Revision ID: package-import@ubuntu.com-20120123134541-bt1s4b3drja8hn94
Tags: upstream-0.31
ImportĀ upstreamĀ versionĀ 0.31

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- mode: c; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ex: set tabstop=2 softtabstop=2 shiftwidth=2 expandtab: */
 
3
 
 
4
/*
 
5
 * Intel(R) Enclosure LED Utilities
 
6
 * Copyright (C) 2009, Intel Corporation.
 
7
 *
 
8
 * This program is free software; you can redistribute it and/or modify it
 
9
 * under the terms and conditions of the GNU General Public License,
 
10
 * version 2, as published by the Free Software Foundation.
 
11
 *
 
12
 * This program is distributed in the hope it will be useful, but WITHOUT
 
13
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
14
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
15
 * more details.
 
16
 *
 
17
 * You should have received a copy of the GNU General Public License along with
 
18
 * this program; if not, write to the Free Software Foundation, Inc., 
 
19
 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
 
20
 *
 
21
 */
 
22
 
 
23
#include <config.h>
 
24
#include <limits.h>
 
25
#include <string.h>
 
26
#include <stdlib.h>
 
27
#include <stdint.h>
 
28
 
 
29
#if _HAVE_DMALLOC_H
 
30
#include <dmalloc.h>
 
31
#endif
 
32
 
 
33
#include "status.h"
 
34
#include "ibpi.h"
 
35
#include "utils.h"
 
36
#include "list.h"
 
37
#include "sysfs.h"
 
38
#include "block.h"
 
39
#include "slave.h"
 
40
 
 
41
/**
 
42
 */
 
43
static unsigned char _get_state(const char *path)
 
44
{
 
45
  char *p, *t, *s;
 
46
  unsigned char result = SLAVE_STATE_UNKNOWN;
 
47
 
 
48
  s = p = get_text(path, "state");
 
49
  if (p) {
 
50
    while (s) {
 
51
      t = strchr(s, ',');
 
52
      if (t) {
 
53
        *(t++) = '\0';
 
54
      }
 
55
      if (strcmp(s, "spare") == 0) {
 
56
        result |= SLAVE_STATE_SPARE;
 
57
      } else if (strcmp(s, "in_sync") == 0) {
 
58
        result |= SLAVE_STATE_IN_SYNC;
 
59
      } else if (strcmp(s, "faulty") == 0) {
 
60
        result |= SLAVE_STATE_FAULTY;
 
61
      } else if (strcmp(s, "write_mostly") == 0) {
 
62
        result |= SLAVE_STATE_WRITE_MOSTLY;
 
63
      } else if (strcmp(s, "blocked") == 0) {
 
64
        result |= SLAVE_STATE_BLOCKED;
 
65
      }
 
66
      s = t;
 
67
    }
 
68
    free(p);
 
69
  }
 
70
  return result;
 
71
}
 
72
 
 
73
/**
 
74
 */
 
75
static unsigned int _get_errors(const char *path)
 
76
{
 
77
  return get_int(path, 0, "errors");
 
78
}
 
79
 
 
80
/**
 
81
 */
 
82
static unsigned int _get_slot(const char *path)
 
83
{
 
84
  unsigned int result = -1;
 
85
 
 
86
  char *p = get_text(path, "slot");
 
87
  if (p) {
 
88
    if (strcmp(p, "none") != 0) {
 
89
      result = atoi(p);
 
90
    }
 
91
    free(p);
 
92
  }
 
93
  return result;
 
94
}
 
95
 
 
96
/**
 
97
 */
 
98
static int _compare(struct block_device *device, const char *path)
 
99
{
 
100
  return (strcmp(device->sysfs_path, path) == 0);
 
101
}
 
102
 
 
103
/**
 
104
 */
 
105
static struct block_device *_get_block(const char *path, void *block_list)
 
106
{
 
107
  char temp[PATH_MAX];
 
108
  char link[PATH_MAX];
 
109
  struct block_device *device = NULL;
 
110
 
 
111
  str_cpy(temp, path, PATH_MAX);
 
112
  str_cat(temp, "/block", PATH_MAX);
 
113
 
 
114
  if (realpath(temp, link)) {
 
115
    device = list_first_that(block_list, _compare, link);
 
116
  }
 
117
  return device;
 
118
}
 
119
 
 
120
/**
 
121
 */
 
122
struct slave_device * slave_device_init(const char *path, void *block_list)
 
123
{
 
124
  struct slave_device *device = NULL;
 
125
  struct block_device *block;
 
126
 
 
127
  block = _get_block(path, block_list);
 
128
  if (block) {
 
129
    device = malloc(sizeof(struct slave_device));
 
130
    if (device) {
 
131
      device->raid = NULL;
 
132
      device->state = _get_state(path);
 
133
      device->slot = _get_slot(path);
 
134
      device->errors = _get_errors(path);
 
135
      device->block = block;
 
136
    }
 
137
  }
 
138
  return device;
 
139
}
 
140
 
 
141
/**
 
142
 */
 
143
void slave_device_fini(struct slave_device *device)
 
144
{
 
145
  (void)device;
 
146
  /* Function reserved for future improvements. */
 
147
}