~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/uti/sge_edit.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 * 
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 * 
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 * 
 
9
 * 
 
10
 *  Sun Industry Standards Source License Version 1.2
 
11
 *  =================================================
 
12
 *  The contents of this file are subject to the Sun Industry Standards
 
13
 *  Source License Version 1.2 (the "License"); You may not use this file
 
14
 *  except in compliance with the License. You may obtain a copy of the
 
15
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 
16
 * 
 
17
 *  Software provided under this License is provided on an "AS IS" basis,
 
18
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 
19
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 
20
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 
21
 *  See the License for the specific provisions governing your rights and
 
22
 *  obligations concerning the Software.
 
23
 * 
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 * 
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 * 
 
28
 *   All Rights Reserved.
 
29
 * 
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
 
 
33
#include <string.h>
 
34
#include <stdio.h>
 
35
#include <sys/types.h>
 
36
#include <sys/wait.h>
 
37
 
 
38
#include "sge_prog.h"
 
39
#include "sge.h"
 
40
#include "sgermon.h"
 
41
#include "sge_log.h"
 
42
#include "sge_signal.h"
 
43
#include "msg_common.h"
 
44
#include "sge_edit.h"
 
45
#include "sge_unistd.h"
 
46
 
 
47
int sge_edit(const char *fname, uid_t myuid, gid_t mygid)
 
48
{
 
49
   SGE_STRUCT_STAT before, after;
 
50
   pid_t pid;
 
51
   int status;
 
52
   int ws = 0;
 
53
 
 
54
   DENTER(TOP_LAYER, "sge_edit");;
 
55
 
 
56
   if (fname == NULL) {
 
57
      ERROR((SGE_EVENT, MSG_NULLPOINTER));
 
58
      return -1;
 
59
   }
 
60
 
 
61
   if (SGE_STAT(fname, &before)) {
 
62
      ERROR((SGE_EVENT, MSG_FILE_EDITFILEXDOESNOTEXIST_S, fname));
 
63
      DEXIT;
 
64
      return -1;
 
65
   }
 
66
 
 
67
   chown(fname, myuid, mygid);
 
68
 
 
69
   pid = fork();
 
70
   if (pid) {
 
71
      while (ws != pid) {
 
72
         ws = waitpid(pid, &status, 0);
 
73
         if (WIFEXITED(status)) {
 
74
            if (WEXITSTATUS(status) != 0) {
 
75
               ERROR((SGE_EVENT, MSG_QCONF_EDITOREXITEDWITHERROR_I,
 
76
                      (int) WEXITSTATUS(status)));
 
77
               DEXIT;
 
78
               return -1;
 
79
            }
 
80
            else {
 
81
               if (SGE_STAT(fname, &after)) {
 
82
                  ERROR((SGE_EVENT, MSG_QCONF_EDITFILEXNOLONGEREXISTS_S, fname));
 
83
                  DEXIT;
 
84
                  return -1;
 
85
               }
 
86
               if ((before.st_mtime != after.st_mtime) || 
 
87
                    (before.st_size != after.st_size)) { 
 
88
                  DEXIT;
 
89
                  return 0;
 
90
               }
 
91
               else {
 
92
                  /* file is unchanged; inform caller */
 
93
                  DEXIT;
 
94
                  return 1;
 
95
               }
 
96
            }
 
97
         }
 
98
#ifndef WIN32  /* signals b18 */
 
99
         if (WIFSIGNALED(status)) {
 
100
            ERROR((SGE_EVENT, MSG_QCONF_EDITORWASTERMINATEDBYSIGX_I,
 
101
                   (int) WTERMSIG(status)));
 
102
            DEXIT;
 
103
            return -1;
 
104
         }
 
105
#endif
 
106
      }
 
107
   } else {
 
108
      const char *cp = NULL;
 
109
 
 
110
      sge_set_def_sig_mask(NULL, NULL);
 
111
      sge_unblock_all_signals();
 
112
      setuid(getuid());
 
113
      setgid(getgid());
 
114
 
 
115
      cp = sge_getenv("EDITOR");
 
116
      if (!cp || strlen(cp) == 0)
 
117
         cp = DEFAULT_EDITOR;
 
118
         
 
119
      execlp(cp, cp, fname, (char *) 0);
 
120
      ERROR((SGE_EVENT, MSG_QCONF_CANTSTARTEDITORX_S, cp));
 
121
      SGE_EXIT(NULL, 1);
 
122
   }
 
123
 
 
124
   DEXIT;
 
125
   return (-1);
 
126
}
 
127